Skip to content

Commit 5e7c031

Browse files
authored
Rollup merge of rust-lang#90826 - petrochenkov:binattr, r=cjgillot
rustc_feature: Convert `BuiltinAttribute` from tuple to a struct The tuple starts having too many fields. Noticed while reviewing rust-lang#88681.
2 parents 5b3cb68 + 6655727 commit 5e7c031

File tree

6 files changed

+62
-44
lines changed

6 files changed

+62
-44
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
33
use rustc_ast::{AssocTyConstraint, AssocTyConstraintKind, NodeId};
44
use rustc_ast::{PatKind, RangeEnd, VariantData};
55
use rustc_errors::struct_span_err;
6-
use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP};
6+
use rustc_feature::{AttributeGate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
77
use rustc_feature::{Features, GateIssue};
88
use rustc_session::parse::{feature_err, feature_err_issue};
99
use rustc_session::Session;
@@ -301,11 +301,14 @@ impl<'a> PostExpansionVisitor<'a> {
301301

302302
impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
303303
fn visit_attribute(&mut self, attr: &ast::Attribute) {
304-
let attr_info =
305-
attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
304+
let attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
306305
// Check feature gates for built-in attributes.
307-
if let Some((.., AttributeGate::Gated(_, name, descr, has_feature))) = attr_info {
308-
gate_feature_fn!(self, has_feature, attr.span, name, descr);
306+
if let Some(BuiltinAttribute {
307+
gate: AttributeGate::Gated(_, name, descr, has_feature),
308+
..
309+
}) = attr_info
310+
{
311+
gate_feature_fn!(self, has_feature, attr.span, *name, descr);
309312
}
310313
// Check unstable flavors of the `#[doc]` attribute.
311314
if attr.has_name(sym::doc) {

compiler/rustc_feature/src/builtin_attrs.rs

+41-24
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,26 @@ macro_rules! template {
115115

116116
macro_rules! ungated {
117117
($attr:ident, $typ:expr, $tpl:expr $(,)?) => {
118-
(sym::$attr, $typ, $tpl, Ungated)
118+
BuiltinAttribute { name: sym::$attr, type_: $typ, template: $tpl, gate: Ungated }
119119
};
120120
}
121121

122122
macro_rules! gated {
123123
($attr:ident, $typ:expr, $tpl:expr, $gate:ident, $msg:expr $(,)?) => {
124-
(sym::$attr, $typ, $tpl, Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate)))
124+
BuiltinAttribute {
125+
name: sym::$attr,
126+
type_: $typ,
127+
template: $tpl,
128+
gate: Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate)),
129+
}
125130
};
126131
($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
127-
(sym::$attr, $typ, $tpl, Gated(Stability::Unstable, sym::$attr, $msg, cfg_fn!($attr)))
132+
BuiltinAttribute {
133+
name: sym::$attr,
134+
type_: $typ,
135+
template: $tpl,
136+
gate: Gated(Stability::Unstable, sym::$attr, $msg, cfg_fn!($attr)),
137+
}
128138
};
129139
}
130140

@@ -143,12 +153,12 @@ macro_rules! rustc_attr {
143153
)
144154
};
145155
($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
146-
(
147-
sym::$attr,
148-
$typ,
149-
$tpl,
150-
Gated(Stability::Unstable, sym::rustc_attrs, $msg, cfg_fn!(rustc_attrs)),
151-
)
156+
BuiltinAttribute {
157+
name: sym::$attr,
158+
type_: $typ,
159+
template: $tpl,
160+
gate: Gated(Stability::Unstable, sym::rustc_attrs, $msg, cfg_fn!(rustc_attrs)),
161+
}
152162
};
153163
}
154164

