Skip to content

Commit 985ae0b

Browse files
committed
Match against attribute name when validating attributes
Extract attribute name once and match it against symbols that are being validated, instead of using `Session::check_name` for each symbol individually. Assume that all validated attributes are used, instead of marking them as such, since the attribute check should be exhaustive.
1 parent ba6275b commit 985ae0b

File tree

3 files changed

+46
-52
lines changed

3 files changed

+46
-52
lines changed

compiler/rustc_ast/src/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub struct Path {
100100
}
101101

102102
impl PartialEq<Symbol> for Path {
103+
#[inline]
103104
fn eq(&self, symbol: &Symbol) -> bool {
104105
self.segments.len() == 1 && { self.segments[0].ident.name == *symbol }
105106
}

compiler/rustc_feature/src/builtin_attrs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
227227
template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...""#),
228228
),
229229
ungated!(link_name, AssumedUsed, template!(NameValueStr: "name")),
230-
ungated!(no_link, Normal, template!(Word)),
231-
ungated!(repr, Normal, template!(List: "C")),
230+
ungated!(no_link, AssumedUsed, template!(Word)),
231+
ungated!(repr, AssumedUsed, template!(List: "C")),
232232
ungated!(export_name, AssumedUsed, template!(NameValueStr: "name")),
233233
ungated!(link_section, AssumedUsed, template!(NameValueStr: "name")),
234234
ungated!(no_mangle, AssumedUsed, template!(Word)),
@@ -317,7 +317,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
317317
"custom test frameworks are an unstable feature",
318318
),
319319
// RFC #1268
320-
gated!(marker, Normal, template!(Word), marker_trait_attr, experimental!(marker)),
320+
gated!(marker, AssumedUsed, template!(Word), marker_trait_attr, experimental!(marker)),
321321
gated!(
322322
thread_local, AssumedUsed, template!(Word),
323323
"`#[thread_local]` is an experimental feature, and does not currently handle destructors",

compiler/rustc_passes/src/check_attr.rs

+42-49
Original file line numberDiff line numberDiff line change
@@ -69,55 +69,48 @@ impl CheckAttrVisitor<'tcx> {
6969
let mut is_valid = true;
7070
let attrs = self.tcx.hir().attrs(hir_id);
7171
for attr in attrs {
72-
is_valid &= if self.tcx.sess.check_name(attr, sym::inline) {
73-
self.check_inline(hir_id, attr, span, target)
74-
} else if self.tcx.sess.check_name(attr, sym::non_exhaustive) {
75-
self.check_non_exhaustive(hir_id, attr, span, target)
76-
} else if self.tcx.sess.check_name(attr, sym::marker) {
77-
self.check_marker(hir_id, attr, span, target)
78-
} else if self.tcx.sess.check_name(attr, sym::target_feature) {
79-
self.check_target_feature(hir_id, attr, span, target)
80-
} else if self.tcx.sess.check_name(attr, sym::track_caller) {
81-
self.check_track_caller(hir_id, &attr.span, attrs, span, target)
82-
} else if self.tcx.sess.check_name(attr, sym::doc) {
83-
self.check_doc_attrs(attr, hir_id, target)
84-
} else if self.tcx.sess.check_name(attr, sym::no_link) {
85-
self.check_no_link(hir_id, &attr, span, target)
86-
} else if self.tcx.sess.check_name(attr, sym::export_name) {
87-
self.check_export_name(hir_id, &attr, span, target)
88-
} else if self.tcx.sess.check_name(attr, sym::rustc_args_required_const) {
89-
self.check_rustc_args_required_const(&attr, span, target, item)
90-
} else if self.tcx.sess.check_name(attr, sym::rustc_layout_scalar_valid_range_start) {
91-
self.check_rustc_layout_scalar_valid_range(&attr, span, target)
92-
} else if self.tcx.sess.check_name(attr, sym::rustc_layout_scalar_valid_range_end) {
93-
self.check_rustc_layout_scalar_valid_range(&attr, span, target)
94-
} else if self.tcx.sess.check_name(attr, sym::allow_internal_unstable) {
95-
self.check_allow_internal_unstable(hir_id, &attr, span, target, &attrs)
96-
} else if self.tcx.sess.check_name(attr, sym::rustc_allow_const_fn_unstable) {
97-
self.check_rustc_allow_const_fn_unstable(hir_id, &attr, span, target)
98-
} else if self.tcx.sess.check_name(attr, sym::naked) {
99-
self.check_naked(hir_id, attr, span, target)
100-
} else if self.tcx.sess.check_name(attr, sym::rustc_legacy_const_generics) {
101-
self.check_rustc_legacy_const_generics(&attr, span, target, item)
102-
} else if self.tcx.sess.check_name(attr, sym::rustc_clean)
103-
|| self.tcx.sess.check_name(attr, sym::rustc_dirty)
104-
|| self.tcx.sess.check_name(attr, sym::rustc_if_this_changed)
105-
|| self.tcx.sess.check_name(attr, sym::rustc_then_this_would_need)
106-
{
107-
self.check_rustc_dirty_clean(&attr)
108-
} else {
109-
// lint-only checks
110-
if self.tcx.sess.check_name(attr, sym::cold) {
111-
self.check_cold(hir_id, attr, span, target);
112-
} else if self.tcx.sess.check_name(attr, sym::link_name) {
113-
self.check_link_name(hir_id, attr, span, target);
114-
} else if self.tcx.sess.check_name(attr, sym::link_section) {
115-
self.check_link_section(hir_id, attr, span, target);
116-
} else if self.tcx.sess.check_name(attr, sym::no_mangle) {
117-
self.check_no_mangle(hir_id, attr, span, target);
72+
is_valid &= match attr.name_or_empty() {
73+
sym::inline => self.check_inline(hir_id, attr, span, target),
74+
sym::non_exhaustive => self.check_non_exhaustive(hir_id, attr, span, target),
75+
sym::marker => self.check_marker(hir_id, attr, span, target),
76+
sym::target_feature => self.check_target_feature(hir_id, attr, span, target),
77+
sym::track_caller => {
78+
self.check_track_caller(hir_id, &attr.span, attrs, span, target)
11879
}
119-
true
80+
sym::doc => self.check_doc_attrs(attr, hir_id, target),
81+
sym::no_link => self.check_no_link(hir_id, &attr, span, target),
82+
sym::export_name => self.check_export_name(hir_id, &attr, span, target),
83+
sym::rustc_args_required_const => {
84+
self.check_rustc_args_required_const(&attr, span, target, item)
85+
}
86+
sym::rustc_layout_scalar_valid_range_start
87+
| sym::rustc_layout_scalar_valid_range_end => {
88+
self.check_rustc_layout_scalar_valid_range(&attr, span, target)
89+
}
90+
sym::allow_internal_unstable => {
91+
self.check_allow_internal_unstable(hir_id, &attr, span, target, &attrs)
92+
}
93+
sym::rustc_allow_const_fn_unstable => {
94+
self.check_rustc_allow_const_fn_unstable(hir_id, &attr, span, target)
95+
}
96+
sym::naked => self.check_naked(hir_id, attr, span, target),
97+
sym::rustc_legacy_const_generics => {
98+
self.check_rustc_legacy_const_generics(&attr, span, target, item)
99+
}
100+
sym::rustc_clean
101+
| sym::rustc_dirty
102+
| sym::rustc_if_this_changed
103+
| sym::rustc_then_this_would_need => self.check_rustc_dirty_clean(&attr),
104+
_ => true,
120105
};
106+
// lint-only checks
107+
match attr.name_or_empty() {
108+
sym::cold => self.check_cold(hir_id, attr, span, target),
109+
sym::link_name => self.check_link_name(hir_id, attr, span, target),
110+
sym::link_section => self.check_link_section(hir_id, attr, span, target),
111+
sym::no_mangle => self.check_no_mangle(hir_id, attr, span, target),
112+
_ => {}
113+
}
121114
}
122115

123116
if !is_valid {
@@ -1116,7 +1109,7 @@ impl CheckAttrVisitor<'tcx> {
11161109
// ```
11171110
let hints: Vec<_> = attrs
11181111
.iter()
1119-
.filter(|attr| self.tcx.sess.check_name(attr, sym::repr))
1112+
.filter(|attr| attr.has_name(sym::repr))
11201113
.filter_map(|attr| attr.meta_item_list())
11211114
.flatten()
11221115
.collect();
@@ -1287,7 +1280,7 @@ impl CheckAttrVisitor<'tcx> {
12871280

12881281
fn check_used(&self, attrs: &'hir [Attribute], target: Target) {
12891282
for attr in attrs {
1290-
if self.tcx.sess.check_name(attr, sym::used) && target != Target::Static {
1283+
if attr.has_name(sym::used) && target != Target::Static {
12911284
self.tcx
12921285
.sess
12931286
.span_err(attr.span, "attribute must be applied to a `static` variable");

0 commit comments

Comments
 (0)