Skip to content

Commit 77e1a7c

Browse files
authored
Rollup merge of #64177 - petrochenkov:curmod, r=matthewjasper
resolve: Do not afraid to set current module to enums and traits After cfbb60b it's ok. This is likely required for #63468 to work correctly, because that PR starts resolving attributes on enum variants. r? @matthewjasper @c410-f3r
2 parents 4ea7797 + 56f6353 commit 77e1a7c

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

Diff for: src/librustc_resolve/build_reduced_graph.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
728728
expansion,
729729
item.span);
730730
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
731+
self.parent_scope.module = module;
731732

732733
for variant in &(*enum_definition).variants {
733-
self.build_reduced_graph_for_variant(variant, module, vis);
734+
self.build_reduced_graph_for_variant(variant, vis);
734735
}
735736
}
736737

@@ -818,10 +819,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
818819

819820
// Constructs the reduced graph for one variant. Variants exist in the
820821
// type and value namespaces.
821-
fn build_reduced_graph_for_variant(&mut self,
822-
variant: &Variant,
823-
parent: Module<'a>,
824-
vis: ty::Visibility) {
822+
fn build_reduced_graph_for_variant(&mut self, variant: &Variant, vis: ty::Visibility) {
823+
let parent = self.parent_scope.module;
825824
let expn_id = self.parent_scope.expansion;
826825
let ident = variant.ident;
827826

@@ -1253,9 +1252,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
12531252
let expansion = self.parent_scope.expansion;
12541253
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
12551254

1256-
self.parent_scope.module = parent.parent.unwrap(); // nearest normal ancestor
12571255
visit::walk_trait_item(self, item);
1258-
self.parent_scope.module = parent;
12591256
}
12601257

12611258
fn visit_token(&mut self, t: Token) {

Diff for: src/librustc_resolve/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,11 @@ impl<'a> ModuleData<'a> {
535535
}
536536

537537
fn nearest_item_scope(&'a self) -> Module<'a> {
538-
if self.is_trait() { self.parent.unwrap() } else { self }
538+
match self.kind {
539+
ModuleKind::Def(DefKind::Enum, ..) | ModuleKind::Def(DefKind::Trait, ..) =>
540+
self.parent.expect("enum or trait module without a parent"),
541+
_ => self,
542+
}
539543
}
540544

541545
fn is_ancestor_of(&self, mut other: &Self) -> bool {
@@ -1637,7 +1641,7 @@ impl<'a> Resolver<'a> {
16371641
}
16381642

16391643
if let ModuleKind::Block(..) = module.kind {
1640-
return Some(module.parent.unwrap());
1644+
return Some(module.parent.unwrap().nearest_item_scope());
16411645
}
16421646

16431647
None

Diff for: src/test/ui/resolve/block-with-trait-parent.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
3+
trait Trait {
4+
fn method(&self) {
5+
// Items inside a block turn it into a module internally.
6+
struct S;
7+
impl Trait for S {}
8+
9+
// OK, `Trait` is in scope here from method resolution point of view.
10+
S.method();
11+
}
12+
}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)