@@ -161,7 +171,12 @@ macro_rules! experimental {
161171
const IMPL_DETAIL: &str = "internal implementation detail";
162172
const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never be stable";
163173

164-
pub type BuiltinAttribute = (Symbol, AttributeType, AttributeTemplate, AttributeGate);
174+
pub struct BuiltinAttribute {
175+
pub name: Symbol,
176+
pub type_: AttributeType,
177+
pub template: AttributeTemplate,
178+
pub gate: AttributeGate,
179+
}
165180

166181
/// Attributes that have a special meaning to rustc or rustdoc.
167182
#[rustfmt::skip]
@@ -290,18 +305,20 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
290305
),
291306

292307
// Plugins:
293-
(
294-
sym::plugin, CrateLevel, template!(List: "name"),
295-
Gated(
308+
BuiltinAttribute {
309+
name: sym::plugin,
310+
type_: CrateLevel,
311+
template: template!(List: "name"),
312+
gate: Gated(
296313
Stability::Deprecated(
297314
"https://github.com/rust-lang/rust/pull/64675",
298315
Some("may be removed in a future compiler version"),
299316
),
300317
sym::plugin,
301318
"compiler plugins are deprecated",
302319
cfg_fn!(plugin)
303-
)
304-
),
320+
),
321+
},
305322

