about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-09-08 00:07:33 +0200
committerGitHub <noreply@github.com>2019-09-08 00:07:33 +0200
commit77e1a7c578f61572bbb4e40c5a12e2d0a74329b8 (patch)
tree565995d5d63496fb383886dcf4995b0a9c96c405 /src
parent4ea77975ab2d9fd31309d8c11013d553d22745d6 (diff)
parent56f635304b7a2689cfe5e98577428d67f059b413 (diff)
downloadrust-77e1a7c578f61572bbb4e40c5a12e2d0a74329b8.tar.gz
rust-77e1a7c578f61572bbb4e40c5a12e2d0a74329b8.zip
Rollup merge of #64177 - petrochenkov:curmod, r=matthewjasper
resolve: Do not afraid to set current module to enums and traits

After https://github.com/rust-lang/rust/pull/63535/commits/cfbb60bf6d83fbcfcca1f2919131aa39fb997b53 it's ok.

This is likely required for https://github.com/rust-lang/rust/pull/63468 to work correctly, because that PR starts resolving attributes on enum variants.

r? @matthewjasper @c410-f3r
Diffstat (limited to 'src')
-rw-r--r--src/librustc_resolve/build_reduced_graph.rs11
-rw-r--r--src/librustc_resolve/lib.rs8
-rw-r--r--src/test/ui/resolve/block-with-trait-parent.rs14
3 files changed, 24 insertions, 9 deletions
diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs
index 81bd687e263..333509e1850 100644
--- a/src/librustc_resolve/build_reduced_graph.rs
+++ b/src/librustc_resolve/build_reduced_graph.rs
@@ -728,9 +728,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
                                              expansion,
                                              item.span);
                 self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
+                self.parent_scope.module = module;
 
                 for variant in &(*enum_definition).variants {
-                    self.build_reduced_graph_for_variant(variant, module, vis);
+                    self.build_reduced_graph_for_variant(variant, vis);
                 }
             }
 
@@ -818,10 +819,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
 
     // Constructs the reduced graph for one variant. Variants exist in the
     // type and value namespaces.
-    fn build_reduced_graph_for_variant(&mut self,
-                                       variant: &Variant,
-                                       parent: Module<'a>,
-                                       vis: ty::Visibility) {
+    fn build_reduced_graph_for_variant(&mut self, variant: &Variant, vis: ty::Visibility) {
+        let parent = self.parent_scope.module;
         let expn_id = self.parent_scope.expansion;
         let ident = variant.ident;
 
@@ -1253,9 +1252,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
         let expansion = self.parent_scope.expansion;
         self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
 
-        self.parent_scope.module = parent.parent.unwrap(); // nearest normal ancestor
         visit::walk_trait_item(self, item);
-        self.parent_scope.module = parent;
     }
 
     fn visit_token(&mut self, t: Token) {
diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs
index e980b8d01f7..6e131c04722 100644
--- a/src/librustc_resolve/lib.rs
+++ b/src/librustc_resolve/lib.rs
@@ -535,7 +535,11 @@ impl<'a> ModuleData<'a> {
     }
 
     fn nearest_item_scope(&'a self) -> Module<'a> {
-        if self.is_trait() { self.parent.unwrap() } else { self }
+        match self.kind {
+            ModuleKind::Def(DefKind::Enum, ..) | ModuleKind::Def(DefKind::Trait, ..) =>
+                self.parent.expect("enum or trait module without a parent"),
+            _ => self,
+        }
     }
 
     fn is_ancestor_of(&self, mut other: &Self) -> bool {
@@ -1637,7 +1641,7 @@ impl<'a> Resolver<'a> {
         }
 
         if let ModuleKind::Block(..) = module.kind {
-            return Some(module.parent.unwrap());
+            return Some(module.parent.unwrap().nearest_item_scope());
         }
 
         None
diff --git a/src/test/ui/resolve/block-with-trait-parent.rs b/src/test/ui/resolve/block-with-trait-parent.rs
new file mode 100644
index 00000000000..bc86f94e921
--- /dev/null
+++ b/src/test/ui/resolve/block-with-trait-parent.rs
@@ -0,0 +1,14 @@
+// check-pass
+
+trait Trait {
+    fn method(&self) {
+        // Items inside a block turn it into a module internally.
+        struct S;
+        impl Trait for S {}
+
+        // OK, `Trait` is in scope here from method resolution point of view.
+        S.method();
+    }
+}
+
+fn main() {}