Skip to content

Commit f34ac26

Browse files
committed
resolve: Model shadowing restriction for macro_rules after modern macros
This is a regression for legacy macros that will be fixed in the next commit
1 parent 83a51de commit f34ac26

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

src/librustc_resolve/macros.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use rustc_data_structures::sync::Lrc;
4646
use rustc_data_structures::small_vec::ExpectOne;
4747

4848
crate struct FromPrelude(bool);
49-
crate struct FromExpansion(bool);
5049

5150
#[derive(Clone)]
5251
pub struct InvocationData<'a> {
@@ -481,7 +480,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
481480
}
482481

483482
let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);
484-
let result = if let Some((legacy_binding, _)) = legacy_resolution {
483+
let result = if let Some(legacy_binding) = legacy_resolution {
485484
Ok(legacy_binding.def())
486485
} else {
487486
match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, force,
@@ -788,7 +787,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
788787
scope: &'a Cell<LegacyScope<'a>>,
789788
ident: Ident,
790789
record_used: bool)
791-
-> Option<(&'a NameBinding<'a>, FromExpansion)> {
790+
-> Option<&'a NameBinding<'a>> {
792791
let ident = ident.modern();
793792

794793
// Names from inner scope that can't shadow names from outer scopes, e.g.
@@ -798,15 +797,14 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
798797
// // the outer `mac` and we have and ambiguity error
799798
// mac!();
800799
// }
801-
let mut potentially_ambiguous_result: Option<(&NameBinding, FromExpansion)> = None;
800+
let mut potentially_ambiguous_result: Option<&NameBinding> = None;
802801

803802
// Go through all the scopes and try to resolve the name.
804803
let mut where_to_resolve = scope;
805-
let mut relative_depth = 0u32;
806804
loop {
807805
let result = match where_to_resolve.get() {
808806
LegacyScope::Binding(legacy_binding) => if ident == legacy_binding.ident {
809-
Some((legacy_binding.binding, FromExpansion(relative_depth > 0)))
807+
Some(legacy_binding.binding)
810808
} else {
811809
None
812810
}
@@ -816,16 +814,11 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
816814
macro_rules! continue_search { () => {
817815
where_to_resolve = match where_to_resolve.get() {
818816
LegacyScope::Binding(binding) => &binding.parent,
819-
LegacyScope::Invocation(invocation) => {
820-
relative_depth = relative_depth.saturating_sub(1);
821-
&invocation.legacy_scope
822-
}
817+
LegacyScope::Invocation(invocation) => &invocation.legacy_scope,
823818
LegacyScope::Expansion(invocation) => match invocation.expansion.get() {
824819
LegacyScope::Empty => &invocation.legacy_scope,
825-
LegacyScope::Binding(..) | LegacyScope::Expansion(..) => {
826-
relative_depth += 1;
827-
&invocation.expansion
828-
}
820+
LegacyScope::Binding(..) |
821+
LegacyScope::Expansion(..) => &invocation.expansion,
829822
LegacyScope::Invocation(..) => {
830823
where_to_resolve.set(invocation.legacy_scope.get());
831824
where_to_resolve
@@ -847,12 +840,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
847840
// Push an ambiguity error for later reporting and
848841
// return something for better recovery.
849842
if let Some(previous_result) = potentially_ambiguous_result {
850-
if result.0.def() != previous_result.0.def() {
843+
if result.def() != previous_result.def() {
851844
self.ambiguity_errors.push(AmbiguityError {
852845
span: ident.span,
853846
name: ident.name,
854-
b1: previous_result.0,
855-
b2: result.0,
847+
b1: previous_result,
848+
b2: result,
856849
});
857850
return Some(previous_result);
858851
}
@@ -861,7 +854,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
861854
// Found a solution that's not an ambiguity yet, but is "suspicious" and
862855
// can participate in ambiguities later on.
863856
// Remember it and go search for other solutions in outer scopes.
864-
if (result.1).0 {
857+
if result.expansion != Mark::root() {
865858
potentially_ambiguous_result = Some(result);
866859

867860
continue_search!();
@@ -933,16 +926,16 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
933926
self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
934927
err.emit();
935928
},
936-
(Some((legacy_binding, FromExpansion(_))), Ok((binding, FromPrelude(false)))) |
937-
(Some((legacy_binding, FromExpansion(true))), Ok((binding, FromPrelude(true)))) => {
929+
(Some(legacy_binding), Ok((binding, FromPrelude(from_prelude))))
930+
if !from_prelude || legacy_binding.expansion != Mark::root() => {
938931
if legacy_binding.def_ignoring_ambiguity() != binding.def_ignoring_ambiguity() {
939932
self.report_ambiguity_error(ident.name, span, legacy_binding, binding);
940933
}
941934
},
942-
// OK, non-macro-expanded legacy wins over macro prelude even if defs are different
943-
(Some((legacy_binding, FromExpansion(false))), Ok((_, FromPrelude(true)))) |
935+
// OK, non-macro-expanded legacy wins over prelude even if defs are different
936+
(Some(legacy_binding), Ok(_)) |
944937
// OK, unambiguous resolution
945-
(Some((legacy_binding, _)), Err(_)) => {
938+
(Some(legacy_binding), Err(_)) => {
946939
check_consistency(self, legacy_binding.def());
947940
}
948941
// OK, unambiguous resolution

src/libunwind/libunwind.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#![allow(nonstandard_style)]
1212

13-
macro_rules! cfg_if {
13+
macro_rules! cfg_if2 {
1414
( $( if #[cfg( $meta:meta )] { $($it1:item)* } else { $($it2:item)* } )* ) =>
1515
( $( $( #[cfg($meta)] $it1)* $( #[cfg(not($meta))] $it2)* )* )
1616
}
@@ -92,7 +92,7 @@ extern "C" {
9292
pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
9393
}
9494

95-
cfg_if! {
95+
cfg_if2! {
9696
if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm"))))] {
9797
// Not ARM EHABI
9898
#[repr(C)]
@@ -238,4 +238,4 @@ if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
238238
_Unwind_SjLj_RaiseException(exc)
239239
}
240240
}
241-
} // cfg_if!
241+
} // cfg_if2!

src/test/ui/macros/macro-shadowing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ foo!(); //~ ERROR `foo` is ambiguous
2828

2929
macro_rules! m2 { () => {
3030
macro_rules! foo { () => {} }
31-
foo!();
31+
foo!(); //~ ERROR `foo` is ambiguous
3232
}}
3333
m2!();
3434
//^ Since `foo` is not used outside this expansion, it is not a shadowing error.

src/test/ui/macros/macro-shadowing.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ LL | macro_rules! foo { () => {} }
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3131
= note: macro-expanded macros do not shadow
3232

33-
error: aborting due to 2 previous errors
33+
error[E0659]: `foo` is ambiguous
34+
--> $DIR/macro-shadowing.rs:31:5
35+
|
36+
LL | foo!(); //~ ERROR `foo` is ambiguous
37+
| ^^^
38+
|
39+
note: `foo` could refer to the name defined here
40+
--> $DIR/macro-shadowing.rs:30:5
41+
|
42+
LL | macro_rules! foo { () => {} }
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
...
45+
LL | m2!();
46+
| ------ in this macro invocation
47+
note: `foo` could also refer to the name defined here
48+
--> $DIR/macro-shadowing.rs:20:5
49+
|
50+
LL | macro_rules! foo { () => {} }
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
...
53+
LL | m1!();
54+
| ------ in this macro invocation
55+
= note: macro-expanded macros do not shadow
56+
57+
error: aborting due to 3 previous errors
3458

3559
For more information about this error, try `rustc --explain E0659`.

0 commit comments

Comments
 (0)