]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_resolve/late.rs
Rename `ForeignItem.node` to `ForeignItem.kind`
[rust.git] / src / librustc_resolve / late.rs
index ac5ae327e5ba1a14390563ff7f801c733c33941b..33a85c6c77026bd786cee1b857ca2620052e9d2b 100644 (file)
@@ -75,6 +75,16 @@ fn descr(self) -> &'static str {
     }
 }
 
+/// Denotes whether the context for the set of already bound bindings is a `Product`
+/// or `Or` context. This is used in e.g., `fresh_binding` and `resolve_pattern_inner`.
+/// See those functions for more information.
+enum PatBoundCtx {
+    /// A product pattern context, e.g., `Variant(a, b)`.
+    Product,
+    /// An or-pattern context, e.g., `p_0 | ... | p_n`.
+    Or,
+}
+
 /// The rib kind restricts certain accesses,
 /// e.g. to a `Res::Local` of an outer item.
 #[derive(Copy, Clone, Debug)]
@@ -211,7 +221,7 @@ fn descr_expected(self) -> &'static str {
                 ValueNS => "method or associated constant",
                 MacroNS => bug!("associated macro"),
             },
-            PathSource::Expr(parent) => match parent.map(|p| &p.node) {
+            PathSource::Expr(parent) => match parent.map(|p| &p.kind) {
                 // "function" here means "anything callable" rather than `DefKind::Fn`,
                 // this is not precise but usually more helpful than just "value".
                 Some(&ExprKind::Call(..)) => "function",
@@ -288,18 +298,18 @@ fn descr_expected(self) -> &'static str {
     }
 
     fn error_code(self, has_unexpected_resolution: bool) -> &'static str {
-        __diagnostic_used!(E0404);
-        __diagnostic_used!(E0405);
-        __diagnostic_used!(E0412);
-        __diagnostic_used!(E0422);
-        __diagnostic_used!(E0423);
-        __diagnostic_used!(E0425);
-        __diagnostic_used!(E0531);
-        __diagnostic_used!(E0532);
-        __diagnostic_used!(E0573);
-        __diagnostic_used!(E0574);
-        __diagnostic_used!(E0575);
-        __diagnostic_used!(E0576);
+        syntax::diagnostic_used!(E0404);
+        syntax::diagnostic_used!(E0405);
+        syntax::diagnostic_used!(E0412);
+        syntax::diagnostic_used!(E0422);
+        syntax::diagnostic_used!(E0423);
+        syntax::diagnostic_used!(E0425);
+        syntax::diagnostic_used!(E0531);
+        syntax::diagnostic_used!(E0532);
+        syntax::diagnostic_used!(E0573);
+        syntax::diagnostic_used!(E0574);
+        syntax::diagnostic_used!(E0575);
+        syntax::diagnostic_used!(E0576);
         match (self, has_unexpected_resolution) {
             (PathSource::Trait(_), true) => "E0404",
             (PathSource::Trait(_), false) => "E0405",
@@ -374,7 +384,7 @@ fn visit_local(&mut self, local: &'tcx Local) {
         self.resolve_local(local);
     }
     fn visit_ty(&mut self, ty: &'tcx Ty) {
-        match ty.node {
+        match ty.kind {
             TyKind::Path(ref qself, ref path) => {
                 self.smart_resolve_path(ty.id, qself.as_ref(), path, PathSource::Type);
             }
@@ -396,7 +406,7 @@ fn visit_poly_trait_ref(&mut self,
         visit::walk_poly_trait_ref(self, tref, m);
     }
     fn visit_foreign_item(&mut self, foreign_item: &'tcx ForeignItem) {
-        let generic_params = match foreign_item.node {
+        let generic_params = match foreign_item.kind {
             ForeignItemKind::Fn(_, ref generics) => {
                 HasGenericParams(generics, ItemRibKind)
             }
@@ -690,9 +700,9 @@ fn future_proof_import(&mut self, use_tree: &UseTree) {
 
     fn resolve_item(&mut self, item: &Item) {
         let name = item.ident.name;
-        debug!("(resolving item) resolving {} ({:?})", name, item.node);
+        debug!("(resolving item) resolving {} ({:?})", name, item.kind);
 
-        match item.node {
+        match item.kind {
             ItemKind::TyAlias(_, ref generics) |
             ItemKind::OpaqueTy(_, ref generics) |
             ItemKind::Fn(_, _, ref generics, _) => {
@@ -730,7 +740,7 @@ fn resolve_item(&mut self, item: &Item) {
                                     AssocItemRibKind,
                                 );
                                 this.with_generic_param_rib(generic_params, |this| {
-                                    match trait_item.node {
+                                    match trait_item.kind {
                                         TraitItemKind::Const(ref ty, ref default) => {
                                             this.visit_ty(ty);
 
@@ -928,7 +938,7 @@ fn with_trait_items<T>(
     ) -> T {
         let trait_assoc_types = replace(
             &mut self.current_trait_assoc_types,
-            trait_items.iter().filter_map(|item| match &item.node {
+            trait_items.iter().filter_map(|item| match &item.kind {
                 TraitItemKind::Type(bounds, _) if bounds.len() == 0 => Some(item.ident),
                 _ => None,
             }).collect(),
@@ -1025,7 +1035,7 @@ fn resolve_implementation(&mut self,
                                                                           AssocItemRibKind);
                                     this.with_generic_param_rib(generic_params, |this| {
                                         use crate::ResolutionError::*;
-                                        match impl_item.node {
+                                        match impl_item.kind {
                                             ImplItemKind::Const(..) => {
                                                 debug!(
                                                     "resolve_implementation ImplItemKind::Const",
@@ -1109,10 +1119,9 @@ fn check_trait_item<F>(&mut self, ident: Ident, ns: Namespace, span: Span, err:
     }
 
     fn resolve_params(&mut self, params: &[Param]) {
-        let mut bindings = smallvec![(false, <_>::default())];
+        let mut bindings = smallvec![(PatBoundCtx::Product, Default::default())];
         for Param { pat, ty, .. } in params {
             self.resolve_pattern(pat, PatternSource::FnParam, &mut bindings);
-            self.check_consistent_bindings_top(pat);
             self.visit_ty(ty);
             debug!("(resolving function / closure) recorded parameter");
         }
@@ -1137,7 +1146,7 @@ fn binding_mode_map(&mut self, pat: &Pat) -> BindingMap {
         let mut binding_map = FxHashMap::default();
 
         pat.walk(&mut |pat| {
-            match pat.node {
+            match pat.kind {
                 PatKind::Ident(binding_mode, ident, ref sub_pat)
                     if sub_pat.is_some() || self.is_base_res_local(pat.id) =>
                 {
@@ -1237,7 +1246,7 @@ fn check_consistent_bindings(&mut self, pats: &[P<Pat>]) -> Vec<BindingMap> {
 
     /// Check the consistency of the outermost or-patterns.
     fn check_consistent_bindings_top(&mut self, pat: &Pat) {
-        pat.walk(&mut |pat| match pat.node {
+        pat.walk(&mut |pat| match pat.kind {
             PatKind::Or(ref ps) => {
                 self.check_consistent_bindings(ps);
                 false
@@ -1248,51 +1257,59 @@ fn check_consistent_bindings_top(&mut self, pat: &Pat) {
 
     fn resolve_arm(&mut self, arm: &Arm) {
         self.with_rib(ValueNS, NormalRibKind, |this| {
-            this.resolve_pats(&arm.pats, PatternSource::Match);
+            this.resolve_pattern_top(&arm.pat, PatternSource::Match);
             walk_list!(this, visit_expr, &arm.guard);
             this.visit_expr(&arm.body);
         });
     }
 
-    /// Arising from `source`, resolve a sequence of patterns (top level or-patterns).
-    fn resolve_pats(&mut self, pats: &[P<Pat>], source: PatternSource) {
-        let mut bindings = smallvec![(true, <_>::default())];
-        for pat in pats {
-            bindings.push((false, <_>::default()));
-            self.resolve_pattern(pat, source, &mut bindings);
-            let collected = bindings.pop().unwrap().1;
-            bindings.last_mut().unwrap().1.extend(collected);
-        }
-        // This has to happen *after* we determine which pat_idents are variants
-        self.check_consistent_bindings(pats);
-    }
-
+    /// Arising from `source`, resolve a top level pattern.
     fn resolve_pattern_top(&mut self, pat: &Pat, pat_src: PatternSource) {
-        self.resolve_pattern(pat, pat_src, &mut smallvec![(false, <_>::default())]);
-        // This has to happen *after* we determine which pat_idents are variants:
-        self.check_consistent_bindings_top(pat);
+        let mut bindings = smallvec![(PatBoundCtx::Product, Default::default())];
+        self.resolve_pattern(pat, pat_src, &mut bindings);
     }
 
     fn resolve_pattern(
         &mut self,
         pat: &Pat,
         pat_src: PatternSource,
-        bindings: &mut SmallVec<[(bool, FxHashSet<Ident>); 1]>,
+        bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
     ) {
         self.resolve_pattern_inner(pat, pat_src, bindings);
+        // This has to happen *after* we determine which pat_idents are variants:
+        self.check_consistent_bindings_top(pat);
         visit::walk_pat(self, pat);
     }
 
+    /// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.
+    ///
+    /// ### `bindings`
+    ///
+    /// A stack of sets of bindings accumulated.
+    ///
+    /// In each set, `PatBoundCtx::Product` denotes that a found binding in it should
+    /// be interpreted as re-binding an already bound binding. This results in an error.
+    /// Meanwhile, `PatBound::Or` denotes that a found binding in the set should result
+    /// in reusing this binding rather than creating a fresh one.
+    ///
+    /// When called at the top level, the stack must have a single element
+    /// with `PatBound::Product`. Otherwise, pushing to the stack happens as
+    /// or-patterns (`p_0 | ... | p_n`) are encountered and the context needs
+    /// to be switched to `PatBoundCtx::Or` and then `PatBoundCtx::Product` for each `p_i`.
+    /// When each `p_i` has been dealt with, the top set is merged with its parent.
+    /// When a whole or-pattern has been dealt with, the thing happens.
+    ///
+    /// See the implementation and `fresh_binding` for more details.
     fn resolve_pattern_inner(
         &mut self,
         pat: &Pat,
         pat_src: PatternSource,
-        bindings: &mut SmallVec<[(bool, FxHashSet<Ident>); 1]>,
+        bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
     ) {
         // Visit all direct subpatterns of this pattern.
         pat.walk(&mut |pat| {
-            debug!("resolve_pattern pat={:?} node={:?}", pat, pat.node);
-            match pat.node {
+            debug!("resolve_pattern pat={:?} node={:?}", pat, pat.kind);
+            match pat.kind {
                 PatKind::Ident(bmode, ident, ref sub) => {
                     // First try to resolve the identifier as some existing entity,
                     // then fall back to a fresh binding.
@@ -1311,15 +1328,15 @@ fn resolve_pattern_inner(
                     self.smart_resolve_path(pat.id, None, path, PathSource::Struct);
                 }
                 PatKind::Or(ref ps) => {
-                    // Add a new set of bindings to the stack. `true` here records that when a
+                    // Add a new set of bindings to the stack. `Or` here records that when a
                     // binding already exists in this set, it should not result in an error because
                     // `V1(a) | V2(a)` must be allowed and are checked for consistency later.
-                    bindings.push((true, <_>::default()));
+                    bindings.push((PatBoundCtx::Or, Default::default()));
                     for p in ps {
                         // Now we need to switch back to a product context so that each
                         // part of the or-pattern internally rejects already bound names.
                         // For example, `V1(a) | V2(a, a)` and `V1(a, a) | V2(a)` are bad.
-                        bindings.push((false, <_>::default()));
+                        bindings.push((PatBoundCtx::Product, Default::default()));
                         self.resolve_pattern_inner(p, pat_src, bindings);
                         // Move up the non-overlapping bindings to the or-pattern.
                         // Existing bindings just get "merged".
@@ -1346,7 +1363,7 @@ fn fresh_binding(
         ident: Ident,
         pat_id: NodeId,
         pat_src: PatternSource,
-        bindings: &mut SmallVec<[(bool, FxHashSet<Ident>); 1]>,
+        bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
     ) -> 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
@@ -1360,10 +1377,10 @@ fn fresh_binding(
         for (is_sum, set) in bindings.iter_mut().rev() {
             match (is_sum, set.get(&ident).cloned()) {
                 // Already bound in a product pattern, e.g. `(a, a)` which is not allowed.
-                (false, Some(..)) => already_bound_and = true,
+                (PatBoundCtx::Product, Some(..)) => already_bound_and = true,
                 // Already bound in an or-pattern, e.g. `V1(a) | V2(a)`.
                 // This is *required* for consistency which is checked later.
-                (true, Some(..)) => already_bound_or = true,
+                (PatBoundCtx::Or, Some(..)) => already_bound_or = true,
                 // Not already bound here.
                 _ => {}
             }
@@ -1787,8 +1804,8 @@ fn resolve_block(&mut self, block: &Block) {
 
         // Descend into the block.
         for stmt in &block.stmts {
-            if let StmtKind::Item(ref item) = stmt.node {
-                if let ItemKind::MacroDef(..) = item.node {
+            if let StmtKind::Item(ref item) = stmt.kind {
+                if let ItemKind::MacroDef(..) = item.kind {
                     num_macro_definition_ribs += 1;
                     let res = self.r.definitions.local_def_id(item.id);
                     self.ribs[ValueNS].push(Rib::new(MacroDefinition(res)));
@@ -1819,7 +1836,7 @@ fn resolve_expr(&mut self, expr: &Expr, parent: Option<&Expr>) {
         self.record_candidate_traits_for_expr_if_necessary(expr);
 
         // Next, resolve the node.
-        match expr.node {
+        match expr.kind {
             ExprKind::Path(ref qself, ref path) => {
                 self.smart_resolve_path(expr.id, qself.as_ref(), path, PathSource::Expr(parent));
                 visit::walk_expr(self, expr);
@@ -1866,9 +1883,9 @@ fn resolve_expr(&mut self, expr: &Expr, parent: Option<&Expr>) {
                 visit::walk_expr(self, expr);
             }
 
-            ExprKind::Let(ref pats, ref scrutinee) => {
+            ExprKind::Let(ref pat, ref scrutinee) => {
                 self.visit_expr(scrutinee);
-                self.resolve_pats(pats, PatternSource::Let);
+                self.resolve_pattern_top(pat, PatternSource::Let);
             }
 
             ExprKind::If(ref cond, ref then, ref opt_else) => {
@@ -1951,7 +1968,7 @@ fn resolve_expr(&mut self, expr: &Expr, parent: Option<&Expr>) {
     }
 
     fn record_candidate_traits_for_expr_if_necessary(&mut self, expr: &Expr) {
-        match expr.node {
+        match expr.kind {
             ExprKind::Field(_, ident) => {
                 // FIXME(#6890): Even though you can't treat a method like a
                 // field, we need to add any trait methods we find that match