Skip to content

Commit 4e5e045

Browse files
committed
resolve: Partially unify bindings from macro_rules and from other items
1 parent 06da917 commit 4e5e045

File tree

3 files changed

+32
-55
lines changed

3 files changed

+32
-55
lines changed

src/librustc_resolve/lib.rs

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,6 @@ struct UseError<'a> {
11661166
struct AmbiguityError<'a> {
11671167
span: Span,
11681168
name: Name,
1169-
lexical: bool,
11701169
b1: &'a NameBinding<'a>,
11711170
b2: &'a NameBinding<'a>,
11721171
}
@@ -1816,7 +1815,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
18161815
NameBindingKind::Import { .. } => false,
18171816
NameBindingKind::Ambiguity { b1, b2 } => {
18181817
self.ambiguity_errors.push(AmbiguityError {
1819-
span, name: ident.name, lexical: false, b1, b2,
1818+
span, name: ident.name, b1, b2,
18201819
});
18211820
true
18221821
}
@@ -4501,35 +4500,32 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
45014500
vis.is_accessible_from(module.normal_ancestor_id, self)
45024501
}
45034502

4504-
fn report_ambiguity_error(
4505-
&self, name: Name, span: Span, _lexical: bool,
4506-
def1: Def, is_import1: bool, is_glob1: bool, from_expansion1: bool, span1: Span,
4507-
def2: Def, is_import2: bool, _is_glob2: bool, _from_expansion2: bool, span2: Span,
4508-
) {
4503+
fn report_ambiguity_error(&self, name: Name, span: Span, b1: &NameBinding, b2: &NameBinding) {
45094504
let participle = |is_import: bool| if is_import { "imported" } else { "defined" };
4510-
let msg1 = format!("`{}` could refer to the name {} here", name, participle(is_import1));
4505+
let msg1 =
4506+
format!("`{}` could refer to the name {} here", name, participle(b1.is_import()));
45114507
let msg2 =
4512-
format!("`{}` could also refer to the name {} here", name, participle(is_import2));
4513-
let note = if from_expansion1 {
4514-
Some(if let Def::Macro(..) = def1 {
4508+
format!("`{}` could also refer to the name {} here", name, participle(b2.is_import()));
4509+
let note = if b1.expansion != Mark::root() {
4510+
Some(if let Def::Macro(..) = b1.def() {
45154511
format!("macro-expanded {} do not shadow",
4516-
if is_import1 { "macro imports" } else { "macros" })
4512+
if b1.is_import() { "macro imports" } else { "macros" })
45174513
} else {
45184514
format!("macro-expanded {} do not shadow when used in a macro invocation path",
4519-
if is_import1 { "imports" } else { "items" })
4515+
if b1.is_import() { "imports" } else { "items" })
45204516
})
4521-
} else if is_glob1 {
4517+
} else if b1.is_glob_import() {
45224518
Some(format!("consider adding an explicit import of `{}` to disambiguate", name))
45234519
} else {
45244520
None
45254521
};
45264522

45274523
let mut err = struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
4528-
err.span_note(span1, &msg1);
4529-
match def2 {
4530-
Def::Macro(..) if span2.is_dummy() =>
4524+
err.span_note(b1.span, &msg1);
4525+
match b2.def() {
4526+
Def::Macro(..) if b2.span.is_dummy() =>
45314527
err.note(&format!("`{}` is also a builtin macro", name)),
4532-
_ => err.span_note(span2, &msg2),
4528+
_ => err.span_note(b2.span, &msg2),
45334529
};
45344530
if let Some(note) = note {
45354531
err.note(&note);
@@ -4554,15 +4550,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
45544550
);
45554551
}
45564552

4557-
for &AmbiguityError { span, name, b1, b2, lexical } in &self.ambiguity_errors {
4553+
for &AmbiguityError { span, name, b1, b2 } in &self.ambiguity_errors {
45584554
if reported_spans.insert(span) {
4559-
self.report_ambiguity_error(
4560-
name, span, lexical,
4561-
b1.def(), b1.is_import(), b1.is_glob_import(),
4562-
b1.expansion != Mark::root(), b1.span,
4563-
b2.def(), b2.is_import(), b2.is_glob_import(),
4564-
b2.expansion != Mark::root(), b2.span,
4565-
);
4555+
self.report_ambiguity_error(name, span, b1, b2);
45664556
}
45674557
}
45684558

@@ -4586,9 +4576,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
45864576
let mut reported_errors = FxHashSet();
45874577
for binding in replace(&mut self.disallowed_shadowing, Vec::new()) {
45884578
if self.resolve_legacy_scope(&binding.parent, binding.ident, false).is_some() &&
4589-
reported_errors.insert((binding.ident, binding.span)) {
4579+
reported_errors.insert((binding.ident, binding.binding.span)) {
45904580
let msg = format!("`{}` is already in scope", binding.ident);
4591-
self.session.struct_span_err(binding.span, &msg)
4581+
self.session.struct_span_err(binding.binding.span, &msg)
45924582
.note("macro-expanded `macro_rules!`s may not shadow \
45934583
existing macros (see RFC 1560)")
45944584
.emit();

src/librustc_resolve/macros.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,12 @@ pub enum LegacyScope<'a> {
7878
Binding(&'a LegacyBinding<'a>),
7979
}
8080

81+
// Binding produced by a `macro_rules` item.
82+
// Not modularized, can shadow previous legacy bindings, etc.
8183
pub struct LegacyBinding<'a> {
84+
pub binding: &'a NameBinding<'a>,
8285
pub parent: Cell<LegacyScope<'a>>,
8386
pub ident: Ident,
84-
def_id: DefId,
85-
pub span: Span,
86-
}
87-
88-
impl<'a> LegacyBinding<'a> {
89-
fn def(&self) -> Def {
90-
Def::Macro(self.def_id, MacroKind::Bang)
91-
}
9287
}
9388

9489
pub struct ProcMacError {
@@ -745,7 +740,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
745740
name: ident.name,
746741
b1: previous_result.0,
747742
b2: result.0,
748-
lexical: true,
749743
});
750744
return Ok(previous_result);
751745
}
@@ -794,7 +788,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
794788
mut scope: &'a Cell<LegacyScope<'a>>,
795789
ident: Ident,
796790
record_used: bool)
797-
-> Option<(&'a LegacyBinding<'a>, FromExpansion)> {
791+
-> Option<(&'a NameBinding<'a>, FromExpansion)> {
798792
let ident = ident.modern();
799793
let mut relative_depth: u32 = 0;
800794
loop {
@@ -821,7 +815,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
821815
if record_used && relative_depth > 0 {
822816
self.disallowed_shadowing.push(potential_binding);
823817
}
824-
return Some((potential_binding, FromExpansion(relative_depth > 0)));
818+
return Some((potential_binding.binding, FromExpansion(relative_depth > 0)));
825819
}
826820
scope = &potential_binding.parent;
827821
}
@@ -881,18 +875,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
881875
self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
882876
err.emit();
883877
},
884-
(Some((legacy_binding, FromExpansion(from_expansion))),
885-
Ok((binding, FromPrelude(false)))) |
886-
(Some((legacy_binding, FromExpansion(from_expansion @ true))),
887-
Ok((binding, FromPrelude(true)))) => {
888-
if legacy_binding.def() != binding.def_ignoring_ambiguity() {
889-
self.report_ambiguity_error(
890-
ident.name, span, true,
891-
legacy_binding.def(), false, false,
892-
from_expansion, legacy_binding.span,
893-
binding.def(), binding.is_import(), binding.is_glob_import(),
894-
binding.expansion != Mark::root(), binding.span,
895-
);
878+
(Some((legacy_binding, FromExpansion(_))), Ok((binding, FromPrelude(false)))) |
879+
(Some((legacy_binding, FromExpansion(true))), Ok((binding, FromPrelude(true)))) => {
880+
if legacy_binding.def_ignoring_ambiguity() != binding.def_ignoring_ambiguity() {
881+
self.report_ambiguity_error(ident.name, span, legacy_binding, binding);
896882
}
897883
},
898884
// OK, non-macro-expanded legacy wins over macro prelude even if defs are different
@@ -1013,10 +999,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
1013999
if def.legacy {
10141000
let ident = ident.modern();
10151001
self.macro_names.insert(ident);
1016-
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
1017-
parent: Cell::new(*legacy_scope), ident: ident, def_id: def_id, span: item.span,
1018-
}));
10191002
let def = Def::Macro(def_id, MacroKind::Bang);
1003+
let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
1004+
let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
1005+
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(
1006+
LegacyBinding { parent: Cell::new(*legacy_scope), binding, ident }
1007+
));
10201008
self.all_macros.insert(ident.name, def);
10211009
if attr::contains_name(&item.attrs, "macro_export") {
10221010
let module = self.graph_root;

src/librustc_resolve/resolve_imports.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
251251
self.ambiguity_errors.push(AmbiguityError {
252252
span: path_span,
253253
name,
254-
lexical: false,
255254
b1: binding,
256255
b2: shadowed_glob,
257256
});

0 commit comments

Comments
 (0)