Skip to content

Commit 3d5e7e0

Browse files
authored
Rollup merge of #80031 - petrochenkov:builtina, r=estebank
resolve: Reject ambiguity built-in attr vs different built-in attr Fixes #79798. Resolution ensures that inert attributes cannot be used through imports like this, but built-in attributes don't go through initial resolution (only through resolution validation), so we have to keep some extra data (the built-in attribute name) to prevent it from happening.
2 parents f783871 + 7f9a2cf commit 3d5e7e0

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

compiler/rustc_hir/src/def.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_ast as ast;
55
use rustc_ast::NodeId;
66
use rustc_macros::HashStable_Generic;
77
use rustc_span::hygiene::MacroKind;
8+
use rustc_span::Symbol;
89

910
use std::array::IntoIter;
1011
use std::fmt::Debug;
@@ -34,7 +35,7 @@ pub enum CtorKind {
3435
#[derive(HashStable_Generic)]
3536
pub enum NonMacroAttrKind {
3637
/// Single-segment attribute defined by the language (`#[inline]`)
37-
Builtin,
38+
Builtin(Symbol),
3839
/// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
3940
Tool,
4041
/// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
@@ -371,7 +372,7 @@ impl CtorKind {
371372
impl NonMacroAttrKind {
372373
pub fn descr(self) -> &'static str {
373374
match self {
374-
NonMacroAttrKind::Builtin => "built-in attribute",
375+
NonMacroAttrKind::Builtin(..) => "built-in attribute",
375376
NonMacroAttrKind::Tool => "tool attribute",
376377
NonMacroAttrKind::DeriveHelper | NonMacroAttrKind::DeriveHelperCompat => {
377378
"derive helper attribute"
@@ -393,7 +394,7 @@ impl NonMacroAttrKind {
393394
NonMacroAttrKind::Tool
394395
| NonMacroAttrKind::DeriveHelper
395396
| NonMacroAttrKind::DeriveHelperCompat => true,
396-
NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false,
397+
NonMacroAttrKind::Builtin(..) | NonMacroAttrKind::Registered => false,
397398
}
398399
}
399400
}

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ impl<'a> Resolver<'a> {
683683
));
684684
}
685685
Scope::BuiltinAttrs => {
686-
let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin);
686+
let res = Res::NonMacroAttr(NonMacroAttrKind::Builtin(kw::Empty));
687687
if filter_fn(res) {
688688
suggestions.extend(
689689
BUILTIN_ATTRIBUTES

compiler/rustc_resolve/src/macros.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,11 @@ impl<'a> Resolver<'a> {
757757
}
758758
Scope::BuiltinAttrs => {
759759
if is_builtin_attr_name(ident.name) {
760-
ok(Res::NonMacroAttr(NonMacroAttrKind::Builtin), DUMMY_SP, this.arenas)
760+
ok(
761+
Res::NonMacroAttr(NonMacroAttrKind::Builtin(ident.name)),
762+
DUMMY_SP,
763+
this.arenas,
764+
)
761765
} else {
762766
Err(Determinacy::Determined)
763767
}
@@ -810,13 +814,15 @@ impl<'a> Resolver<'a> {
810814
// Found another solution, if the first one was "weak", report an error.
811815
let (res, innermost_res) = (binding.res(), innermost_binding.res());
812816
if res != innermost_res {
813-
let builtin = Res::NonMacroAttr(NonMacroAttrKind::Builtin);
817+
let is_builtin = |res| {
818+
matches!(res, Res::NonMacroAttr(NonMacroAttrKind::Builtin(..)))
819+
};
814820
let derive_helper_compat =
815821
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
816822

817823
let ambiguity_error_kind = if is_import {
818824
Some(AmbiguityKind::Import)
819-
} else if innermost_res == builtin || res == builtin {
825+
} else if is_builtin(innermost_res) || is_builtin(res) {
820826
Some(AmbiguityKind::BuiltinAttr)
821827
} else if innermost_res == derive_helper_compat
822828
|| res == derive_helper_compat

src/test/ui/proc-macro/ambiguous-builtin-attrs.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
// edition:2018
12
// aux-build:builtin-attrs.rs
2-
33
#![feature(decl_macro)] //~ ERROR `feature` is ambiguous
44

55
extern crate builtin_attrs;
@@ -31,3 +31,7 @@ fn main() {
3131
Bench;
3232
NonExistent; //~ ERROR cannot find value `NonExistent` in this scope
3333
}
34+
35+
use deny as allow;
36+
#[allow(unused)] //~ ERROR `allow` is ambiguous (built-in attribute vs any other name)
37+
fn builtin_renamed() {}

src/test/ui/proc-macro/ambiguous-builtin-attrs.stderr

+15-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ LL | use builtin_attrs::*;
6060
| ^^^^^^^^^^^^^^^^
6161
= help: use `crate::repr` to refer to this attribute macro unambiguously
6262

63+
error[E0659]: `allow` is ambiguous (built-in attribute vs any other name)
64+
--> $DIR/ambiguous-builtin-attrs.rs:36:3
65+
|
66+
LL | #[allow(unused)]
67+
| ^^^^^ ambiguous name
68+
|
69+
= note: `allow` could refer to a built-in attribute
70+
note: `allow` could also refer to the built-in attribute imported here
71+
--> $DIR/ambiguous-builtin-attrs.rs:35:5
72+
|
73+
LL | use deny as allow;
74+
| ^^^^^^^^^^^^^
75+
= help: use `crate::allow` to refer to this built-in attribute unambiguously
76+
6377
error[E0659]: `feature` is ambiguous (built-in attribute vs any other name)
6478
--> $DIR/ambiguous-builtin-attrs.rs:3:4
6579
|
@@ -80,7 +94,7 @@ error[E0517]: attribute should be applied to a struct, enum, or union
8094
LL | fn non_macro_expanded_location<#[repr(C)] T>() {
8195
| ^ - not a struct, enum, or union
8296

83-
error: aborting due to 7 previous errors
97+
error: aborting due to 8 previous errors
8498

8599
Some errors have detailed explanations: E0425, E0517, E0659.
86100
For more information about an error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)