1010
1111use hir:: def:: * ;
1212use hir:: def_id:: DefId ;
13+ use hir:: { self , PatKind } ;
1314use ty:: TyCtxt ;
1415use util:: nodemap:: FnvHashMap ;
15-
1616use syntax:: ast;
17- use hir:: { self , PatKind } ;
18- use syntax:: codemap:: { respan, Span , Spanned , DUMMY_SP } ;
17+ use syntax:: codemap:: { Span , Spanned , DUMMY_SP } ;
1918
20- use std:: cell:: RefCell ;
2119use std:: iter:: { Enumerate , ExactSizeIterator } ;
2220
2321pub type PatIdMap = FnvHashMap < ast:: Name , ast:: NodeId > ;
@@ -57,9 +55,9 @@ impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T {
5755
5856// This is used because same-named variables in alternative patterns need to
5957// use the NodeId of their namesake in the first pattern.
60- pub fn pat_id_map ( dm : & RefCell < DefMap > , pat : & hir:: Pat ) -> PatIdMap {
58+ pub fn pat_id_map ( pat : & hir:: Pat ) -> PatIdMap {
6159 let mut map = FnvHashMap ( ) ;
62- pat_bindings ( dm , pat, |_bm, p_id, _s, path1| {
60+ pat_bindings ( pat, |_bm, p_id, _s, path1| {
6361 map. insert ( path1. node , p_id) ;
6462 } ) ;
6563 map
@@ -70,7 +68,6 @@ pub fn pat_is_refutable(dm: &DefMap, pat: &hir::Pat) -> bool {
7068 PatKind :: Lit ( _) | PatKind :: Range ( _, _) | PatKind :: QPath ( ..) => true ,
7169 PatKind :: TupleStruct ( ..) |
7270 PatKind :: Path ( ..) |
73- PatKind :: Ident ( _, _, None ) |
7471 PatKind :: Struct ( ..) => {
7572 match dm. get ( & pat. id ) . map ( |d| d. full_def ( ) ) {
7673 Some ( Def :: Variant ( ..) ) => true ,
@@ -86,7 +83,6 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
8683 match pat. node {
8784 PatKind :: TupleStruct ( ..) |
8885 PatKind :: Path ( ..) |
89- PatKind :: Ident ( _, _, None ) |
9086 PatKind :: Struct ( ..) => {
9187 match dm. get ( & pat. id ) . map ( |d| d. full_def ( ) ) {
9288 Some ( Def :: Variant ( ..) ) | Some ( Def :: Struct ( ..) ) | Some ( Def :: TyAlias ( ..) ) => true ,
@@ -99,7 +95,7 @@ pub fn pat_is_variant_or_struct(dm: &DefMap, pat: &hir::Pat) -> bool {
9995
10096pub fn pat_is_const ( dm : & DefMap , pat : & hir:: Pat ) -> bool {
10197 match pat. node {
102- PatKind :: Ident ( _ , _ , None ) | PatKind :: Path ( ..) | PatKind :: QPath ( ..) => {
98+ PatKind :: Path ( ..) | PatKind :: QPath ( ..) => {
10399 match dm. get ( & pat. id ) . map ( |d| d. full_def ( ) ) {
104100 Some ( Def :: Const ( ..) ) | Some ( Def :: AssociatedConst ( ..) ) => true ,
105101 _ => false
@@ -113,7 +109,7 @@ pub fn pat_is_const(dm: &DefMap, pat: &hir::Pat) -> bool {
113109// returned instead of a panic.
114110pub fn pat_is_resolved_const ( dm : & DefMap , pat : & hir:: Pat ) -> bool {
115111 match pat. node {
116- PatKind :: Ident ( _ , _ , None ) | PatKind :: Path ( ..) | PatKind :: QPath ( ..) => {
112+ PatKind :: Path ( ..) | PatKind :: QPath ( ..) => {
117113 match dm. get ( & pat. id )
118114 . and_then ( |d| if d. depth == 0 { Some ( d. base_def ) }
119115 else { None } ) {
@@ -125,46 +121,25 @@ pub fn pat_is_resolved_const(dm: &DefMap, pat: &hir::Pat) -> bool {
125121 }
126122}
127123
128- pub fn pat_is_binding ( dm : & DefMap , pat : & hir:: Pat ) -> bool {
129- match pat. node {
130- PatKind :: Ident ( ..) => {
131- !pat_is_variant_or_struct ( dm, pat) &&
132- !pat_is_const ( dm, pat)
133- }
134- _ => false
135- }
136- }
137-
138- pub fn pat_is_binding_or_wild ( dm : & DefMap , pat : & hir:: Pat ) -> bool {
139- match pat. node {
140- PatKind :: Ident ( ..) => pat_is_binding ( dm, pat) ,
141- PatKind :: Wild => true ,
142- _ => false
143- }
144- }
145-
146- /// Call `it` on every "binding" in a pattern, e.g., on `a` in
124+ /// Call `f` on every "binding" in a pattern, e.g., on `a` in
147125/// `match foo() { Some(a) => (), None => () }`
148- pub fn pat_bindings < I > ( dm : & RefCell < DefMap > , pat : & hir:: Pat , mut it : I ) where
149- I : FnMut ( hir:: BindingMode , ast:: NodeId , Span , & Spanned < ast:: Name > ) ,
126+ pub fn pat_bindings < F > ( pat : & hir:: Pat , mut f : F )
127+ where F : FnMut ( hir:: BindingMode , ast:: NodeId , Span , & Spanned < ast:: Name > ) ,
150128{
151129 pat. walk ( |p| {
152- match p. node {
153- PatKind :: Ident ( binding_mode, ref pth, _) if pat_is_binding ( & dm. borrow ( ) , p) => {
154- it ( binding_mode, p. id , p. span , & respan ( pth. span , pth. node ) ) ;
155- }
156- _ => { }
130+ if let PatKind :: Binding ( binding_mode, ref pth, _) = p. node {
131+ f ( binding_mode, p. id , p. span , pth) ;
157132 }
158133 true
159134 } ) ;
160135}
161136
162137/// Checks if the pattern contains any patterns that bind something to
163138/// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
164- pub fn pat_contains_bindings ( dm : & DefMap , pat : & hir:: Pat ) -> bool {
139+ pub fn pat_contains_bindings ( pat : & hir:: Pat ) -> bool {
165140 let mut contains_bindings = false ;
166141 pat. walk ( |p| {
167- if pat_is_binding ( dm , p ) {
142+ if let PatKind :: Binding ( .. ) = p . node {
168143 contains_bindings = true ;
169144 false // there's at least one binding, can short circuit now.
170145 } else {
@@ -176,28 +151,25 @@ pub fn pat_contains_bindings(dm: &DefMap, pat: &hir::Pat) -> bool {
176151
177152/// Checks if the pattern contains any `ref` or `ref mut` bindings,
178153/// and if yes whether its containing mutable ones or just immutables ones.
179- pub fn pat_contains_ref_binding ( dm : & RefCell < DefMap > , pat : & hir:: Pat ) -> Option < hir:: Mutability > {
154+ pub fn pat_contains_ref_binding ( pat : & hir:: Pat ) -> Option < hir:: Mutability > {
180155 let mut result = None ;
181- pat_bindings ( dm, pat, |mode, _, _, _| {
182- match mode {
183- hir:: BindingMode :: BindByRef ( m) => {
184- // Pick Mutable as maximum
185- match result {
186- None | Some ( hir:: MutImmutable ) => result = Some ( m) ,
187- _ => ( ) ,
188- }
156+ pat_bindings ( pat, |mode, _, _, _| {
157+ if let hir:: BindingMode :: BindByRef ( m) = mode {
158+ // Pick Mutable as maximum
159+ match result {
160+ None | Some ( hir:: MutImmutable ) => result = Some ( m) ,
161+ _ => ( ) ,
189162 }
190- hir:: BindingMode :: BindByValue ( _) => { }
191163 }
192164 } ) ;
193165 result
194166}
195167
196168/// Checks if the patterns for this arm contain any `ref` or `ref mut`
197169/// bindings, and if yes whether its containing mutable ones or just immutables ones.
198- pub fn arm_contains_ref_binding ( dm : & RefCell < DefMap > , arm : & hir:: Arm ) -> Option < hir:: Mutability > {
170+ pub fn arm_contains_ref_binding ( arm : & hir:: Arm ) -> Option < hir:: Mutability > {
199171 arm. pats . iter ( )
200- . filter_map ( |pat| pat_contains_ref_binding ( dm , pat) )
172+ . filter_map ( |pat| pat_contains_ref_binding ( pat) )
201173 . max_by_key ( |m| match * m {
202174 hir:: MutMutable => 1 ,
203175 hir:: MutImmutable => 0 ,
@@ -206,22 +178,23 @@ pub fn arm_contains_ref_binding(dm: &RefCell<DefMap>, arm: &hir::Arm) -> Option<
206178
207179/// Checks if the pattern contains any patterns that bind something to
208180/// an ident or wildcard, e.g. `foo`, or `Foo(_)`, `foo @ Bar(..)`,
209- pub fn pat_contains_bindings_or_wild ( dm : & DefMap , pat : & hir:: Pat ) -> bool {
181+ pub fn pat_contains_bindings_or_wild ( pat : & hir:: Pat ) -> bool {
210182 let mut contains_bindings = false ;
211183 pat. walk ( |p| {
212- if pat_is_binding_or_wild ( dm, p) {
213- contains_bindings = true ;
214- false // there's at least one binding/wildcard, can short circuit now.
215- } else {
216- true
184+ match p. node {
185+ PatKind :: Binding ( ..) | PatKind :: Wild => {
186+ contains_bindings = true ;
187+ false // there's at least one binding/wildcard, can short circuit now.
188+ }
189+ _ => true
217190 }
218191 } ) ;
219192 contains_bindings
220193}
221194
222195pub fn simple_name < ' a > ( pat : & ' a hir:: Pat ) -> Option < ast:: Name > {
223196 match pat. node {
224- PatKind :: Ident ( hir:: BindByValue ( _ ) , ref path1, None ) => {
197+ PatKind :: Binding ( hir:: BindByValue ( .. ) , ref path1, None ) => {
225198 Some ( path1. node )
226199 }
227200 _ => {
@@ -241,7 +214,6 @@ pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
241214 match p. node {
242215 PatKind :: TupleStruct ( ..) |
243216 PatKind :: Path ( ..) |
244- PatKind :: Ident ( _, _, None ) |
245217 PatKind :: Struct ( ..) => {
246218 match dm. get ( & p. id ) {
247219 Some ( & PathResolution { base_def : Def :: Variant ( _, id) , .. } ) => {
0 commit comments