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| {

0 commit comments

Comments
 (0)