Skip to content

Commit 111caef

Browse files
committed
Clean up the scopes of expanded #[macro_use] imports.
1 parent 31e0e12 commit 111caef

File tree

4 files changed

+31
-32
lines changed

4 files changed

+31
-32
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! Here we build the "reduced graph": the graph of the module tree without
1414
//! any imports resolved.
1515
16-
use macros::{InvocationData, LegacyImports, LegacyScope};
16+
use macros::{InvocationData, LegacyScope};
1717
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
1818
use {Module, ModuleS, ModuleKind};
1919
use Namespace::{self, TypeNS, ValueNS};
@@ -84,7 +84,7 @@ impl<'b> Resolver<'b> {
8484
}
8585

8686
/// Constructs the reduced graph for one item.
87-
fn build_reduced_graph_for_item(&mut self, item: &Item, legacy_imports: &mut LegacyImports) {
87+
fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
8888
let parent = self.current_module;
8989
let name = item.ident.name;
9090
let sp = item.span;
@@ -202,7 +202,14 @@ impl<'b> Resolver<'b> {
202202
if def.use_locally {
203203
let ext =
204204
Rc::new(macro_rules::compile(&self.session.parse_sess, &def));
205-
legacy_imports.insert(name, (ext, loaded_macro.import_site));
205+
if self.builtin_macros.insert(name, ext).is_some() &&
206+
expansion != Mark::root() {
207+
let msg = format!("`{}` is already in scope", name);
208+
self.session.struct_span_err(loaded_macro.import_site, &msg)
209+
.note("macro-expanded `#[macro_use]`s may not shadow \
210+
existing macros (see RFC 1560)")
211+
.emit();
212+
}
206213
self.macro_names.insert(name);
207214
}
208215
if def.export {
@@ -513,7 +520,7 @@ impl<'b> Resolver<'b> {
513520
pub struct BuildReducedGraphVisitor<'a, 'b: 'a> {
514521
pub resolver: &'a mut Resolver<'b>,
515522
pub legacy_scope: LegacyScope<'b>,
516-
pub legacy_imports: LegacyImports,
523+
pub expansion: Mark,
517524
}
518525

519526
impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
@@ -554,7 +561,7 @@ impl<'a, 'b> Visitor for BuildReducedGraphVisitor<'a, 'b> {
554561
};
555562

556563
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
557-
self.resolver.build_reduced_graph_for_item(item, &mut self.legacy_imports);
564+
self.resolver.build_reduced_graph_for_item(item, self.expansion);
558565
visit::walk_item(self, item);
559566
self.resolver.current_module = parent;
560567
if !macro_use {

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,8 +3389,8 @@ impl<'a> Resolver<'a> {
33893389
reported_errors.insert((name, span)) {
33903390
let msg = format!("`{}` is already in scope", name);
33913391
self.session.struct_span_err(span, &msg)
3392-
.note("macro-expanded `macro_rules!`s and `#[macro_use]`s \
3393-
may not shadow existing macros (see RFC 1560)")
3392+
.note("macro-expanded `macro_rules!`s may not shadow \
3393+
existing macros (see RFC 1560)")
33943394
.emit();
33953395
}
33963396
}

src/librustc_resolve/macros.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,10 @@ impl<'a> base::Resolver for Resolver<'a> {
111111
let mut visitor = BuildReducedGraphVisitor {
112112
resolver: self,
113113
legacy_scope: LegacyScope::Invocation(invocation),
114-
legacy_imports: FnvHashMap(),
114+
expansion: mark,
115115
};
116116
expansion.visit_with(&mut visitor);
117117
invocation.expansion.set(visitor.legacy_scope);
118-
119-
if !visitor.legacy_imports.is_empty() {
120-
invocation.legacy_scope.set({
121-
LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
122-
parent: invocation.legacy_scope.get(),
123-
kind: LegacyBindingKind::MacroUse(visitor.legacy_imports),
124-
}))
125-
});
126-
}
127118
}
128119

129120
fn add_macro(&mut self, scope: Mark, mut def: ast::MacroDef) {
@@ -173,7 +164,7 @@ impl<'a> base::Resolver for Resolver<'a> {
173164
None
174165
}
175166

176-
fn resolve_invoc(&mut self, scope: Mark, invoc: &Invocation, _force: bool)
167+
fn resolve_invoc(&mut self, scope: Mark, invoc: &Invocation, force: bool)
177168
-> Result<Rc<SyntaxExtension>, Determinacy> {
178169
let (name, span) = match invoc.kind {
179170
InvocationKind::Bang { ref mac, .. } => {
@@ -194,11 +185,15 @@ impl<'a> base::Resolver for Resolver<'a> {
194185
invocation.legacy_scope.set(LegacyScope::simplify_expansion(parent));
195186
}
196187
self.resolve_macro_name(invocation.legacy_scope.get(), name, true).ok_or_else(|| {
197-
let mut err =
198-
self.session.struct_span_err(span, &format!("macro undefined: '{}!'", name));
199-
self.suggest_macro_name(&name.as_str(), &mut err);
200-
err.emit();
201-
Determinacy::Determined
188+
if force {
189+
let mut err =
190+
self.session.struct_span_err(span, &format!("macro undefined: '{}!'", name));
191+
self.suggest_macro_name(&name.as_str(), &mut err);
192+
err.emit();
193+
Determinacy::Determined
194+
} else {
195+
Determinacy::Undetermined
196+
}
202197
})
203198
}
204199

src/test/compile-fail/macro-shadowing.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,28 @@
1212

1313
macro_rules! foo { () => {} }
1414
macro_rules! macro_one { () => {} }
15+
#[macro_use(macro_two)] extern crate two_macros;
1516

1617
macro_rules! m1 { () => {
1718
macro_rules! foo { () => {} } //~ ERROR `foo` is already in scope
18-
//~^ NOTE macro-expanded `macro_rules!`s and `#[macro_use]`s may not shadow existing macros
19+
//~^ NOTE macro-expanded `macro_rules!`s may not shadow existing macros
1920

20-
#[macro_use] //~ ERROR `macro_one` is already in scope
21-
//~^ NOTE macro-expanded `macro_rules!`s and `#[macro_use]`s may not shadow existing macros
22-
extern crate two_macros;
21+
#[macro_use] //~ ERROR `macro_two` is already in scope
22+
//~^ NOTE macro-expanded `#[macro_use]`s may not shadow existing macros
23+
extern crate two_macros as __;
2324
}}
2425
m1!(); //~ NOTE in this expansion
2526
//~| NOTE in this expansion
2627
//~| NOTE in this expansion
2728
//~| NOTE in this expansion
2829

29-
fn f() { macro_one!(); }
3030
foo!();
3131

3232
macro_rules! m2 { () => {
3333
macro_rules! foo { () => {} }
34-
#[macro_use] extern crate two_macros as __;
35-
36-
fn g() { macro_one!(); }
3734
foo!();
3835
}}
3936
m2!();
40-
//^ Since `foo` and `macro_one` are not used outside this expansion, they are not shadowing errors.
37+
//^ Since `foo` is not used outside this expansion, it is not a shadowing error.
4138

4239
fn main() {}

0 commit comments

Comments
 (0)