]> git.lizzy.rs Git - rust.git/commitdiff
resolve: Model shadowing restriction for macro_rules after modern macros
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Mon, 27 Aug 2018 23:20:59 +0000 (02:20 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sat, 8 Sep 2018 11:15:10 +0000 (14:15 +0300)
This is a regression for legacy macros that will be fixed in the next commit

src/librustc_resolve/macros.rs
src/libunwind/libunwind.rs
src/test/ui/macros/macro-shadowing.rs
src/test/ui/macros/macro-shadowing.stderr

index 3baf581c0dec076c8aad7479ead58825f6a311d6..7494e7e4ce5a3e0316390c25808c3fca3a5f3811 100644 (file)
@@ -46,7 +46,6 @@
 use rustc_data_structures::small_vec::ExpectOne;
 
 crate struct FromPrelude(bool);
-crate struct FromExpansion(bool);
 
 #[derive(Clone)]
 pub struct InvocationData<'a> {
@@ -481,7 +480,7 @@ pub fn resolve_macro_to_def_inner(&mut self, path: &ast::Path, kind: MacroKind,
         }
 
         let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);
-        let result = if let Some((legacy_binding, _)) = legacy_resolution {
+        let result = if let Some(legacy_binding) = legacy_resolution {
             Ok(legacy_binding.def())
         } else {
             match self.resolve_lexical_macro_path_segment(path[0], MacroNS, false, force,
@@ -788,7 +787,7 @@ fn resolve_legacy_scope(&mut self,
                             scope: &'a Cell<LegacyScope<'a>>,
                             ident: Ident,
                             record_used: bool)
-                            -> Option<(&'a NameBinding<'a>, FromExpansion)> {
+                            -> Option<&'a NameBinding<'a>> {
         let ident = ident.modern();
 
         // Names from inner scope that can't shadow names from outer scopes, e.g.
@@ -798,15 +797,14 @@ fn resolve_legacy_scope(&mut self,
         //                    // the outer `mac` and we have and ambiguity error
         //     mac!();
         // }
-        let mut potentially_ambiguous_result: Option<(&NameBinding, FromExpansion)> = None;
+        let mut potentially_ambiguous_result: Option<&NameBinding> = None;
 
         // Go through all the scopes and try to resolve the name.
         let mut where_to_resolve = scope;
-        let mut relative_depth = 0u32;
         loop {
             let result = match where_to_resolve.get() {
                 LegacyScope::Binding(legacy_binding) => if ident == legacy_binding.ident {
-                    Some((legacy_binding.binding, FromExpansion(relative_depth > 0)))
+                    Some(legacy_binding.binding)
                 } else {
                     None
                 }
@@ -816,16 +814,11 @@ fn resolve_legacy_scope(&mut self,
             macro_rules! continue_search { () => {
                 where_to_resolve = match where_to_resolve.get() {
                     LegacyScope::Binding(binding) => &binding.parent,
-                    LegacyScope::Invocation(invocation) => {
-                        relative_depth = relative_depth.saturating_sub(1);
-                        &invocation.legacy_scope
-                    }
+                    LegacyScope::Invocation(invocation) => &invocation.legacy_scope,
                     LegacyScope::Expansion(invocation) => match invocation.expansion.get() {
                         LegacyScope::Empty => &invocation.legacy_scope,
-                        LegacyScope::Binding(..) | LegacyScope::Expansion(..) => {
-                            relative_depth += 1;
-                            &invocation.expansion
-                        }
+                        LegacyScope::Binding(..) |
+                        LegacyScope::Expansion(..) => &invocation.expansion,
                         LegacyScope::Invocation(..) => {
                             where_to_resolve.set(invocation.legacy_scope.get());
                             where_to_resolve
@@ -847,12 +840,12 @@ macro_rules! continue_search { () => {
                     // Push an ambiguity error for later reporting and
                     // return something for better recovery.
                     if let Some(previous_result) = potentially_ambiguous_result {
-                        if result.0.def() != previous_result.0.def() {
+                        if result.def() != previous_result.def() {
                             self.ambiguity_errors.push(AmbiguityError {
                                 span: ident.span,
                                 name: ident.name,
-                                b1: previous_result.0,
-                                b2: result.0,
+                                b1: previous_result,
+                                b2: result,
                             });
                             return Some(previous_result);
                         }
@@ -861,7 +854,7 @@ macro_rules! continue_search { () => {
                     // Found a solution that's not an ambiguity yet, but is "suspicious" and
                     // can participate in ambiguities later on.
                     // Remember it and go search for other solutions in outer scopes.
-                    if (result.1).0 {
+                    if result.expansion != Mark::root() {
                         potentially_ambiguous_result = Some(result);
 
                         continue_search!();
@@ -933,16 +926,16 @@ pub fn finalize_current_module_macro_resolutions(&mut self) {
                     self.suggest_macro_name(&ident.as_str(), kind, &mut err, span);
                     err.emit();
                 },
-                (Some((legacy_binding, FromExpansion(_))), Ok((binding, FromPrelude(false)))) |
-                (Some((legacy_binding, FromExpansion(true))), Ok((binding, FromPrelude(true)))) => {
+                (Some(legacy_binding), Ok((binding, FromPrelude(from_prelude))))
+                        if !from_prelude || legacy_binding.expansion != Mark::root() => {
                     if legacy_binding.def_ignoring_ambiguity() != binding.def_ignoring_ambiguity() {
                         self.report_ambiguity_error(ident.name, span, legacy_binding, binding);
                     }
                 },
-                // OK, non-macro-expanded legacy wins over macro prelude even if defs are different
-                (Some((legacy_binding, FromExpansion(false))), Ok((_, FromPrelude(true)))) |
+                // OK, non-macro-expanded legacy wins over prelude even if defs are different
+                (Some(legacy_binding), Ok(_)) |
                 // OK, unambiguous resolution
-                (Some((legacy_binding, _)), Err(_)) => {
+                (Some(legacy_binding), Err(_)) => {
                     check_consistency(self, legacy_binding.def());
                 }
                 // OK, unambiguous resolution
index 43c3e1e7666239568d219c4dd2299e8ae8521132..d9c18408ac9ae30fb50b7ddb3fc1faf73c11e0ad 100644 (file)
@@ -10,7 +10,7 @@
 
 #![allow(nonstandard_style)]
 
-macro_rules! cfg_if {
+macro_rules! cfg_if2 {
     ( $( if #[cfg( $meta:meta )] { $($it1:item)* } else { $($it2:item)* } )* ) =>
         ( $( $( #[cfg($meta)] $it1)* $( #[cfg(not($meta))] $it2)* )* )
 }
@@ -92,7 +92,7 @@ pub enum _Unwind_Context {}
     pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
 }
 
-cfg_if! {
+cfg_if2! {
 if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm"))))] {
     // Not ARM EHABI
     #[repr(C)]
@@ -238,4 +238,4 @@ pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Rea
         _Unwind_SjLj_RaiseException(exc)
     }
 }
-} // cfg_if!
+} // cfg_if2!
index bf0a7fa21d37acb3126d0292f8efa1e1b1b74ef2..85d8f29fe28b68bed81e812ff731b39e75389824 100644 (file)
@@ -28,7 +28,7 @@ macro_rules! foo { () => {} }
 
 macro_rules! m2 { () => {
     macro_rules! foo { () => {} }
-    foo!();
+    foo!(); //~ ERROR `foo` is ambiguous
 }}
 m2!();
 //^ Since `foo` is not used outside this expansion, it is not a shadowing error.
index 04f4abc401337c2c06ce99a2162d62fd44529c66..0f28f123b43d90e2adb64843b3773b2c4310cf57 100644 (file)
@@ -30,6 +30,30 @@ LL | macro_rules! foo { () => {} }
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: macro-expanded macros do not shadow
 
-error: aborting due to 2 previous errors
+error[E0659]: `foo` is ambiguous
+  --> $DIR/macro-shadowing.rs:31:5
+   |
+LL |     foo!(); //~ ERROR `foo` is ambiguous
+   |     ^^^
+   |
+note: `foo` could refer to the name defined here
+  --> $DIR/macro-shadowing.rs:30:5
+   |
+LL |     macro_rules! foo { () => {} }
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+...
+LL | m2!();
+   | ------ in this macro invocation
+note: `foo` could also refer to the name defined here
+  --> $DIR/macro-shadowing.rs:20:5
+   |
+LL |     macro_rules! foo { () => {} }
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+...
+LL | m1!();
+   | ------ in this macro invocation
+   = note: macro-expanded macros do not shadow
+
+error: aborting due to 3 previous errors
 
 For more information about this error, try `rustc --explain E0659`.