]> git.lizzy.rs Git - rust.git/commitdiff
resolve: move `fresh_binding`.
authorMazdak Farrokhzad <twingoow@gmail.com>
Wed, 28 Aug 2019 08:27:40 +0000 (10:27 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Thu, 5 Sep 2019 06:33:09 +0000 (08:33 +0200)
src/librustc_resolve/late.rs

index 29264b3eedce1ea45c5d0bac4bed84178213f5a9..1b550bd7b0f5640cb3406bb5d839813682dd897c 100644 (file)
@@ -1272,18 +1272,56 @@ fn resolve_block(&mut self, block: &Block) {
         debug!("(resolving block) leaving block");
     }
 
-    fn fresh_binding(&mut self,
-                     ident: Ident,
-                     pat_id: NodeId,
-                     outer_pat_id: NodeId,
-                     pat_src: PatternSource,
-                     bindings: &mut IdentMap<NodeId>)
-                     -> Res {
-        // Add the binding to the local ribs, if it
-        // doesn't already exist in the bindings map. (We
-        // must not add it if it's in the bindings map
-        // because that breaks the assumptions later
-        // passes make about or-patterns.)
+    fn resolve_pattern(
+        &mut self,
+        pat: &Pat,
+        pat_src: PatternSource,
+        // Maps idents to the node ID for the outermost pattern that binds them.
+        bindings: &mut IdentMap<NodeId>,
+    ) {
+        // Visit all direct subpatterns of this pattern.
+        let outer_pat_id = pat.id;
+        pat.walk(&mut |pat| {
+            debug!("resolve_pattern pat={:?} node={:?}", pat, pat.node);
+            match pat.node {
+                PatKind::Ident(bmode, ident, ref sub) => {
+                    // First try to resolve the identifier as some existing entity,
+                    // then fall back to a fresh binding.
+                    let has_sub = sub.is_some();
+                    let res = self.try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub)
+                        .unwrap_or_else(|| {
+                            self.fresh_binding(ident, pat.id, outer_pat_id, pat_src, bindings)
+                        });
+                    self.r.record_partial_res(pat.id, PartialRes::new(res));
+                }
+                PatKind::TupleStruct(ref path, ..) => {
+                    self.smart_resolve_path(pat.id, None, path, PathSource::TupleStruct);
+                }
+                PatKind::Path(ref qself, ref path) => {
+                    self.smart_resolve_path(pat.id, qself.as_ref(), path, PathSource::Pat);
+                }
+                PatKind::Struct(ref path, ..) => {
+                    self.smart_resolve_path(pat.id, None, path, PathSource::Struct);
+                }
+                _ => {}
+            }
+            true
+        });
+
+        visit::walk_pat(self, pat);
+    }
+
+    fn fresh_binding(
+        &mut self,
+        ident: Ident,
+        pat_id: NodeId,
+        outer_pat_id: NodeId,
+        pat_src: PatternSource,
+        bindings: &mut IdentMap<NodeId>,
+    ) -> Res {
+        // Add the binding to the local ribs, if it doesn't already exist in the bindings map.
+        // (We must not add it if it's in the bindings map because that breaks the assumptions
+        // later passes make about or-patterns.)
         let ident = ident.modern_and_legacy();
         let mut res = Res::Local(pat_id);
         match bindings.get(&ident).cloned() {
@@ -1291,16 +1329,14 @@ fn fresh_binding(&mut self,
                 // `Variant(a, a)`, error
                 self.r.report_error(
                     ident.span,
-                    ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(
-                        &ident.as_str())
+                    ResolutionError::IdentifierBoundMoreThanOnceInSamePattern(&ident.as_str()),
                 );
             }
             Some(..) if pat_src == PatternSource::FnParam => {
                 // `fn f(a: u8, a: u8)`, error
                 self.r.report_error(
                     ident.span,
-                    ResolutionError::IdentifierBoundMoreThanOnceInParameterList(
-                        &ident.as_str())
+                    ResolutionError::IdentifierBoundMoreThanOnceInParameterList(&ident.as_str()),
                 );
             }
             Some(..) if pat_src == PatternSource::Match ||
@@ -1329,45 +1365,6 @@ fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut IdentMap<Res> {
         &mut self.ribs[ns].last_mut().unwrap().bindings
     }
 
-    fn resolve_pattern(
-        &mut self,
-        pat: &Pat,
-        pat_src: PatternSource,
-        // Maps idents to the node ID for the outermost pattern that binds them.
-        bindings: &mut IdentMap<NodeId>,
-    ) {
-        // Visit all direct subpatterns of this pattern.
-        let outer_pat_id = pat.id;
-        pat.walk(&mut |pat| {
-            debug!("resolve_pattern pat={:?} node={:?}", pat, pat.node);
-            match pat.node {
-                PatKind::Ident(bmode, ident, ref sub) => {
-                    // First try to resolve the identifier as some existing entity,
-                    // then fall back to a fresh binding.
-                    let has_sub = sub.is_some();
-                    let res = self.try_resolve_as_non_binding(pat_src, pat, bmode, ident, has_sub)
-                        .unwrap_or_else(|| {
-                            self.fresh_binding(ident, pat.id, outer_pat_id, pat_src, bindings)
-                        });
-                    self.r.record_partial_res(pat.id, PartialRes::new(res));
-                }
-                PatKind::TupleStruct(ref path, ..) => {
-                    self.smart_resolve_path(pat.id, None, path, PathSource::TupleStruct);
-                }
-                PatKind::Path(ref qself, ref path) => {
-                    self.smart_resolve_path(pat.id, qself.as_ref(), path, PathSource::Pat);
-                }
-                PatKind::Struct(ref path, ..) => {
-                    self.smart_resolve_path(pat.id, None, path, PathSource::Struct);
-                }
-                _ => {}
-            }
-            true
-        });
-
-        visit::walk_pat(self, pat);
-    }
-
     fn try_resolve_as_non_binding(
         &mut self,
         pat_src: PatternSource,