Skip to content

Commit 747b7b3

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 f45d4ac commit 747b7b3

37 files changed

+205
-83
lines changed

compiler/rustc_ast/src/ast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3034,6 +3034,7 @@ impl NormalAttr {
30343034
path: Path::from_ident(ident),
30353035
args: AttrArgs::Empty,
30363036
tokens: None,
3037+
span: ident.span,
30373038
},
30383039
tokens: None,
30393040
}
@@ -3047,6 +3048,7 @@ pub struct AttrItem {
30473048
pub args: AttrArgs,
30483049
// Tokens for the meta item, e.g. just the `foo` within `#[foo]` or `#![foo]`.
30493050
pub tokens: Option<LazyAttrTokenStream>,
3051+
pub span: Span,
30503052
}
30513053

30523054
impl AttrItem {

compiler/rustc_ast/src/attr/mod.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl Attribute {
219219
/// Extracts the MetaItem from inside this Attribute.
220220
pub fn meta(&self) -> Option<MetaItem> {
221221
match &self.kind {
222-
AttrKind::Normal(normal) => normal.item.meta(self.span),
222+
AttrKind::Normal(normal) => normal.item.meta(),
223223
AttrKind::DocComment(..) => None,
224224
}
225225
}
@@ -285,12 +285,12 @@ impl AttrItem {
285285
}
286286
}
287287

288-
pub fn meta(&self, span: Span) -> Option<MetaItem> {
288+
pub fn meta(&self) -> Option<MetaItem> {
289289
Some(MetaItem {
290290
unsafety: Safety::Default,
291291
path: self.path.clone(),
292292
kind: self.meta_kind()?,
293-
span,
293+
span: self.span(),
294294
})
295295
}
296296

@@ -406,7 +406,7 @@ impl MetaItem {
406406
Path { span, segments, tokens: None }
407407
}
408408
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
409-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
409+
token::Nonterminal::NtMeta(item) => return item.meta(),
410410
token::Nonterminal::NtPath(path) => (**path).clone(),
411411
_ => return None,
412412
},
@@ -620,8 +620,15 @@ pub fn mk_attr(
620620
path: Path,
621621
args: AttrArgs,
622622
span: Span,
623+
inner_span: Span,
623624
) -> Attribute {
624-
mk_attr_from_item(g, AttrItem { unsafety, path, args, tokens: None }, None, style, span)
625+
mk_attr_from_item(
626+
g,
627+
AttrItem { unsafety, path, args, tokens: None, span: inner_span },
628+
None,
629+
style,
630+
span,
631+
)
625632
}
626633

627634
pub fn mk_attr_from_item(
@@ -645,10 +652,11 @@ pub fn mk_attr_word(
645652
unsafety: Safety,
646653
name: Symbol,
647654
span: Span,
655+
inner_span: Span,
648656
) -> Attribute {
649657
let path = Path::from_ident(Ident::new(name, span));
650658
let args = AttrArgs::Empty;
651-
mk_attr(g, style, unsafety, path, args, span)
659+
mk_attr(g, style, unsafety, path, args, span, inner_span)
652660
}
653661

654662
pub fn mk_attr_nested_word(
@@ -658,6 +666,7 @@ pub fn mk_attr_nested_word(
658666
outer: Symbol,
659667
inner: Symbol,
660668
span: Span,
669+
inner_span: Span,
661670
) -> Attribute {
662671
let inner_tokens = TokenStream::new(vec![TokenTree::Token(
663672
Token::from_ast_ident(Ident::new(inner, span)),
@@ -670,7 +679,7 @@ pub fn mk_attr_nested_word(
670679
delim: Delimiter::Parenthesis,
671680
tokens: inner_tokens,
672681
});
673-
mk_attr(g, style, unsafety, path, attr_args, span)
682+
mk_attr(g, style, unsafety, path, attr_args, span, inner_span)
674683
}
675684

676685
pub fn mk_attr_name_value_str(
@@ -680,6 +689,7 @@ pub fn mk_attr_name_value_str(
680689
name: Symbol,
681690
val: Symbol,
682691
span: Span,
692+
inner_span: Span,
683693
) -> Attribute {
684694
let lit = token::Lit::new(token::Str, escape_string_symbol(val), None);
685695
let expr = P(Expr {
@@ -691,7 +701,7 @@ pub fn mk_attr_name_value_str(
691701
});
692702
let path = Path::from_ident(Ident::new(name, span));
693703
let args = AttrArgs::Eq { eq_span: span, expr };
694-
mk_attr(g, style, unsafety, path, args, span)
704+
mk_attr(g, style, unsafety, path, args, span, inner_span)
695705
}
696706

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

compiler/rustc_ast/src/mut_visit.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,14 @@ fn walk_attribute<T: MutVisitor>(vis: &mut T, attr: &mut Attribute) {
723723
match kind {
724724
AttrKind::Normal(normal) => {
725725
let NormalAttr {
726-
item: AttrItem { unsafety: _, path, args, tokens },
726+
item: AttrItem { unsafety: _, path, args, tokens, span },
727727
tokens: attr_tokens,
728728
} = &mut **normal;
729729
vis.visit_path(path);
730730
visit_attr_args(vis, args);
731731
visit_lazy_tts(vis, tokens);
732732
visit_lazy_tts(vis, attr_tokens);
733+
vis.visit_span(span);
733734
}
734735
AttrKind::DocComment(_kind, _sym) => {}
735736
}
@@ -909,10 +910,11 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
909910
token::NtExpr(expr) => vis.visit_expr(expr),
910911
token::NtLiteral(expr) => vis.visit_expr(expr),
911912
token::NtMeta(item) => {
912-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
913+
let AttrItem { unsafety: _, path, args, tokens, span } = item.deref_mut();
913914
vis.visit_path(path);
914915
visit_attr_args(vis, args);
915916
visit_lazy_tts(vis, tokens);
917+
vis.visit_span(span);
916918
}
917919
token::NtPath(path) => vis.visit_path(path),
918920
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute)
13081308
match kind {
13091309
AttrKind::Normal(normal) => {
13101310
let NormalAttr { item, tokens: _ } = &**normal;
1311-
let AttrItem { unsafety: _, path, args, tokens: _ } = item;
1311+
let AttrItem { unsafety: _, path, args, tokens: _, span: _ } = item;
13121312
try_visit!(visitor.visit_path(path, DUMMY_NODE_ID));
13131313
try_visit!(walk_attr_args(visitor, args));
13141314
}

compiler/rustc_ast_lowering/src/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19851985
sym::allow,
19861986
sym::unreachable_code,
19871987
try_span,
1988+
try_span,
19881989
);
19891990
let attrs: AttrVec = thin_vec![attr];
19901991

compiler/rustc_ast_pretty/src/pprust/state.rs

+2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ pub fn print_crate<'a>(
257257
sym::feature,
258258
sym::prelude_import,
259259
DUMMY_SP,
260+
DUMMY_SP,
260261
);
261262
s.print_attribute(&fake_attr);
262263

@@ -270,6 +271,7 @@ pub fn print_crate<'a>(
270271
Safety::Default,
271272
sym::no_std,
272273
DUMMY_SP,
274+
DUMMY_SP,
273275
);
274276
s.print_attribute(&fake_attr);
275277
}

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
9090
body,
9191
}));
9292

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

9595
let item = cx.item(span, Ident::from_str_and_span("__rg_oom", span), attrs, kind);
9696
cx.stmt_item(sig_span, item)

compiler/rustc_builtin_macros/src/assert/context.rs

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

compiler/rustc_builtin_macros/src/cmdline_attrs.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
1717
raw_attr.clone(),
1818
));
1919

