Skip to content

Commit b3718fe

Browse files
committed
Fix incorrect cfg structured suggestion
Keep a span for the attribute *within* the square brackets as part of the `AttrKind`. ``` error: `cfg` is not followed by parentheses --> $DIR/cfg-attr-syntax-validation.rs:4:1 | LL | #[cfg = 10] | ^^^^^^^^^^^ | help: expected syntax is | LL - #[cfg = 10] LL + #[cfg(/* predicate */)] | ``` Noticed in #137343 (comment).
1 parent 2f8eeb2 commit b3718fe

File tree

37 files changed

+276
-82
lines changed

37 files changed

+276
-82
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3326,6 +3326,7 @@ impl NormalAttr {
33263326
path: Path::from_ident(ident),
33273327
args: AttrArgs::Empty,
33283328
tokens: None,
3329+
span: ident.span,
33293330
},
33303331
tokens: None,
33313332
}
@@ -3339,6 +3340,7 @@ pub struct AttrItem {
33393340
pub args: AttrArgs,
33403341
// Tokens for the meta item, e.g. just the `foo` within `#[foo]` or `#![foo]`.
33413342
pub tokens: Option<LazyAttrTokenStream>,
3343+
pub span: Span,
33423344
}
33433345

33443346
impl AttrItem {

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl Attribute {
231231
/// Extracts the MetaItem from inside this Attribute.
232232
pub fn meta(&self) -> Option<MetaItem> {
233233
match &self.kind {
234-
AttrKind::Normal(normal) => normal.item.meta(self.span),
234+
AttrKind::Normal(normal) => normal.item.meta(),
235235
AttrKind::DocComment(..) => None,
236236
}
237237
}
@@ -297,12 +297,12 @@ impl AttrItem {
297297
}
298298
}
299299

300-
pub fn meta(&self, span: Span) -> Option<MetaItem> {
300+
pub fn meta(&self) -> Option<MetaItem> {
301301
Some(MetaItem {
302302
unsafety: Safety::Default,
303303
path: self.path.clone(),
304304
kind: self.meta_kind()?,
305-
span,
305+
span: self.span(),
306306
})
307307
}
308308

@@ -644,8 +644,15 @@ fn mk_attr(
644644
path: Path,
645645
args: AttrArgs,
646646
span: Span,
647+
inner_span: Span,
647648
) -> Attribute {
648-
mk_attr_from_item(g, AttrItem { unsafety, path, args, tokens: None }, None, style, span)
649+
mk_attr_from_item(
650+
g,
651+
AttrItem { unsafety, path, args, tokens: None, span: inner_span },
652+
None,
653+
style,
654+
span,
655+
)
649656
}
650657

651658
pub fn mk_attr_from_item(
@@ -669,10 +676,11 @@ pub fn mk_attr_word(
669676
unsafety: Safety,
670677
name: Symbol,
671678
span: Span,
679+
inner_span: Span,
672680
) -> Attribute {
673681
let path = Path::from_ident(Ident::new(name, span));
674682
let args = AttrArgs::Empty;
675-
mk_attr(g, style, unsafety, path, args, span)
683+
mk_attr(g, style, unsafety, path, args, span, inner_span)
676684
}
677685

678686
pub fn mk_attr_nested_word(
@@ -682,6 +690,7 @@ pub fn mk_attr_nested_word(
682690
outer: Symbol,
683691
inner: Symbol,
684692
span: Span,
693+
inner_span: Span,
685694
) -> Attribute {
686695
let inner_tokens = TokenStream::new(vec![TokenTree::Token(
687696
Token::from_ast_ident(Ident::new(inner, span)),
@@ -694,7 +703,7 @@ pub fn mk_attr_nested_word(
694703
delim: Delimiter::Parenthesis,
695704
tokens: inner_tokens,
696705
});
697-
mk_attr(g, style, unsafety, path, attr_args, span)
706+
mk_attr(g, style, unsafety, path, attr_args, span, inner_span)
698707
}
699708

700709
pub fn mk_attr_name_value_str(
@@ -704,6 +713,7 @@ pub fn mk_attr_name_value_str(
704713
name: Symbol,
705714
val: Symbol,
706715
span: Span,
716+
inner_span: Span,
707717
) -> Attribute {
708718
let lit = token::Lit::new(token::Str, escape_string_symbol(val), None);
709719
let expr = P(Expr {
@@ -715,7 +725,7 @@ pub fn mk_attr_name_value_str(
715725
});
716726
let path = Path::from_ident(Ident::new(name, span));
717727
let args = AttrArgs::Eq { eq_span: span, expr };
718-
mk_attr(g, style, unsafety, path, args, span)
728+
mk_attr(g, style, unsafety, path, args, span, inner_span)
719729
}
720730

721731
pub fn filter_by_name<A: AttributeExt>(attrs: &[A], name: Symbol) -> impl Iterator<Item = &A> {

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@ macro_rules! common_visitor_and_walkers {
18781878
match kind {
18791879
AttrKind::Normal(normal) => {
18801880
let NormalAttr { item, tokens: _ } = &$($mut)?**normal;
1881-
let AttrItem { unsafety: _, path, args, tokens: _ } = item;
1881+
let AttrItem { unsafety: _, path, args, tokens: _, span: _ } = item;
18821882
try_visit!(vis.visit_path(path));
18831883
try_visit!(walk_attr_args(vis, args));
18841884
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19921992
sym::allow,
19931993
sym::unreachable_code,
19941994
try_span,
1995+
try_span,
19951996
);
19961997
let attrs: AttrVec = thin_vec![attr];
19971998

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ fn print_crate_inner<'a>(
287287
sym::feature,
288288
sym::prelude_import,
289289
DUMMY_SP,
290+
DUMMY_SP,
290291
);
291292
s.print_attribute(&fake_attr);
292293

@@ -300,6 +301,7 @@ fn print_crate_inner<'a>(
300301
Safety::Default,
301302
sym::no_std,
302303
DUMMY_SP,
304+
DUMMY_SP,
303305
);
304306
s.print_attribute(&fake_attr);
305307
}

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
9292
define_opaque: None,
9393
}));
9494

95-
let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)];
95+
let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span, span)];
9696

9797
let item = cx.item(span, attrs, kind);
9898
cx.stmt_item(sig_span, item)

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ impl<'cx, 'a> Context<'cx, 'a> {
112112
self.span,
113113
self.cx.item(
114114
self.span,
115-
thin_vec![self.cx.attr_nested_word(sym::allow, sym::unused_imports, self.span)],
115+
thin_vec![self.cx.attr_nested_word(
116+
sym::allow,
117+
sym::unused_imports,
118+
self.span,
119+
self.span
120+
)],
116121
ItemKind::Use(UseTree {
117122
prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])),
118123
kind: UseTreeKind::Nested {

compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ mod llvm_enzyme {
362362
path: ast::Path::from_ident(Ident::with_dummy_span(sym::inline)),
363363
args: ast::AttrArgs::Delimited(never_arg),
364364
tokens: None,
365+
span,
365366
};
366367
let inline_never_attr = P(ast::NormalAttr { item: inline_item, tokens: None });
367368
let new_id = ecx.sess.psess.attr_id_generator.mk_attr_id();

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) fn expand_deriving_clone(
8181
explicit_self: true,
8282
nonself_args: Vec::new(),
8383
ret_ty: Self_,
84-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
84+
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
8585
fieldless_variants_strategy: FieldlessVariantsStrategy::Default,
8686
combine_substructure: substructure,
8787
}],

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ pub(crate) fn expand_deriving_eq(
3232
nonself_args: vec![],
3333
ret_ty: Unit,
3434
attributes: thin_vec![
35-
cx.attr_word(sym::inline, span),
36-
cx.attr_nested_word(sym::doc, sym::hidden, span),
37-
cx.attr_nested_word(sym::coverage, sym::off, span)
35+
cx.attr_word(sym::inline, span, span),
36+
cx.attr_nested_word(sym::doc, sym::hidden, span, span),
37+
cx.attr_nested_word(sym::coverage, sym::off, span, span)
3838
],
3939
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
4040
combine_substructure: combine_substructure(Box::new(|a, b, c| {

compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) fn expand_deriving_ord(
2828
explicit_self: true,
2929
nonself_args: vec![(self_ref(), sym::other)],
3030
ret_ty: Path(path_std!(cmp::Ordering)),
31-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
31+
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
3232
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
3333
combine_substructure: combine_substructure(Box::new(|a, b, c| cs_cmp(a, b, c))),
3434
}],

compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(crate) fn expand_deriving_partial_eq(
4141
explicit_self: true,
4242
nonself_args: vec![(self_ref(), sym::other)],
4343
ret_ty: Path(path_local!(bool)),
44-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
44+
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
4545
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
4646
combine_substructure: combine_substructure(Box::new(|a, b, c| {
4747
BlockOrExpr::new_expr(get_substructure_equality_expr(a, b, c))

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) fn expand_deriving_partial_ord(
4747
explicit_self: true,
4848
nonself_args: vec![(self_ref(), sym::other)],
4949
ret_ty,
50-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
50+
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
5151
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
5252
combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
5353
cs_partial_cmp(cx, span, substr, discr_then_data)

compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub(crate) fn expand_deriving_coerce_pointee(
9898

9999
// Declare helper function that adds implementation blocks.
100100
// FIXME(dingxiangfei2009): Investigate the set of attributes on target struct to be propagated to impls
101-
let attrs = thin_vec![cx.attr_word(sym::automatically_derived, span),];
101+
let attrs = thin_vec![cx.attr_word(sym::automatically_derived, span, span),];
102102
// # Validity assertion which will be checked later in `rustc_hir_analysis::coherence::builtins`.
103103
{
104104
let trait_path =

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) fn expand_deriving_debug(
3232
explicit_self: true,
3333
nonself_args: vec![(fmtr, sym::f)],
3434
ret_ty: Path(path_std!(fmt::Result)),
35-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
35+
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
3636
fieldless_variants_strategy:
3737
FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless,
3838
combine_substructure: combine_substructure(Box::new(|a, b, c| {

compiler/rustc_builtin_macros/src/deriving/default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn expand_deriving_default(
3535
explicit_self: false,
3636
nonself_args: Vec::new(),
3737
ret_ty: Self_,
38-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
38+
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
3939
fieldless_variants_strategy: FieldlessVariantsStrategy::Default,
4040
combine_substructure: combine_substructure(Box::new(|cx, trait_span, substr| {
4141
match substr.fields {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ impl<'a> TraitDef<'a> {
785785
let path = cx.path_all(self.span, false, vec![type_ident], self_params);
786786
let self_type = cx.ty_path(path);
787787

788-
let attrs = thin_vec![cx.attr_word(sym::automatically_derived, self.span),];
788+
let attrs = thin_vec![cx.attr_word(sym::automatically_derived, self.span, self.span)];
789789
let opt_trait_ref = Some(trait_ref);
790790

791791
cx.item(

compiler/rustc_builtin_macros/src/deriving/hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub(crate) fn expand_deriving_hash(
3333
explicit_self: true,
3434
nonself_args: vec![(Ref(Box::new(Path(arg)), Mutability::Mut), sym::state)],
3535
ret_ty: Unit,
36-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
36+
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
3737
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
3838
combine_substructure: combine_substructure(Box::new(|a, b, c| {
3939
hash_substructure(a, b, c)

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl AllocFnFactory<'_, '_> {
102102
}
103103

104104
fn attrs(&self) -> AttrVec {
105-
thin_vec![self.cx.attr_word(sym::rustc_std_internal_symbol, self.span)]
105+
thin_vec![self.cx.attr_word(sym::rustc_std_internal_symbol, self.span, self.span)]
106106
}
107107

108108
fn arg_ty(&self, input: &AllocatorMethodInput, args: &mut ThinVec<Param>) -> P<Expr> {

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,9 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
372372
cx.expr_array_ref(span, decls),
373373
);
374374
decls_static.attrs.extend([
375-
cx.attr_word(sym::rustc_proc_macro_decls, span),
376-
cx.attr_word(sym::used, span),
377-
cx.attr_nested_word(sym::allow, sym::deprecated, span),
375+
cx.attr_word(sym::rustc_proc_macro_decls, span, span),
376+
cx.attr_word(sym::used, span, span),
377+
cx.attr_nested_word(sym::allow, sym::deprecated, span, span),
378378
]);
379379

380380
let block = cx.expr_block(

compiler/rustc_builtin_macros/src/standard_library_imports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn inject(
4343

4444
let item = cx.item(
4545
span,
46-
thin_vec![cx.attr_word(sym::macro_use, span)],
46+
thin_vec![cx.attr_word(sym::macro_use, span, span)],
4747
ast::ItemKind::ExternCrate(None, Ident::new(name, ident_span)),
4848
);
4949

@@ -67,7 +67,7 @@ pub fn inject(
6767
// Inject the relevant crate's prelude.
6868
let use_item = cx.item(
6969
span,
70-
thin_vec![cx.attr_word(sym::prelude_import, span)],
70+
thin_vec![cx.attr_word(sym::prelude_import, span, span)],
7171
ast::ItemKind::Use(ast::UseTree {
7272
prefix: cx.path(span, import_path),
7373
kind: ast::UseTreeKind::Glob,

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ pub(crate) fn expand_test_case(
6969
kind: ast::VisibilityKind::Public,
7070
tokens: None,
7171
};
72-
item.attrs.push(ecx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, sp));
72+
item.attrs.push(ecx.attr_name_value_str(
73+
sym::rustc_test_marker,
74+
test_path_symbol,
75+
sp,
76+
sp,
77+
));
7378
}
7479
_ => {}
7580
}
@@ -200,7 +205,7 @@ pub(crate) fn expand_test_or_bench(
200205
// corresponding macro declaration in `core::macros`.
201206
let coverage_off = |mut expr: P<ast::Expr>| {
202207
assert_matches!(expr.kind, ast::ExprKind::Closure(_));
203-
expr.attrs.push(cx.attr_nested_word(sym::coverage, sym::off, sp));
208+
expr.attrs.push(cx.attr_nested_word(sym::coverage, sym::off, sp, sp));
204209
expr
205210
};
206211

@@ -272,11 +277,11 @@ pub(crate) fn expand_test_or_bench(
272277
sp,
273278
thin_vec![
274279
// #[cfg(test)]
275-
cx.attr_nested_word(sym::cfg, sym::test, attr_sp),
280+
cx.attr_nested_word(sym::cfg, sym::test, attr_sp, attr_sp),
276281
// #[rustc_test_marker = "test_case_sort_key"]
277-
cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp),
282+
cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp, attr_sp),
278283
// #[doc(hidden)]
279-
cx.attr_nested_word(sym::doc, sym::hidden, attr_sp),
284+
cx.attr_nested_word(sym::doc, sym::hidden, attr_sp, attr_sp),
280285
],
281286
// const $ident: test::TestDescAndFn =
282287
ast::ItemKind::Const(

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ impl<'a> MutVisitor for EntryPointCleaner<'a> {
208208
sym::allow,
209209
sym::dead_code,
210210
self.def_site,
211+
self.def_site,
211212
);
212213
item.attrs.retain(|attr| !attr.has_name(sym::rustc_main));
213214
item.attrs.push(allow_dead_code);
@@ -312,11 +313,11 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
312313
);
313314

314315
// #[rustc_main]
315-
let main_attr = ecx.attr_word(sym::rustc_main, sp);
316+
let main_attr = ecx.attr_word(sym::rustc_main, sp, sp);
316317
// #[coverage(off)]
317-
let coverage_attr = ecx.attr_nested_word(sym::coverage, sym::off, sp);
318+
let coverage_attr = ecx.attr_nested_word(sym::coverage, sym::off, sp, sp);
318319
// #[doc(hidden)]
319-
let doc_hidden_attr = ecx.attr_nested_word(sym::doc, sym::hidden, sp);
320+
let doc_hidden_attr = ecx.attr_nested_word(sym::doc, sym::hidden, sp, sp);
320321

321322
// pub fn main() { ... }
322323
let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(ThinVec::new()));

0 commit comments

Comments
 (0)