Skip to content

Commit 849f73a

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 849f73a

35 files changed

+192
-80
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 18 additions & 8 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 4 additions & 5 deletions
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

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
}],

0 commit comments

Comments
 (0)