20-
let start_span = parser.token.span;
21-
let AttrItem { unsafety, path, args, tokens: _ } =
20+
let AttrItem { unsafety, path, args, tokens: _, span } =
2221
match parser.parse_attr_item(ForceCollect::No) {
2322
Ok(ai) => ai,
2423
Err(err) => {
2524
err.emit();
2625
continue;
2726
}
2827
};
29-
let end_span = parser.token.span;
3028
if parser.token != token::Eof {
31-
psess.dcx().emit_err(errors::InvalidCrateAttr { span: start_span.to(end_span) });
29+
psess.dcx().emit_err(errors::InvalidCrateAttr { span });
3230
continue;
3331
}
3432

@@ -38,7 +36,8 @@ pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
3836
unsafety,
3937
path,
4038
args,
41-
start_span.to(end_span),
39+
span,
40+
span,
4241
));
4342
}
4443
}

compiler/rustc_builtin_macros/src/deriving/clone.rs

+1-1
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

+3-3
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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub(crate) fn expand_deriving_partial_eq(
9595
explicit_self: true,
9696
nonself_args: vec![(self_ref(), sym::other)],
9797
ret_ty: Path(path_local!(bool)),
98-
attributes: thin_vec![cx.attr_word(sym::inline, span)],
98+
attributes: thin_vec![cx.attr_word(sym::inline, span, span)],
9999
fieldless_variants_strategy: FieldlessVariantsStrategy::Unify,
100100
combine_substructure: combine_substructure(Box::new(|a, b, c| cs_eq(a, b, c))),
101101
}];

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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub(crate) fn expand_deriving_coerce_pointee(
9999

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

compiler/rustc_builtin_macros/src/deriving/debug.rs

+1-1
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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ impl<'a> TraitDef<'a> {
777777
let path = cx.path_all(self.span, false, vec![type_ident], self_params);
778778
let self_type = cx.ty_path(path);
779779

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

783783
cx.item(

compiler/rustc_builtin_macros/src/deriving/hash.rs

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl AllocFnFactory<'_, '_> {
105105
}
106106

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

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

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
369369
cx.expr_array_ref(span, decls),
370370
)
371371
.map(|mut i| {
372-
i.attrs.push(cx.attr_word(sym::rustc_proc_macro_decls, span));
373-
i.attrs.push(cx.attr_word(sym::used, span));
374-
i.attrs.push(cx.attr_nested_word(sym::allow, sym::deprecated, span));
372+
i.attrs.push(cx.attr_word(sym::rustc_proc_macro_decls, span, span));
373+
i.attrs.push(cx.attr_word(sym::used, span, span));
374+
i.attrs.push(cx.attr_nested_word(sym::allow, sym::deprecated, span, span));
375375
i
376376
});
377377

compiler/rustc_builtin_macros/src/standard_library_imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn inject(
4444
let item = cx.item(
4545
span,
4646
Ident::new(name, ident_span),
47-
thin_vec![cx.attr_word(sym::macro_use, span)],
47+
thin_vec![cx.attr_word(sym::macro_use, span, span)],
4848
ast::ItemKind::ExternCrate(None),
4949
);
5050

@@ -68,7 +68,7 @@ pub fn inject(
6868
let use_item = cx.item(
6969
span,
7070
Ident::empty(),
71-
thin_vec![cx.attr_word(sym::prelude_import, span)],
71+
thin_vec![cx.attr_word(sym::prelude_import, span, span)],
7272
ast::ItemKind::Use(ast::UseTree {
7373
prefix: cx.path(span, import_path),
7474
kind: ast::UseTreeKind::Glob,

compiler/rustc_builtin_macros/src/test.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) fn expand_test_case(
6363
tokens: None,
6464
};
6565
item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
66-
item.attrs.push(ecx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, sp));
66+
item.attrs.push(ecx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, sp, sp));
6767
item
6868
});
6969

@@ -200,7 +200,7 @@ pub(crate) fn expand_test_or_bench(
200200
// corresponding macro declaration in `core::macros`.
201201
let coverage_off = |mut expr: P<ast::Expr>| {
202202
assert_matches!(expr.kind, ast::ExprKind::Closure(_));
203-
expr.attrs.push(cx.attr_nested_word(sym::coverage, sym::off, sp));
203+
expr.attrs.push(cx.attr_nested_word(sym::coverage, sym::off, sp, sp));
204204
expr
205205
};
206206

@@ -273,11 +273,11 @@ pub(crate) fn expand_test_or_bench(
273273
Ident::new(item.ident.name, sp),
274274
thin_vec![
275275
// #[cfg(test)]
276-
cx.attr_nested_word(sym::cfg, sym::test, attr_sp),
276+
cx.attr_nested_word(sym::cfg, sym::test, attr_sp, attr_sp),
277277
// #[rustc_test_marker = "test_case_sort_key"]
278-
cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp),
278+
cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp, attr_sp),
279279
// #[doc(hidden)]
280-
cx.attr_nested_word(sym::doc, sym::hidden, attr_sp),
280+
cx.attr_nested_word(sym::doc, sym::hidden, attr_sp, attr_sp),
281281
],
282282
// const $ident: test::TestDescAndFn =
283283
ast::ItemKind::Const(

0 commit comments

Comments
 (0)