Skip to content

Commit 076c5d4

Browse files
committed
Fix shadowing checking.
1 parent 6c4b551 commit 076c5d4

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/librustc_resolve/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ use std::mem::replace;
7777
use std::rc::Rc;
7878

7979
use resolve_imports::{ImportDirective, NameResolution};
80-
use macros::{InvocationData, LegacyBinding};
80+
use macros::{InvocationData, LegacyBinding, LegacyScope};
8181

8282
// NB: This module needs to be declared first so diagnostics are
8383
// registered before they are used.
@@ -1077,6 +1077,7 @@ pub struct Resolver<'a> {
10771077
crate_loader: &'a mut CrateLoader,
10781078
macro_names: FnvHashSet<Name>,
10791079
builtin_macros: FnvHashMap<Name, Rc<SyntaxExtension>>,
1080+
lexical_macro_resolutions: Vec<(Name, LegacyScope<'a>)>,
10801081

10811082
// Maps the `Mark` of an expansion to its containing module or block.
10821083
invocations: FnvHashMap<Mark, &'a InvocationData<'a>>,
@@ -1267,6 +1268,7 @@ impl<'a> Resolver<'a> {
12671268
crate_loader: crate_loader,
12681269
macro_names: FnvHashSet(),
12691270
builtin_macros: FnvHashMap(),
1271+
lexical_macro_resolutions: Vec::new(),
12701272
invocations: invocations,
12711273
}
12721274
}
@@ -3363,9 +3365,13 @@ impl<'a> Resolver<'a> {
33633365
}
33643366

33653367
fn report_shadowing_errors(&mut self) {
3368+
for (name, scope) in replace(&mut self.lexical_macro_resolutions, Vec::new()) {
3369+
self.resolve_macro_name(scope, name);
3370+
}
3371+
33663372
let mut reported_errors = FnvHashSet();
33673373
for binding in replace(&mut self.disallowed_shadowing, Vec::new()) {
3368-
if self.resolve_macro_name(binding.parent, binding.name, false).is_some() &&
3374+
if self.resolve_macro_name(binding.parent, binding.name).is_some() &&
33693375
reported_errors.insert((binding.name, binding.span)) {
33703376
let msg = format!("`{}` is already in scope", binding.name);
33713377
self.session.struct_span_err(binding.span, &msg)

src/librustc_resolve/macros.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'a> base::Resolver for Resolver<'a> {
174174
if let LegacyScope::Expansion(parent) = invocation.legacy_scope.get() {
175175
invocation.legacy_scope.set(LegacyScope::simplify_expansion(parent));
176176
}
177-
self.resolve_macro_name(invocation.legacy_scope.get(), name, true).ok_or_else(|| {
177+
self.resolve_macro_name(invocation.legacy_scope.get(), name).ok_or_else(|| {
178178
if force {
179179
let msg = format!("macro undefined: '{}!'", name);
180180
let mut err = self.session.struct_span_err(path.span, &msg);
@@ -189,17 +189,18 @@ impl<'a> base::Resolver for Resolver<'a> {
189189
}
190190

191191
impl<'a> Resolver<'a> {
192-
pub fn resolve_macro_name(&mut self,
193-
mut scope: LegacyScope<'a>,
194-
name: ast::Name,
195-
record_used: bool)
192+
pub fn resolve_macro_name(&mut self, mut scope: LegacyScope<'a>, name: ast::Name)
196193
-> Option<Rc<SyntaxExtension>> {
194+
let mut possible_time_travel = None;
197195
let mut relative_depth: u32 = 0;
198196
loop {
199197
scope = match scope {
200198
LegacyScope::Empty => break,
201199
LegacyScope::Expansion(invocation) => {
202200
if let LegacyScope::Empty = invocation.expansion.get() {
201+
if possible_time_travel.is_none() {
202+
possible_time_travel = Some(scope);
203+
}
203204
invocation.legacy_scope.get()
204205
} else {
205206
relative_depth += 1;
@@ -212,7 +213,10 @@ impl<'a> Resolver<'a> {
212213
}
213214
LegacyScope::Binding(binding) => {
214215
if binding.name == name {
215-
if record_used && relative_depth > 0 {
216+
if let Some(scope) = possible_time_travel {
217+
// Check for disallowed shadowing later
218+
self.lexical_macro_resolutions.push((name, scope));
219+
} else if relative_depth > 0 {
216220
self.disallowed_shadowing.push(binding);
217221
}
218222
return Some(binding.ext.clone());
@@ -222,6 +226,9 @@ impl<'a> Resolver<'a> {
222226
};
223227
}
224228

229+
if let Some(scope) = possible_time_travel {
230+
self.lexical_macro_resolutions.push((name, scope));
231+
}
225232
self.builtin_macros.get(&name).cloned()
226233
}
227234

0 commit comments

Comments
 (0)