Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: Do not afraid to set current module to enums and traits #64177

Merged
merged 2 commits into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,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 {
Expand Down Expand Up @@ -1640,7 +1644,7 @@ impl<'a> Resolver<'a> {
}

if let ModuleKind::Block(..) = module.kind {
return Some(module.parent.unwrap());
return Some(module.parent.unwrap().nearest_item_scope());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks like it's not doing anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fixes the CI failure above (minimized in block-with-trait-parent.rs ).
If the block is directly inside a trait item, then its parent is the trait and we need to skip it and jump to the trait's parent module.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, I missed that this was using the parent module.

}

None
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/resolve/block-with-trait-parent.rs
Original file line number Diff line number Diff line change
@@ -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() {}