Skip to content

Commit 6ee9e97

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 ee8491e commit 6ee9e97

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> {
@@ -458,7 +457,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
458457
}
459458

460459
let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);
461-
let result = if let Some((legacy_binding, _)) = legacy_resolution {
460+
let result = if let Some(legacy_binding) = legacy_resolution {
462461
Ok(legacy_binding.def())
463462
} else {
464463
match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, force,
@@ -765,7 +764,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
765764
scope: &'a Cell<LegacyScope<'a>>,
766765
ident: Ident,
767766
record_used: bool)
768-
-> Option<(&'a NameBinding<'a>, FromExpansion)> {
767+
-> Option<&'a NameBinding<'a>> {
769768
let ident = ident.modern();
770769

771770
// Names from inner scope that can't shadow names from outer scopes, e.g.
@@ -775,15 +774,14 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
775774
// // the outer `mac` and we have and ambiguity error
776775
// mac!();
777776
// }
778-
let mut potentially_ambiguous_result: Option<(&NameBinding, FromExpansion)> = None;
777+
let mut potentially_ambiguous_result: Option<&NameBinding> = None;
779778

780779
// Go through all the scopes and try to resolve the name.
781780
let mut where_to_resolve = scope;
782-
let mut relative_depth = 0u32;
783781
loop {
784782
let result = match where_to_resolve.get() {
785783
LegacyScope::Binding(legacy_binding) => if ident == legacy_binding.ident {
786-
Some((legacy_binding.binding, FromExpansion(relative_depth > 0)))
784+
Some(legacy_binding.binding)
787785
} else {
788786
None
789787
}
@@ -793,16 +791,11 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
793791
macro_rules! continue_search { () => {
794792
where_to_resolve = match where_to_resolve.get() {
795793
LegacyScope::Binding(binding) => &binding.parent,
796-
LegacyScope::Invocation(invocation) => {
797-
relative_depth = relative_depth.saturating_sub(1);
798-
&invocation.legacy_scope
799-
}
794+
LegacyScope::Invocation(invocation) => &invocation.legacy_scope,
800795
LegacyScope::Expansion(invocation) => match invocation.expansion.get() {
801796
LegacyScope::Empty => &invocation.legacy_scope,
802-
LegacyScope::Binding(..) | LegacyScope::Expansion(..) => {
803-
relative_depth += 1;
804-
&invocation.expansion
805-
}
797+
LegacyScope::Binding(..) |
798+
LegacyScope::Expansion(..) => &invocation.expansion,
806799
LegacyScope::Invocation(..) => {
807800
where_to_resolve.set(invocation.legacy_scope.get());
808801
where_to_resolve
@@ -824,12 +817,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
824817
// Push an ambiguity error for later reporting and
825818
// return something for better recovery.
826819
if let Some(previous_result) = potentially_ambiguous_result {
827-
if result.0.def() != previous_result.0.def() {
820+
if result.def() != previous_result.def() {
828821
self.ambiguity_errors.push(AmbiguityError {
829822
span: ident.span,
830823
name: ident.name,
831-
b1: previous_result.0,
832-
b2: result.0,
824+
b1: previous_result,
825+
b2: result,
833826
});
834827
return Some(previous_result);
835828
}
@@ -838,7 +831,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
838831
// Found a solution that's not an ambiguity yet, but is "suspicious" and
839832
// can participate in ambiguities later on.
840833
// Remember it and go search for other solutions in outer scopes.
841-
if (result.1).0 {
834+
if result.expansion != Mark::root() {
842835
potentially_ambiguous_result = Some(result);
843836

844837
continue_search!();
@@ -910,16 +903,16 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
910903
self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
911904
err.emit();
912905
},
913-
(Some((legacy_binding, FromExpansion(_))), Ok((binding, FromPrelude(false)))) |
914-
(Some((legacy_binding, FromExpansion(true))), Ok((binding, FromPrelude(true)))) => {
906+
(Some(legacy_binding), Ok((binding, FromPrelude(from_prelude))))
907+
if !from_prelude || legacy_binding.expansion != Mark::root() => {
915908
if legacy_binding.def_ignoring_ambiguity() != binding.def_ignoring_ambiguity() {
916909
self.report_ambiguity_error(ident.name, span, legacy_binding, binding);
917910
}
918911
},
919-
// OK, non-macro-expanded legacy wins over macro prelude even if defs are different
920-
(Some((legacy_binding, FromExpansion(false))), Ok((_, FromPrelude(true)))) |
912+
// OK, non-macro-expanded legacy wins over prelude even if defs are different
913+
(Some(legacy_binding), Ok(_)) |
921914
// OK, unambiguous resolution
922-
(Some((legacy_binding, _)), Err(_)) => {
915+
(Some(legacy_binding), Err(_)) => {
923916
check_consistency(self, legacy_binding.def());
924917
}
925918
// 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)