306323
// Testing:
307324
gated!(allow_fail, Normal, template!(Word), experimental!(allow_fail)),
@@ -497,17 +514,17 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
497514
lang, Normal, template!(NameValueStr: "name"), lang_items,
498515
"language items are subject to change",
499516
),
500-
(
501-
sym::rustc_diagnostic_item,
502-
Normal,
503-
template!(NameValueStr: "name"),
504-
Gated(
517+
BuiltinAttribute {
518+
name: sym::rustc_diagnostic_item,
519+
type_: Normal,
520+
template: template!(NameValueStr: "name"),
521+
gate: Gated(
505522
Stability::Unstable,
506523
sym::rustc_attrs,
507524
"diagnostic items compiler internal support for linting",
508525
cfg_fn!(rustc_attrs),
509526
),
510-
),
527+
},
511528
gated!(
512529
// Used in resolve:
513530
prelude_import, Normal, template!(Word),
@@ -601,7 +618,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
601618
];
602619

603620
pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> {
604-
BUILTIN_ATTRIBUTES.iter().filter(|(.., gate)| gate.is_deprecated()).collect()
621+
BUILTIN_ATTRIBUTES.iter().filter(|attr| attr.gate.is_deprecated()).collect()
605622
}
606623

607624
pub fn is_builtin_attr_name(name: Symbol) -> bool {
@@ -612,8 +629,8 @@ pub static BUILTIN_ATTRIBUTE_MAP: SyncLazy<FxHashMap<Symbol, &BuiltinAttribute>>
612629
SyncLazy::new(|| {
613630
let mut map = FxHashMap::default();
614631
for attr in BUILTIN_ATTRIBUTES.iter() {
615-
if map.insert(attr.0, attr).is_some() {
616-
panic!("duplicate builtin attribute `{}`", attr.0);
632+
if map.insert(attr.name, attr).is_some() {
633+
panic!("duplicate builtin attribute `{}`", attr.name);
617634
}
618635
}
619636
map

compiler/rustc_lint/src/builtin.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ use rustc_ast_pretty::pprust::{self, expr_to_string};
3232
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3333
use rustc_data_structures::stack::ensure_sufficient_stack;
3434
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
35-
use rustc_feature::{deprecated_attributes, AttributeGate, AttributeTemplate, AttributeType};
36-
use rustc_feature::{GateIssue, Stability};
35+
use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, GateIssue, Stability};
3736
use rustc_hir as hir;
3837
use rustc_hir::def::{DefKind, Res};
3938
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
@@ -959,7 +958,7 @@ impl EarlyLintPass for AnonymousParameters {
959958
pub struct DeprecatedAttr {
960959
// This is not free to compute, so we want to keep it around, rather than
961960
// compute it for every attribute.
962-
depr_attrs: Vec<&'static (Symbol, AttributeType, AttributeTemplate, AttributeGate)>,
961+
depr_attrs: Vec<&'static BuiltinAttribute>,
963962
}
964963

965964
impl_lint_pass!(DeprecatedAttr => []);
@@ -990,14 +989,14 @@ fn lint_deprecated_attr(
990989

991990
impl EarlyLintPass for DeprecatedAttr {
992991
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
993-
for &&(n, _, _, ref g) in &self.depr_attrs {
994-
if attr.ident().map(|ident| ident.name) == Some(n) {
992+
for BuiltinAttribute { name, gate, .. } in &self.depr_attrs {
993+
if attr.ident().map(|ident| ident.name) == Some(*name) {
995994
if let &AttributeGate::Gated(
996995
Stability::Deprecated(link, suggestion),
997996
name,
998997
reason,
999998
_,
1000-
) = g
999+
) = gate
10011000
{
10021001
let msg =
10031002
format!("use of deprecated attribute `{}`: {}. See {}", name, reason, link);

compiler/rustc_parse/src/validate_attr.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::parse_in;
55
use rustc_ast::tokenstream::{DelimSpan, TokenTree};
66
use rustc_ast::{self as ast, Attribute, MacArgs, MacDelimiter, MetaItem, MetaItemKind};
77
use rustc_errors::{Applicability, FatalError, PResult};
8-
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
8+
use rustc_feature::{AttributeTemplate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
99
use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
1010
use rustc_session::parse::ParseSess;
1111
use rustc_span::{sym, Symbol};
@@ -15,14 +15,13 @@ pub fn check_meta(sess: &ParseSess, attr: &Attribute) {
1515
return;
1616
}
1717

18-
let attr_info =
19-
attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
18+
let attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
2019

2120
// Check input tokens for built-in and key-value attributes.
2221
match attr_info {
2322
// `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
24-
Some((name, _, template, _)) if name != sym::rustc_dummy => {
25-
check_builtin_attribute(sess, attr, name, template)
23+
Some(BuiltinAttribute { name, template, .. }) if *name != sym::rustc_dummy => {
24+
check_builtin_attribute(sess, attr, *name, *template)
2625
}
2726
_ if let MacArgs::Eq(..) = attr.get_normal_item().args => {
2827
// All key-value attributes are restricted to meta-item syntax.
@@ -168,7 +167,7 @@ pub fn emit_fatal_malformed_builtin_attribute(
168167
attr: &Attribute,
169168
name: Symbol,
170169
) -> ! {
171-
let template = BUILTIN_ATTRIBUTE_MAP.get(&name).expect("builtin attr defined").2;
170+
let template = BUILTIN_ATTRIBUTE_MAP.get(&name).expect("builtin attr defined").template;
172171
emit_malformed_attribute(sess, attr, name, template);
173172
// This is fatal, otherwise it will likely cause a cascade of other errors
174173
// (and an error here is expected to be very rare).

compiler/rustc_passes/src/check_attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::TyCtxt;
1111
use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, NestedMetaItem};
1212
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1313
use rustc_errors::{pluralize, struct_span_err, Applicability};
14-
use rustc_feature::{AttributeType, BUILTIN_ATTRIBUTE_MAP};
14+
use rustc_feature::{AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
1515
use rustc_hir as hir;
1616
use rustc_hir::def_id::LocalDefId;
1717
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
@@ -148,7 +148,7 @@ impl CheckAttrVisitor<'tcx> {
148148
}
149149

150150
if hir_id != CRATE_HIR_ID {
151-
if let Some((_, AttributeType::CrateLevel, ..)) =
151+
if let Some(BuiltinAttribute { type_: AttributeType::CrateLevel, .. }) =
152152
attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name))
153153
{
154154
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ impl<'a> Resolver<'a> {
731731
suggestions.extend(
732732
BUILTIN_ATTRIBUTES
733733
.iter()
734-
.map(|(name, ..)| TypoSuggestion::typo_from_res(*name, res)),
734+
.map(|attr| TypoSuggestion::typo_from_res(attr.name, res)),
735735
);
736736
}
737737
}

0 commit comments

Comments
 (0)