Skip to content

Commit dfe7d5c

Browse files
authored
Rollup merge of #128524 - chenyukang:yukang-fix-127930-invalid-outer-style-sugg, r=cjgillot
Don't suggest turning crate-level attributes into outer style Fixes #127930
2 parents 0030892 + 22aa104 commit dfe7d5c

13 files changed

+70
-53
lines changed

compiler/rustc_ast/src/ast.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2902,6 +2902,17 @@ pub struct AttrItem {
29022902
pub tokens: Option<LazyAttrTokenStream>,
29032903
}
29042904

2905+
impl AttrItem {
2906+
pub fn is_valid_for_outer_style(&self) -> bool {
2907+
self.path == sym::cfg_attr
2908+
|| self.path == sym::cfg
2909+
|| self.path == sym::forbid
2910+
|| self.path == sym::warn
2911+
|| self.path == sym::allow
2912+
|| self.path == sym::deny
2913+
}
2914+
}
2915+
29052916
/// `TraitRef`s appear in impls.
29062917
///
29072918
/// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all

compiler/rustc_parse/src/parser/attr.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl<'a> Parser<'a> {
7676
token::CommentKind::Line => OuterAttributeType::DocComment,
7777
token::CommentKind::Block => OuterAttributeType::DocBlockComment,
7878
},
79+
true,
7980
) {
8081
err.note(fluent::parse_note);
8182
err.span_suggestion_verbose(
@@ -139,7 +140,11 @@ impl<'a> Parser<'a> {
139140

140141
// Emit error if inner attribute is encountered and forbidden.
141142
if style == ast::AttrStyle::Inner {
142-
this.error_on_forbidden_inner_attr(attr_sp, inner_parse_policy);
143+
this.error_on_forbidden_inner_attr(
144+
attr_sp,
145+
inner_parse_policy,
146+
item.is_valid_for_outer_style(),
147+
);
143148
}
144149

145150
Ok(attr::mk_attr_from_item(&self.psess.attr_id_generator, item, None, style, attr_sp))
@@ -151,6 +156,7 @@ impl<'a> Parser<'a> {
151156
err: &mut Diag<'_>,
152157
span: Span,
153158
attr_type: OuterAttributeType,
159+
suggest_to_outer: bool,
154160
) -> Option<Span> {
155161
let mut snapshot = self.create_snapshot_for_diagnostic();
156162
let lo = span.lo()
@@ -185,16 +191,18 @@ impl<'a> Parser<'a> {
185191
// FIXME(#100717)
186192
err.arg("item", item.kind.descr());
187193
err.span_label(item.span, fluent::parse_label_does_not_annotate_this);
188-
err.span_suggestion_verbose(
189-
replacement_span,
190-
fluent::parse_sugg_change_inner_to_outer,
191-
match attr_type {
192-
OuterAttributeType::Attribute => "",
193-
OuterAttributeType::DocBlockComment => "*",
194-
OuterAttributeType::DocComment => "/",
195-
},
196-
rustc_errors::Applicability::MachineApplicable,
197-
);
194+
if suggest_to_outer {
195+
err.span_suggestion_verbose(
196+
replacement_span,
197+
fluent::parse_sugg_change_inner_to_outer,
198+
match attr_type {
199+
OuterAttributeType::Attribute => "",
200+
OuterAttributeType::DocBlockComment => "*",
201+
OuterAttributeType::DocComment => "/",
202+
},
203+
rustc_errors::Applicability::MachineApplicable,
204+
);
205+
}
198206
return None;
199207
}
200208
Err(item_err) => {
@@ -205,7 +213,12 @@ impl<'a> Parser<'a> {
205213
Some(replacement_span)
206214
}
207215

208-
pub(super) fn error_on_forbidden_inner_attr(&self, attr_sp: Span, policy: InnerAttrPolicy) {
216+
pub(super) fn error_on_forbidden_inner_attr(
217+
&self,
218+
attr_sp: Span,
219+
policy: InnerAttrPolicy,
220+
suggest_to_outer: bool,
221+
) {
209222
if let InnerAttrPolicy::Forbidden(reason) = policy {
210223
let mut diag = match reason.as_ref().copied() {
211224
Some(InnerAttrForbiddenReason::AfterOuterDocComment { prev_doc_comment_span }) => {
@@ -239,6 +252,7 @@ impl<'a> Parser<'a> {
239252
&mut diag,
240253
attr_sp,
241254
OuterAttributeType::Attribute,
255+
suggest_to_outer,
242256
)
243257
.is_some()
244258
{

compiler/rustc_parse/src/parser/stmt.rs

+5
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,16 @@ impl<'a> Parser<'a> {
459459
pub fn parse_block(&mut self) -> PResult<'a, P<Block>> {
460460
let (attrs, block) = self.parse_inner_attrs_and_block()?;
461461
if let [.., last] = &*attrs {
462+
let suggest_to_outer = match &last.kind {
463+
ast::AttrKind::Normal(attr) => attr.item.is_valid_for_outer_style(),
464+
_ => false,
465+
};
462466
self.error_on_forbidden_inner_attr(
463467
last.span,
464468
super::attr::InnerAttrPolicy::Forbidden(Some(
465469
InnerAttrForbiddenReason::InCodeBlock,
466470
)),
471+
suggest_to_outer,
467472
);
468473
}
469474
Ok(block)

tests/ui/delegation/inner-attr.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ LL | fn main() {}
88
| ------------ the inner attribute doesn't annotate this function
99
|
1010
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
11-
help: to annotate the function, change the attribute from inner to outer style
12-
|
13-
LL - reuse a as b { #![rustc_dummy] self }
14-
LL + reuse a as b { #[rustc_dummy] self }
15-
|
1611

1712
error: aborting due to 1 previous error
1813

tests/ui/parser/attribute/attr-stmt-expr-attr-bad.stderr

-15
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,6 @@ LL | #[cfg(FALSE)] fn s() { #[attr] #![attr] foo!(); }
359359
| previous outer attribute
360360
|
361361
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
362-
help: to annotate the item macro invocation, change the attribute from inner to outer style
363-
|
364-
LL - #[cfg(FALSE)] fn s() { #[attr] #![attr] foo!(); }
365-
LL + #[cfg(FALSE)] fn s() { #[attr] #[attr] foo!(); }
366-
|
367362

368363
error: an inner attribute is not permitted following an outer attribute
369364
--> $DIR/attr-stmt-expr-attr-bad.rs:77:32
@@ -375,11 +370,6 @@ LL | #[cfg(FALSE)] fn s() { #[attr] #![attr] foo![]; }
375370
| previous outer attribute
376371
|
377372
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
378-
help: to annotate the item macro invocation, change the attribute from inner to outer style
379-
|
380-
LL - #[cfg(FALSE)] fn s() { #[attr] #![attr] foo![]; }
381-
LL + #[cfg(FALSE)] fn s() { #[attr] #[attr] foo![]; }
382-
|
383373

384374
error: an inner attribute is not permitted following an outer attribute
385375
--> $DIR/attr-stmt-expr-attr-bad.rs:79:32
@@ -391,11 +381,6 @@ LL | #[cfg(FALSE)] fn s() { #[attr] #![attr] foo!{}; }
391381
| previous outer attribute
392382
|
393383
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
394-
help: to annotate the item macro invocation, change the attribute from inner to outer style
395-
|
396-
LL - #[cfg(FALSE)] fn s() { #[attr] #![attr] foo!{}; }
397-
LL + #[cfg(FALSE)] fn s() { #[attr] #[attr] foo!{}; }
398-
|
399384

400385
error[E0586]: inclusive range with no end
401386
--> $DIR/attr-stmt-expr-attr-bad.rs:85:35

tests/ui/parser/attribute/attr.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ LL | fn foo() {}
77
| ----------- the inner attribute doesn't annotate this function
88
|
99
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
10-
help: to annotate the function, change the attribute from inner to outer style
11-
|
12-
LL - #![lang = "foo"]
13-
LL + #[lang = "foo"]
14-
|
1510

1611
error: aborting due to 1 previous error
1712

tests/ui/parser/inner-attr-after-doc-comment.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ LL | fn main() {}
1313
| ------------ the inner attribute doesn't annotate this function
1414
|
1515
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
16-
help: to annotate the function, change the attribute from inner to outer style
17-
|
18-
LL - #![recursion_limit="100"]
19-
LL + #[recursion_limit="100"]
20-
|
2116

2217
error: aborting due to 1 previous error
2318

tests/ui/parser/inner-attr.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ LL | fn main() {}
1010
| ------------ the inner attribute doesn't annotate this function
1111
|
1212
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
13-
help: to annotate the function, change the attribute from inner to outer style
14-
|
15-
LL - #![recursion_limit="100"]
16-
LL + #[recursion_limit="100"]
17-
|
1813

1914
error: aborting due to 1 previous error
2015

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![allow(dead_code)]
2+
fn foo() {}
3+
4+
#![feature(iter_array_chunks)] //~ ERROR an inner attribute is not permitted in this context
5+
fn bar() {}
6+
7+
fn main() {
8+
foo();
9+
bar();
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: an inner attribute is not permitted in this context
2+
--> $DIR/isgg-invalid-outer-attttr-issue-127930.rs:4:1
3+
|
4+
LL | #![feature(iter_array_chunks)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
LL | fn bar() {}
7+
| ----------- the inner attribute doesn't annotate this function
8+
|
9+
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
10+
11+
error: aborting due to 1 previous error
12+

tests/ui/parser/issues/issue-30318.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn foo() { }
66
//~^ ERROR expected outer doc comment
77
fn bar() { } //~ NOTE the inner doc comment doesn't annotate this function
88

9-
#[test] //~ ERROR an inner attribute is not permitted in this context
9+
#[cfg(test)] //~ ERROR an inner attribute is not permitted in this context
1010
fn baz() { } //~ NOTE the inner attribute doesn't annotate this function
1111
//~^^ NOTE inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually
1212

tests/ui/parser/issues/issue-30318.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn foo() { }
66
//~^ ERROR expected outer doc comment
77
fn bar() { } //~ NOTE the inner doc comment doesn't annotate this function
88

9-
#![test] //~ ERROR an inner attribute is not permitted in this context
9+
#![cfg(test)] //~ ERROR an inner attribute is not permitted in this context
1010
fn baz() { } //~ NOTE the inner attribute doesn't annotate this function
1111
//~^^ NOTE inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually
1212

tests/ui/parser/issues/issue-30318.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ LL | /// Misplaced comment...
1515
error: an inner attribute is not permitted in this context
1616
--> $DIR/issue-30318.rs:9:1
1717
|
18-
LL | #![test]
19-
| ^^^^^^^^
18+
LL | #![cfg(test)]
19+
| ^^^^^^^^^^^^^
2020
LL | fn baz() { }
2121
| ------------ the inner attribute doesn't annotate this function
2222
|
2323
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
2424
help: to annotate the function, change the attribute from inner to outer style
2525
|
26-
LL - #![test]
27-
LL + #[test]
26+
LL - #![cfg(test)]
27+
LL + #[cfg(test)]
2828
|
2929

3030
error[E0753]: expected outer doc comment

0 commit comments

Comments
 (0)