Skip to content

Commit 18563f6

Browse files
committed
Put spans in all parsed attributes
1 parent 9c4ff56 commit 18563f6

File tree

26 files changed

+157
-114
lines changed

26 files changed

+157
-114
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ pub enum AttributeKind {
187187
Align { align: Align, span: Span },
188188

189189
/// Represents `#[rustc_allow_const_fn_unstable]`.
190-
AllowConstFnUnstable(ThinVec<Symbol>),
190+
AllowConstFnUnstable { features: ThinVec<(Symbol, Span)> },
191191

192192
/// Represents `#[allow_internal_unstable]`.
193-
AllowInternalUnstable(ThinVec<(Symbol, Span)>),
193+
AllowInternalUnstable { features: ThinVec<(Symbol, Span)> },
194194

195195
/// Represents `#[rustc_as_ptr]` (used by the `dangling_pointers_from_temporaries` lint).
196-
AsPtr(Span),
196+
AsPtr { span: Span },
197197

198198
/// Represents `#[rustc_default_body_unstable]`.
199199
BodyStability {
@@ -217,7 +217,7 @@ pub enum AttributeKind {
217217
},
218218

219219
/// Represents `#[rustc_const_stable_indirect]`.
220-
ConstStabilityIndirect,
220+
ConstStabilityIndirect { span: Span },
221221

222222
/// Represents [`#[deprecated]`](https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html#the-deprecated-attribute).
223223
Deprecation { deprecation: Deprecation, span: Span },
@@ -226,14 +226,14 @@ pub enum AttributeKind {
226226
DocComment { style: AttrStyle, kind: CommentKind, span: Span, comment: Symbol },
227227

228228
/// Represents `#[inline]` and `#[rustc_force_inline]`.
229-
Inline(InlineAttr, Span),
229+
Inline { kind: InlineAttr, span: Span },
230230

231231
/// Represents `#[rustc_macro_transparency]`.
232-
MacroTransparency(Transparency),
232+
MacroTransparency { transparency: Transparency, span: Span },
233233
/// Represents `#[optimize(size|speed)]`
234-
Optimize(OptimizeAttr, Span),
234+
Optimize { kind: OptimizeAttr, span: Span },
235235
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
236-
Repr(ThinVec<(ReprAttr, Span)>),
236+
Repr { reprs: ThinVec<(ReprAttr, Span)> },
237237

238238
/// Represents `#[stable]`, `#[unstable]` and `#[rustc_allowed_through_unstable_modules]`.
239239
Stability {

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::iter;
2-
31
use rustc_attr_data_structures::AttributeKind;
42
use rustc_feature::{AttributeTemplate, template};
53
use rustc_span::{Span, Symbol, sym};
@@ -13,24 +11,24 @@ pub(crate) struct AllowInternalUnstableParser;
1311
impl<S: Stage> CombineAttributeParser<S> for AllowInternalUnstableParser {
1412
const PATH: &[Symbol] = &[sym::allow_internal_unstable];
1513
type Item = (Symbol, Span);
16-
const CONVERT: ConvertFn<Self::Item> = AttributeKind::AllowInternalUnstable;
14+
const CONVERT: ConvertFn<Self::Item> =
15+
|features| AttributeKind::AllowInternalUnstable { features };
1716
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
1817

1918
fn extend<'c>(
2019
cx: &'c mut AcceptContext<'_, '_, S>,
2120
args: &'c ArgParser<'_>,
2221
) -> impl IntoIterator<Item = Self::Item> {
2322
parse_unstable(cx, args, <Self as CombineAttributeParser<S>>::PATH[0])
24-
.into_iter()
25-
.zip(iter::repeat(cx.attr_span))
2623
}
2724
}
2825

2926
pub(crate) struct AllowConstFnUnstableParser;
3027
impl<S: Stage> CombineAttributeParser<S> for AllowConstFnUnstableParser {
3128
const PATH: &[Symbol] = &[sym::rustc_allow_const_fn_unstable];
32-
type Item = Symbol;
33-
const CONVERT: ConvertFn<Self::Item> = AttributeKind::AllowConstFnUnstable;
29+
type Item = (Symbol, Span);
30+
const CONVERT: ConvertFn<Self::Item> =
31+
|features| AttributeKind::AllowConstFnUnstable { features };
3432
const TEMPLATE: AttributeTemplate = template!(Word, List: "feat1, feat2, ...");
3533

3634
fn extend<'c>(
@@ -45,7 +43,7 @@ fn parse_unstable<S: Stage>(
4543
cx: &AcceptContext<'_, '_, S>,
4644
args: &ArgParser<'_>,
4745
symbol: Symbol,
48-
) -> impl IntoIterator<Item = Symbol> {
46+
) -> impl IntoIterator<Item = (Symbol, Span)> {
4947
let mut res = Vec::new();
5048

5149
let Some(list) = args.list() else {
@@ -59,7 +57,7 @@ fn parse_unstable<S: Stage>(
5957
for param in list.mixed() {
6058
let param_span = param.span();
6159
if let Some(ident) = param.meta_item().and_then(|i| i.path().word()) {
62-
res.push(ident.name);
60+
res.push((ident.name, param_span));
6361
} else {
6462
cx.emit_err(session_diagnostics::ExpectsFeatures {
6563
span: param_span,

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
2525
return None;
2626
};
2727

28-
let res = match single.meta_item().and_then(|i| i.path().word().map(|i| i.name)) {
28+
let kind = match single.meta_item().and_then(|i| i.path().word().map(|i| i.name)) {
2929
Some(sym::size) => OptimizeAttr::Size,
3030
Some(sym::speed) => OptimizeAttr::Speed,
3131
Some(sym::none) => OptimizeAttr::DoNotOptimize,
@@ -35,6 +35,6 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
3535
}
3636
};
3737

38-
Some(AttributeKind::Optimize(res, cx.attr_span))
38+
Some(AttributeKind::Optimize { kind, span: cx.attr_span })
3939
}
4040
}

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
2121
const TEMPLATE: AttributeTemplate = template!(Word, List: "always|never");
2222

2323
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
24+
let span = cx.attr_span;
2425
match args {
25-
ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
26+
ArgParser::NoArgs => Some(AttributeKind::Inline { kind: InlineAttr::Hint, span }),
2627
ArgParser::List(list) => {
2728
let Some(l) = list.single() else {
2829
cx.expected_single_argument(list.span);
@@ -31,10 +32,10 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
3132

3233
match l.meta_item().and_then(|i| i.path().word_sym()) {
3334
Some(sym::always) => {
34-
Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
35+
Some(AttributeKind::Inline { kind: InlineAttr::Always, span })
3536
}
3637
Some(sym::never) => {
37-
Some(AttributeKind::Inline(InlineAttr::Never, cx.attr_span))
38+
Some(AttributeKind::Inline { kind: InlineAttr::Never, span })
3839
}
3940
_ => {
4041
cx.expected_specific_argument(l.span(), vec!["always", "never"]);
@@ -89,9 +90,9 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
8990
}
9091
};
9192

92-
Some(AttributeKind::Inline(
93-
InlineAttr::Force { attr_span: cx.attr_span, reason },
94-
cx.attr_span,
95-
))
93+
Some(AttributeKind::Inline {
94+
kind: InlineAttr::Force { attr_span: cx.attr_span, reason },
95+
span: cx.attr_span,
96+
})
9697
}
9798
}

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ impl<S: Stage> SingleAttributeParser<S> for AsPtrParser {
1616

1717
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
1818
// FIXME: check that there's no args (this is currently checked elsewhere)
19-
Some(AttributeKind::AsPtr(cx.attr_span))
19+
Some(AttributeKind::AsPtr { span: cx.attr_span })
2020
}
2121
}

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) struct ReprParser;
2323
impl<S: Stage> CombineAttributeParser<S> for ReprParser {
2424
type Item = (ReprAttr, Span);
2525
const PATH: &[Symbol] = &[sym::repr];
26-
const CONVERT: ConvertFn<Self::Item> = AttributeKind::Repr;
26+
const CONVERT: ConvertFn<Self::Item> = |reprs| AttributeKind::Repr { reprs };
2727
// FIXME(jdonszelmann): never used
2828
const TEMPLATE: AttributeTemplate = template!(List: "C");
2929

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ impl<S: Stage> SingleAttributeParser<S> for ConstStabilityIndirectParser {
139139
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
140140
const TEMPLATE: AttributeTemplate = template!(Word);
141141

142-
fn convert(_cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
143-
Some(AttributeKind::ConstStabilityIndirect)
142+
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
143+
Some(AttributeKind::ConstStabilityIndirect { span: cx.attr_span })
144144
}
145145
}
146146

compiler/rustc_attr_parsing/src/attributes/transparency.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ impl<S: Stage> SingleAttributeParser<S> for TransparencyParser {
2626
cx.expected_name_value(cx.attr_span, None);
2727
return None;
2828
};
29-
match nv.value_as_str() {
30-
Some(sym::transparent) => Some(Transparency::Transparent),
31-
Some(sym::semiopaque | sym::semitransparent) => Some(Transparency::SemiOpaque),
32-
Some(sym::opaque) => Some(Transparency::Opaque),
33-
Some(_) => {
29+
30+
let transparency = match nv.value_as_str()? {
31+
sym::transparent => Transparency::Transparent,
32+
sym::semiopaque | sym::semitransparent => Transparency::SemiOpaque,
33+
sym::opaque => Transparency::Opaque,
34+
_ => {
3435
cx.expected_specific_argument_strings(
3536
nv.value_span,
3637
vec!["transparent", "semitransparent", "opaque"],
3738
);
38-
None
39+
return None
3940
}
40-
None => None,
41-
}
42-
.map(AttributeKind::MacroTransparency)
41+
};
42+
Some(AttributeKind::MacroTransparency { transparency, span: cx.attr_span })
4343
}
4444
}

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ impl<'a> TraitDef<'a> {
485485
Annotatable::Item(item) => {
486486
let is_packed = matches!(
487487
AttributeParser::parse_limited(cx.sess, &item.attrs, sym::repr, item.span, item.id),
488-
Some(Attribute::Parsed(AttributeKind::Repr(r))) if r.iter().any(|(x, _)| matches!(x, ReprPacked(..)))
488+
Some(Attribute::Parsed(AttributeKind::Repr{reprs})) if reprs.iter().any(|(x, _)| matches!(x, ReprPacked(..)))
489489
);
490490

491491
let newitem = match &item.kind {

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
442442

443443
let inline_span;
444444
(codegen_fn_attrs.inline, inline_span) = if let Some((inline_attr, span)) =
445-
find_attr!(attrs, AttributeKind::Inline(i, span) => (*i, *span))
445+
find_attr!(attrs, AttributeKind::Inline{kind, span} => (*kind, *span))
446446
{
447447
(inline_attr, Some(span))
448448
} else {
@@ -456,8 +456,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
456456
codegen_fn_attrs.inline = InlineAttr::Never;
457457
}
458458

459-
codegen_fn_attrs.optimize =
460-
find_attr!(attrs, AttributeKind::Optimize(i, _) => *i).unwrap_or(OptimizeAttr::Default);
459+
codegen_fn_attrs.optimize = find_attr!(attrs, AttributeKind::Optimize{kind, ..} => *kind)
460+
.unwrap_or(OptimizeAttr::Default);
461461

462462
// #73631: closures inherit `#[target_feature]` annotations
463463
//

compiler/rustc_const_eval/src/check_consts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn rustc_allow_const_fn_unstable(
8282
) -> bool {
8383
let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(def_id));
8484

85-
attrs::find_attr!(attrs, attrs::AttributeKind::AllowConstFnUnstable(syms) if syms.contains(&feature_gate))
85+
attrs::find_attr!(attrs, attrs::AttributeKind::AllowConstFnUnstable{features} if features.iter().any(|(f, _)| *f == feature_gate))
8686
}
8787

8888
/// Returns `true` if the given `def_id` (trait or function) is "safe to expose on stable".

compiler/rustc_expand/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,8 @@ impl SyntaxExtension {
880880
is_local: bool,
881881
) -> SyntaxExtension {
882882
let allow_internal_unstable =
883-
find_attr!(attrs, AttributeKind::AllowInternalUnstable(i) => i)
884-
.map(|i| i.as_slice())
883+
find_attr!(attrs, AttributeKind::AllowInternalUnstable{features} => features)
884+
.map(|f| f.as_slice())
885885
.unwrap_or_default();
886886
// FIXME(jdonszelman): allow_internal_unsafe isn't yet new-style
887887
// let allow_internal_unsafe = find_attr!(attrs, AttributeKind::AllowInternalUnsafe);

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,9 @@ pub fn compile_declarative_macro(
540540

541541
check_emission(macro_check::check_meta_variables(&sess.psess, node_id, span, &lhses, &rhses));
542542

543-
let transparency = find_attr!(attrs, AttributeKind::MacroTransparency(x) => *x)
544-
.unwrap_or(Transparency::fallback(macro_rules));
543+
let transparency =
544+
find_attr!(attrs, AttributeKind::MacroTransparency{transparency, .. } => *transparency)
545+
.unwrap_or(Transparency::fallback(macro_rules));
545546

546547
if let Some(guar) = guar {
547548
// To avoid warning noise, only consider the rules of this

compiler/rustc_hir/src/hir.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,12 +1297,33 @@ impl AttributeExt for Attribute {
12971297

12981298
#[inline]
12991299
fn span(&self) -> Span {
1300+
use AttributeKind::*;
1301+
1302+
fn combine(spans: impl IntoIterator<Item = Span>) -> Span {
1303+
spans.into_iter().reduce(|a, b| a.to(b)).unwrap_or(DUMMY_SP)
1304+
}
1305+
13001306
match &self {
13011307
Attribute::Unparsed(u) => u.span,
13021308
// FIXME: should not be needed anymore when all attrs are parsed
1303-
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
1304-
Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
1305-
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
1309+
Attribute::Parsed(p) => match p {
1310+
Confusables { first_span: span, .. }
1311+
| ConstStability { span, .. }
1312+
| Stability { span, .. }
1313+
| ConstStabilityIndirect { span }
1314+
| Deprecation { span, .. }
1315+
| DocComment { span, .. }
1316+
| Align { span, .. }
1317+
| AsPtr { span }
1318+
| Inline { span, .. }
1319+
| BodyStability { span, .. }
1320+
| MacroTransparency { span, .. }
1321+
| Optimize { span, .. } => *span,
1322+
AllowConstFnUnstable { features, .. } | AllowInternalUnstable { features, .. } => {
1323+
combine(features.iter().map(|(_, s)| s).copied())
1324+
}
1325+
Repr { reprs } => combine(reprs.iter().map(|(_, s)| s).copied()),
1326+
},
13061327
}
13071328
}
13081329

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,8 +1249,7 @@ fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
12491249
pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
12501250
let repr = def.repr();
12511251
if repr.packed() {
1252-
if let Some(reprs) =
1253-
attrs::find_attr!(tcx.get_all_attrs(def.did()), attrs::AttributeKind::Repr(r) => r)
1252+
if let Some(reprs) = attrs::find_attr!(tcx.get_all_attrs(def.did()), attrs::AttributeKind::Repr{reprs} => reprs)
12541253
{
12551254
for (r, _) in reprs {
12561255
if let ReprPacked(pack) = r
@@ -1469,10 +1468,10 @@ fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) {
14691468
if def.variants().is_empty() {
14701469
attrs::find_attr!(
14711470
tcx.get_all_attrs(def_id),
1472-
attrs::AttributeKind::Repr(rs) => {
1471+
attrs::AttributeKind::Repr{reprs} => {
14731472
struct_span_code_err!(
14741473
tcx.dcx(),
1475-
rs.first().unwrap().1,
1474+
reprs.first().unwrap().1,
14761475
E0084,
14771476
"unsupported representation for zero-variant enum"
14781477
)

compiler/rustc_lint/src/dangling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn lint_expr(cx: &LateContext<'_>, expr: &Expr<'_>) {
134134
&& let ty = cx.typeck_results().expr_ty(receiver)
135135
&& owns_allocation(cx.tcx, ty)
136136
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
137-
&& find_attr!(cx.tcx.get_all_attrs(fn_id), AttributeKind::AsPtr(_))
137+
&& find_attr!(cx.tcx.get_all_attrs(fn_id), AttributeKind::AsPtr { .. })
138138
{
139139
// FIXME: use `emit_node_lint` when `#[primary_span]` is added.
140140
cx.tcx.emit_node_span_lint(

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl EarlyLintPass for NonCamelCaseTypes {
165165
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
166166
let has_repr_c = matches!(
167167
AttributeParser::parse_limited(cx.sess(), &it.attrs, sym::repr, it.span, it.id),
168-
Some(Attribute::Parsed(AttributeKind::Repr(r))) if r.iter().any(|(r, _)| r == &ReprAttr::ReprC)
168+
Some(Attribute::Parsed(AttributeKind::Repr{reprs})) if reprs.iter().any(|(r, _)| r == &ReprAttr::ReprC)
169169
);
170170

171171
if has_repr_c {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,8 @@ impl<'tcx> TyCtxt<'tcx> {
15381538
field_shuffle_seed ^= user_seed;
15391539
}
15401540

1541-
if let Some(reprs) = attr::find_attr!(self.get_all_attrs(did), AttributeKind::Repr(r) => r)
1541+
if let Some(reprs) =
1542+
attr::find_attr!(self.get_all_attrs(did), AttributeKind::Repr{reprs} => reprs )
15421543
{
15431544
for (r, _) in reprs {
15441545
flags.insert(match *r {

0 commit comments

Comments
 (0)