Skip to content

Commit 6808b0a

Browse files
committed
Check for shadowing errors after all invocations have been expanded.
1 parent 53fd3b0 commit 6808b0a

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/librustc_resolve/lib.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use std::mem::replace;
7878
use std::rc::Rc;
7979

8080
use resolve_imports::{ImportDirective, NameResolution};
81-
use macros::{InvocationData, LegacyBinding};
81+
use macros::{InvocationData, LegacyBinding, LegacyScope};
8282

8383
// NB: This module needs to be declared first so diagnostics are
8484
// registered before they are used.
@@ -1073,7 +1073,7 @@ pub struct Resolver<'a> {
10731073

10741074
privacy_errors: Vec<PrivacyError<'a>>,
10751075
ambiguity_errors: Vec<AmbiguityError<'a>>,
1076-
macro_shadowing_errors: FnvHashSet<Span>,
1076+
disallowed_shadowing: Vec<(Name, Span, LegacyScope<'a>)>,
10771077

10781078
arenas: &'a ResolverArenas<'a>,
10791079
dummy_binding: &'a NameBinding<'a>,
@@ -1260,7 +1260,7 @@ impl<'a> Resolver<'a> {
12601260

12611261
privacy_errors: Vec::new(),
12621262
ambiguity_errors: Vec::new(),
1263-
macro_shadowing_errors: FnvHashSet(),
1263+
disallowed_shadowing: Vec::new(),
12641264

12651265
arenas: arenas,
12661266
dummy_binding: arenas.alloc_name_binding(NameBinding {
@@ -3353,7 +3353,8 @@ impl<'a> Resolver<'a> {
33533353
vis.is_accessible_from(module.normal_ancestor_id.unwrap(), self)
33543354
}
33553355

3356-
fn report_errors(&self) {
3356+
fn report_errors(&mut self) {
3357+
self.report_shadowing_errors();
33573358
let mut reported_spans = FnvHashSet();
33583359

33593360
for &AmbiguityError { span, name, b1, b2 } in &self.ambiguity_errors {
@@ -3381,6 +3382,20 @@ impl<'a> Resolver<'a> {
33813382
}
33823383
}
33833384

3385+
fn report_shadowing_errors(&mut self) {
3386+
let mut reported_errors = FnvHashSet();
3387+
for (name, span, scope) in replace(&mut self.disallowed_shadowing, Vec::new()) {
3388+
if self.resolve_macro_name(scope, name, false).is_some() &&
3389+
reported_errors.insert((name, span)) {
3390+
let msg = format!("`{}` is already in scope", name);
3391+
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)")
3394+
.emit();
3395+
}
3396+
}
3397+
}
3398+
33843399
fn report_conflict(&self,
33853400
parent: Module,
33863401
name: Name,

src/librustc_resolve/macros.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -207,20 +207,14 @@ impl<'a> base::Resolver for Resolver<'a> {
207207
}
208208

209209
impl<'a> Resolver<'a> {
210-
fn resolve_macro_name(&mut self,
211-
mut scope: LegacyScope<'a>,
212-
name: ast::Name,
213-
record_used: bool)
214-
-> Option<Rc<SyntaxExtension>> {
210+
pub fn resolve_macro_name(&mut self,
211+
mut scope: LegacyScope<'a>,
212+
name: ast::Name,
213+
record_used: bool)
214+
-> Option<Rc<SyntaxExtension>> {
215215
let check_shadowing = |this: &mut Self, relative_depth, scope, span| {
216-
if record_used && relative_depth > 0 &&
217-
this.resolve_macro_name(scope, name, false).is_some() &&
218-
this.macro_shadowing_errors.insert(span) {
219-
let msg = format!("`{}` is already in scope", name);
220-
this.session.struct_span_err(span, &msg)
221-
.note("macro-expanded `macro_rules!`s and `#[macro_use]`s \
222-
may not shadow existing macros (see RFC 1560)")
223-
.emit();
216+
if record_used && relative_depth > 0 {
217+
this.disallowed_shadowing.push((name, span, scope));
224218
}
225219
};
226220

0 commit comments

Comments
 (0)