From cfbb60bf6d83fbcfcca1f2919131aa39fb997b53 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 12 Aug 2019 21:55:42 +0300 Subject: [PATCH] resolve: Do not "normalize away" trait/enum modules prematurely The previous approach was brittle - what would happen if `ParentScope` wasn't created by `invoc_parent_scope`? That's exactly the case for various uses of `ParentScope` in diagnostics and in built-in attribute validation. --- src/librustc_resolve/lib.rs | 8 +++++--- src/librustc_resolve/macros.rs | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 3d82ef3d5801..6bb2cd037708 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -1321,13 +1321,15 @@ impl<'a> Resolver<'a> { ScopeSet::AbsolutePath(ns) => (ns, true), ScopeSet::Macro(_) => (MacroNS, false), }; + // Jump out of trait or enum modules, they do not act as scopes. + let module = parent_scope.module.nearest_item_scope(); let mut scope = match ns { _ if is_absolute_path => Scope::CrateRoot, - TypeNS | ValueNS => Scope::Module(parent_scope.module), + TypeNS | ValueNS => Scope::Module(module), MacroNS => Scope::DeriveHelpers, }; let mut ident = ident.modern(); - let mut use_prelude = !parent_scope.module.no_implicit_prelude; + let mut use_prelude = !module.no_implicit_prelude; loop { let visit = match scope { @@ -1360,7 +1362,7 @@ impl<'a> Resolver<'a> { LegacyScope::Invocation(invoc) => Scope::MacroRules( invoc.output_legacy_scope.get().unwrap_or(invoc.parent_legacy_scope) ), - LegacyScope::Empty => Scope::Module(parent_scope.module), + LegacyScope::Empty => Scope::Module(module), } Scope::CrateRoot => match ns { TypeNS => { diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index f54f395063c2..6d882a6e194d 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -258,7 +258,7 @@ impl<'a> Resolver<'a> { fn invoc_parent_scope(&self, invoc_id: ExpnId, derives: Vec) -> ParentScope<'a> { let invoc = self.invocations[&invoc_id]; ParentScope { - module: invoc.module.nearest_item_scope(), + module: invoc.module, expansion: invoc_id.parent(), legacy: invoc.parent_legacy_scope, derives,