Rollup merge of #80031 - petrochenkov:builtina, r=estebank
resolve: Reject ambiguity built-in attr vs different built-in attr Fixes https://github.com/rust-lang/rust/issues/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.
This commit is contained in:
commit
3d5e7e0f47
5 changed files with 34 additions and 9 deletions
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue