//!
//! The actual definitions were copied from rustc's `compiler/rustc_feature/src/builtin_attrs.rs`.
//!
+//! It was last synchronized with upstream commit ae90dcf0207c57c3034f00b07048d63f8b2363c8.
+//!
//! The macros were adjusted to only expand to the attribute name, since that is all we need to do
//! name resolution, and `BUILTIN_ATTRIBUTES` is almost entirely unchanged from the original, to
//! ease updating.
+use once_cell::sync::OnceCell;
+use rustc_hash::FxHashMap;
+
/// Ignored attribute namespaces used by tools.
pub const TOOL_MODULES: &[&str] = &["rustfmt", "clippy"];
-type BuiltinAttribute = &'static str;
+pub struct BuiltinAttribute {
+ pub name: &'static str,
+ pub template: AttributeTemplate,
+}
+
+/// A template that the attribute input must match.
+/// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
+#[derive(Clone, Copy)]
+pub struct AttributeTemplate {
+ pub word: bool,
+ pub list: Option<&'static str>,
+ pub name_value_str: Option<&'static str>,
+}
+
+pub fn find_builtin_attr_idx(name: &str) -> Option<usize> {
+ static BUILTIN_LOOKUP_TABLE: OnceCell<FxHashMap<&'static str, usize>> = OnceCell::new();
+ BUILTIN_LOOKUP_TABLE
+ .get_or_init(|| {
+ INERT_ATTRIBUTES.iter().map(|attr| attr.name).enumerate().map(|(a, b)| (b, a)).collect()
+ })
+ .get(name)
+ .copied()
+}
+
+// impl AttributeTemplate {
+// const DEFAULT: AttributeTemplate =
+// AttributeTemplate { word: false, list: None, name_value_str: None };
+// }
+
+/// A convenience macro for constructing attribute templates.
+/// E.g., `template!(Word, List: "description")` means that the attribute
+/// supports forms `#[attr]` and `#[attr(description)]`.
+macro_rules! template {
+ (Word) => { template!(@ true, None, None) };
+ (List: $descr: expr) => { template!(@ false, Some($descr), None) };
+ (NameValueStr: $descr: expr) => { template!(@ false, None, Some($descr)) };
+ (Word, List: $descr: expr) => { template!(@ true, Some($descr), None) };
+ (Word, NameValueStr: $descr: expr) => { template!(@ true, None, Some($descr)) };
+ (List: $descr1: expr, NameValueStr: $descr2: expr) => {
+ template!(@ false, Some($descr1), Some($descr2))
+ };
+ (Word, List: $descr1: expr, NameValueStr: $descr2: expr) => {
+ template!(@ true, Some($descr1), Some($descr2))
+ };
+ (@ $word: expr, $list: expr, $name_value_str: expr) => {
+ AttributeTemplate {
+ word: $word, list: $list, name_value_str: $name_value_str
+ }
+ };
+}
macro_rules! ungated {
($attr:ident, $typ:expr, $tpl:expr $(,)?) => {
- stringify!($attr)
+ BuiltinAttribute { name: stringify!($attr), template: $tpl }
};
}
macro_rules! gated {
- ($attr:ident $($rest:tt)*) => {
- stringify!($attr)
+ ($attr:ident, $typ:expr, $tpl:expr, $gate:ident, $msg:expr $(,)?) => {
+ BuiltinAttribute { name: stringify!($attr), template: $tpl }
+ };
+ ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
+ BuiltinAttribute { name: stringify!($attr), template: $tpl }
};
}
macro_rules! rustc_attr {
- (TEST, $attr:ident $($rest:tt)*) => {
- stringify!($attr)
+ (TEST, $attr:ident, $typ:expr, $tpl:expr $(,)?) => {
+ rustc_attr!(
+ $attr,
+ $typ,
+ $tpl,
+ concat!(
+ "the `#[",
+ stringify!($attr),
+ "]` attribute is just used for rustc unit tests \
+ and will never be stable",
+ ),
+ )
};
- ($attr:ident $($rest:tt)*) => {
- stringify!($attr)
+ ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
+ BuiltinAttribute { name: stringify!($attr), template: $tpl }
};
}
-/// Attributes that have a special meaning to rustc or rustdoc.
+/// "Inert" built-in attributes that have a special meaning to rustc or rustdoc.
#[rustfmt::skip]
-pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
+pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
// ==========================================================================
// Stable attributes:
// ==========================================================================
ungated!(reexport_test_harness_main, Normal, template!(NameValueStr: "name")),
// Macros:
- ungated!(derive, Normal, template!(List: "Trait1, Trait2, ...")),
ungated!(automatically_derived, Normal, template!(Word)),
// FIXME(#14407)
ungated!(macro_use, Normal, template!(Word, List: "name1, name2, ...")),
template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...""#),
),
ungated!(link_name, AssumedUsed, template!(NameValueStr: "name")),
- ungated!(no_link, Normal, template!(Word)),
- ungated!(repr, Normal, template!(List: "C")),
+ ungated!(no_link, AssumedUsed, template!(Word)),
+ ungated!(repr, AssumedUsed, template!(List: "C")),
ungated!(export_name, AssumedUsed, template!(NameValueStr: "name")),
ungated!(link_section, AssumedUsed, template!(NameValueStr: "name")),
ungated!(no_mangle, AssumedUsed, template!(Word)),
const_eval_limit, CrateLevel, template!(NameValueStr: "N"), const_eval_limit,
experimental!(const_eval_limit)
),
+ gated!(
+ move_size_limit, CrateLevel, template!(NameValueStr: "N"), large_assignments,
+ experimental!(move_size_limit)
+ ),
// Entry point:
ungated!(main, Normal, template!(Word)),
template!(List: "address, memory, thread"),
experimental!(no_sanitize)
),
+ gated!(no_coverage, AssumedUsed, template!(Word), experimental!(no_coverage)),
// FIXME: #14408 assume docs are used since rustdoc looks at them.
ungated!(doc, AssumedUsed, template!(List: "hidden|inline|...", NameValueStr: "string")),
// Linking:
gated!(naked, AssumedUsed, template!(Word), naked_functions, experimental!(naked)),
- gated!(
- link_args, Normal, template!(NameValueStr: "args"),
- "the `link_args` attribute is experimental and not portable across platforms, \
- it is recommended to use `#[link(name = \"foo\")] instead",
- ),
gated!(
link_ordinal, AssumedUsed, template!(List: "ordinal"), raw_dylib,
experimental!(link_ordinal)
// Plugins:
// XXX Modified for use in rust-analyzer
- gated!(plugin_registrar),
- gated!(plugin),
+ gated!(plugin_registrar, Normal, template!(Word), experimental!()),
+ gated!(plugin, CrateLevel, template!(Word), experimental!()),
// Testing:
gated!(allow_fail, Normal, template!(Word), experimental!(allow_fail)),
"custom test frameworks are an unstable feature",
),
// RFC #1268
- gated!(marker, Normal, template!(Word), marker_trait_attr, experimental!(marker)),
+ gated!(marker, AssumedUsed, template!(Word), marker_trait_attr, experimental!(marker)),
gated!(
thread_local, AssumedUsed, template!(Word),
"`#[thread_local]` is an experimental feature, and does not currently handle destructors",
),
gated!(cmse_nonsecure_entry, AssumedUsed, template!(Word), experimental!(cmse_nonsecure_entry)),
+ // RFC 2632
+ gated!(
+ default_method_body_is_const, AssumedUsed, template!(Word), const_trait_impl,
+ "`default_method_body_is_const` is a temporary placeholder for declaring default bodies \
+ as `const`, which may be removed or renamed in the future."
+ ),
// ==========================================================================
// Internal attributes: Stability, deprecation, and unsafe:
),
gated!(panic_runtime, AssumedUsed, template!(Word), experimental!(panic_runtime)),
gated!(needs_panic_runtime, AssumedUsed, template!(Word), experimental!(needs_panic_runtime)),
- gated!(
- unwind, AssumedUsed, template!(List: "allowed|aborts"), unwind_attributes,
- experimental!(unwind),
- ),
gated!(
compiler_builtins, AssumedUsed, template!(Word),
"the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
// Internal attributes, Macro related:
// ==========================================================================
- rustc_attr!(rustc_builtin_macro, AssumedUsed, template!(Word), IMPL_DETAIL),
+ rustc_attr!(
+ rustc_builtin_macro, AssumedUsed,
+ template!(Word, List: "name, /*opt*/ attributes(name1, name2, ...)"),
+ IMPL_DETAIL,
+ ),
rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE),
rustc_attr!(
rustc_macro_transparency, AssumedUsed,
// ==========================================================================
rustc_attr!(rustc_promotable, AssumedUsed, template!(Word), IMPL_DETAIL),
- rustc_attr!(rustc_args_required_const, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
+ rustc_attr!(rustc_legacy_const_generics, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
// ==========================================================================
// Internal attributes, Layout related:
lang, Normal, template!(NameValueStr: "name"), lang_items,
"language items are subject to change",
),
- gated!(rustc_diagnostic_item), // XXX modified in rust-analyzer
+ gated!(rustc_diagnostic_item, Normal, template!(NameValueStr: "name"), experimental!()), // XXX Modified for use in rust-analyzer
gated!(
// Used in resolve:
prelude_import, AssumedUsed, template!(Word),
rustc_specialization_trait, Normal, template!(Word),
"the `#[rustc_specialization_trait]` attribute is used to check specializations"
),
+ rustc_attr!(
+ rustc_main, Normal, template!(Word),
+ "the `#[rustc_main]` attribute is used internally to specify test entry point function",
+ ),
+ rustc_attr!(
+ rustc_skip_array_during_method_dispatch, Normal, template!(Word),
+ "the `#[rustc_skip_array_during_method_dispatch]` attribute is used to exclude a trait \
+ from method dispatch when the receiver is an array, for compatibility in editions < 2021."
+ ),
// ==========================================================================
// Internal attributes, Testing:
rustc_attr!(TEST, rustc_outlives, Normal, template!(Word)),
rustc_attr!(TEST, rustc_capture_analysis, Normal, template!(Word)),
+ rustc_attr!(TEST, rustc_insignificant_dtor, Normal, template!(Word)),
rustc_attr!(TEST, rustc_variance, Normal, template!(Word)),
rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ...")),
rustc_attr!(TEST, rustc_regions, Normal, template!(Word)),
template!(Word, List: "delay_span_bug_from_inside_query")
),
rustc_attr!(TEST, rustc_dump_user_substs, AssumedUsed, template!(Word)),
+ rustc_attr!(TEST, rustc_evaluate_where_clauses, AssumedUsed, template!(Word)),
rustc_attr!(TEST, rustc_if_this_changed, AssumedUsed, template!(Word, List: "DepNode")),
rustc_attr!(TEST, rustc_then_this_would_need, AssumedUsed, template!(List: "DepNode")),
- rustc_attr!(
- TEST, rustc_dirty, AssumedUsed,
- template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
- ),
rustc_attr!(
TEST, rustc_clean, AssumedUsed,
template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
rustc_attr!(TEST, rustc_dump_program_clauses, AssumedUsed, template!(Word)),
rustc_attr!(TEST, rustc_dump_env_program_clauses, AssumedUsed, template!(Word)),
rustc_attr!(TEST, rustc_object_lifetime_default, AssumedUsed, template!(Word)),
+ rustc_attr!(TEST, rustc_dump_vtable, AssumedUsed, template!(Word)),
rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/)),
gated!(
omit_gdb_pretty_printer_section, AssumedUsed, template!(Word),