Skip to content

Commit 181804a

Browse files
committed
resolve: Partially unify bindings from macro_rules and from other items
1 parent 2687112 commit 181804a

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
}
@@ -1814,7 +1813,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
18141813
NameBindingKind::Import { .. } => false,
18151814
NameBindingKind::Ambiguity { b1, b2 } => {
18161815
self.ambiguity_errors.push(AmbiguityError {
1817-
span, name: ident.name, lexical: false, b1, b2,
1816+
span, name: ident.name, b1, b2,
18181817
});
18191818
true
18201819
}
@@ -4499,35 +4498,32 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
44994498
vis.is_accessible_from(module.normal_ancestor_id, self)
45004499
}
45014500

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

45254521
let mut err = struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
4526-
err.span_note(span1, &msg1);
4527-
match def2 {
4528-
Def::Macro(..) if span2.is_dummy() =>
4522+
err.span_note(b1.span, &msg1);
4523+
match b2.def() {
4524+
Def::Macro(..) if b2.span.is_dummy() =>
45294525
err.note(&format!("`{}` is also a builtin macro", name)),
4530-
_ => err.span_note(span2, &msg2),
4526+
_ => err.span_note(b2.span, &msg2),
45314527
};
45324528
if let Some(note) = note {
45334529
err.note(&note);
@@ -4552,15 +4548,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
45524548
);
45534549
}
45544550

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

@@ -4584,9 +4574,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
45844574
let mut reported_errors = FxHashSet();
45854575
for binding in replace(&mut self.disallowed_shadowing, Vec::new()) {
45864576
if self.resolve_legacy_scope(&binding.parent, binding.ident, false).is_some() &&
4587-
reported_errors.insert((binding.ident, binding.span)) {
4577+
reported_errors.insert((binding.ident, binding.binding.span)) {
45884578
let msg = format!("`{}` is already in scope", binding.ident);
4589-
self.session.struct_span_err(binding.span, &msg)
4579+
self.session.struct_span_err(binding.binding.span, &msg)
45904580
.note("macro-expanded `macro_rules!`s may not shadow \
45914581
existing macros (see RFC 1560)")
45924582
.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 {
@@ -722,7 +717,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
722717
name: ident.name,
723718
b1: previous_result.0,
724719
b2: result.0,
725-
lexical: true,
726720
});
727721
return Ok(previous_result);
728722
}
@@ -771,7 +765,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
771765
mut scope: &'a Cell<LegacyScope<'a>>,
772766
ident: Ident,
773767
record_used: bool)
774-
-> Option<(&'a LegacyBinding<'a>, FromExpansion)> {
768+
-> Option<(&'a NameBinding<'a>, FromExpansion)> {
775769
let ident = ident.modern();
776770
let mut relative_depth: u32 = 0;
777771
loop {
@@ -798,7 +792,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
798792
if record_used && relative_depth > 0 {
799793
self.disallowed_shadowing.push(potential_binding);
800794
}
801-
return Some((potential_binding, FromExpansion(relative_depth > 0)));
795+
return Some((potential_binding.binding, FromExpansion(relative_depth > 0)));
802796
}
803797
scope = &potential_binding.parent;
804798
}
@@ -858,18 +852,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
858852
self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
859853
err.emit();
860854
},
861-
(Some((legacy_binding, FromExpansion(from_expansion))),
862-
Ok((binding, FromPrelude(false)))) |
863-
(Some((legacy_binding, FromExpansion(from_expansion @ true))),
864-
Ok((binding, FromPrelude(true)))) => {
865-
if legacy_binding.def() != binding.def_ignoring_ambiguity() {
866-
self.report_ambiguity_error(
867-
ident.name, span, true,
868-
legacy_binding.def(), false, false,
869-
from_expansion, legacy_binding.span,
870-
binding.def(), binding.is_import(), binding.is_glob_import(),
871-
binding.expansion != Mark::root(), binding.span,
872-
);
855+
(Some((legacy_binding, FromExpansion(_))), Ok((binding, FromPrelude(false)))) |
856+
(Some((legacy_binding, FromExpansion(true))), Ok((binding, FromPrelude(true)))) => {
857+
if legacy_binding.def_ignoring_ambiguity() != binding.def_ignoring_ambiguity() {
858+
self.report_ambiguity_error(ident.name, span, legacy_binding, binding);
873859
}
874860
},
875861
// OK, non-macro-expanded legacy wins over macro prelude even if defs are different
@@ -990,10 +976,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
990976
if def.legacy {
991977
let ident = ident.modern();
992978
self.macro_names.insert(ident);
993-
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(LegacyBinding {
994-
parent: Cell::new(*legacy_scope), ident: ident, def_id: def_id, span: item.span,
995-
}));
996979
let def = Def::Macro(def_id, MacroKind::Bang);
980+
let vis = ty::Visibility::Invisible; // Doesn't matter for legacy bindings
981+
let binding = (def, vis, item.span, expansion).to_name_binding(self.arenas);
982+
*legacy_scope = LegacyScope::Binding(self.arenas.alloc_legacy_binding(
983+
LegacyBinding { parent: Cell::new(*legacy_scope), binding, ident }
984+
));
997985
self.all_macros.insert(ident.name, def);
998986
if attr::contains_name(&item.attrs, "macro_export") {
999987
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)