From 7f9a2cfa1b91b8525907582a42d896dcd2fa176b Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 13 Dec 2020 19:34:04 +0300 Subject: [PATCH] resolve: Reject ambiguity built-in attr vs different built-in attr --- compiler/rustc_hir/src/def.rs | 7 ++++--- compiler/rustc_resolve/src/diagnostics.rs | 2 +- compiler/rustc_resolve/src/macros.rs | 12 +++++++++--- .../ui/proc-macro/ambiguous-builtin-attrs.rs | 6 +++++- .../ui/proc-macro/ambiguous-builtin-attrs.stderr | 16 +++++++++++++++- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_hir/src/def.rs b/compiler/rustc_hir/src/def.rs index 4ede9d67b741f..a81eb747a335a 100644 --- a/compiler/rustc_hir/src/def.rs +++ b/compiler/rustc_hir/src/def.rs @@ -5,6 +5,7 @@ use rustc_ast as ast; use rustc_ast::NodeId; use rustc_macros::HashStable_Generic; use rustc_span::hygiene::MacroKind; +use rustc_span::Symbol; use std::array::IntoIter; use std::fmt::Debug; @@ -34,7 +35,7 @@ pub enum CtorKind { #[derive(HashStable_Generic)] pub enum NonMacroAttrKind { /// Single-segment attribute defined by the language (`#[inline]`) - Builtin, + Builtin(Symbol), /// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`). Tool, /// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`). @@ -371,7 +372,7 @@ impl CtorKind { impl NonMacroAttrKind { pub fn descr(self) -> &'static str { match self { - NonMacroAttrKind::Builtin => "built-in attribute", + NonMacroAttrKind::Builtin(..) => "built-in attribute", NonMacroAttrKind::Tool => "tool attribute", NonMacroAttrKind::DeriveHelper | NonMacroAttrKind::DeriveHelperCompat => { "derive helper attribute" @@ -393,7 +394,7 @@ impl NonMacroAttrKind { NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper | NonMacroAttrKind::DeriveHelperCompat => true, - NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false, + NonMacroAttrKind::Builtin(..) | NonMacroAttrKind::Registered => false, } } } diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 4f8047ac2dff0..69fb6870d2c67 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -683,7 +683,7 @@ impl<'a> Resolver<'a> { )); } Scope::BuiltinAttrs => { - let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin); + let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin(kw::Empty)); if filter_fn(res) { suggestions.extend( BUILTIN_ATTRIBUTES diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 5c74094ecd3ee..5d6120cd31076 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -757,7 +757,11 @@ impl<'a> Resolver<'a> { } Scope::BuiltinAttrs => { if is_builtin_attr_name(ident.name) { - ok(Res::NonMacroAttr(NonMacroAttrKind::Builtin), DUMMY_SP, this.arenas) + ok( + Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)), + DUMMY_SP, + this.arenas, + ) } else { Err(Determinacy::Determined) } @@ -810,13 +814,15 @@ impl<'a> Resolver<'a> { // Found another solution, if the first one was "weak", report an error. let (res, innermost_res) = (binding.res(), innermost_binding.res()); if res != innermost_res { - let builtin = Res::NonMacroAttr(NonMacroAttrKind::Builtin); + let is_builtin = |res| { + matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Builtin(..))) + }; let derive_helper_compat = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat); let ambiguity_error_kind = if is_import { Some(AmbiguityKind::Import) - } else if innermost_res == builtin || res == builtin { + } else if is_builtin(innermost_res) || is_builtin(res) { Some(AmbiguityKind::BuiltinAttr) } else if innermost_res == derive_helper_compat || res == derive_helper_compat diff --git a/src/test/ui/proc-macro/ambiguous-builtin-attrs.rs b/src/test/ui/proc-macro/ambiguous-builtin-attrs.rs index 142efb3c6cdcf..5f45f6892d2e3 100644 --- a/src/test/ui/proc-macro/ambiguous-builtin-attrs.rs +++ b/src/test/ui/proc-macro/ambiguous-builtin-attrs.rs @@ -1,5 +1,5 @@ +// edition:2018 // aux-build:builtin-attrs.rs - #![feature(decl_macro)] //~ ERROR `feature` is ambiguous extern crate builtin_attrs; @@ -31,3 +31,7 @@ fn main() { Bench; NonExistent; //~ ERROR cannot find value `NonExistent` in this scope } + +use deny as allow; +#[allow(unused)] //~ ERROR `allow` is ambiguous (built-in attribute vs any other name) +fn builtin_renamed() {} diff --git a/src/test/ui/proc-macro/ambiguous-builtin-attrs.stderr b/src/test/ui/proc-macro/ambiguous-builtin-attrs.stderr index 276ee1cfd3562..dfd60dc92cc35 100644 --- a/src/test/ui/proc-macro/ambiguous-builtin-attrs.stderr +++ b/src/test/ui/proc-macro/ambiguous-builtin-attrs.stderr @@ -60,6 +60,20 @@ LL | use builtin_attrs::*; | ^^^^^^^^^^^^^^^^ = help: use `crate::repr` to refer to this attribute macro unambiguously +error[E0659]: `allow` is ambiguous (built-in attribute vs any other name) + --> $DIR/ambiguous-builtin-attrs.rs:36:3 + | +LL | #[allow(unused)] + | ^^^^^ ambiguous name + | + = note: `allow` could refer to a built-in attribute +note: `allow` could also refer to the built-in attribute imported here + --> $DIR/ambiguous-builtin-attrs.rs:35:5 + | +LL | use deny as allow; + | ^^^^^^^^^^^^^ + = help: use `crate::allow` to refer to this built-in attribute unambiguously + error[E0659]: `feature` is ambiguous (built-in attribute vs any other name) --> $DIR/ambiguous-builtin-attrs.rs:3:4 | @@ -80,7 +94,7 @@ error[E0517]: attribute should be applied to a struct, enum, or union LL | fn non_macro_expanded_location<#[repr(C)] T>() { | ^ - not a struct, enum, or union -error: aborting due to 7 previous errors +error: aborting due to 8 previous errors Some errors have detailed explanations: E0425, E0517, E0659. For more information about an error, try `rustc --explain E0425`.