Skip to content

Commit

Permalink
don't suggest turning crate-level attributes into outer style
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Aug 3, 2024
1 parent 70591dc commit 22aa104
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 53 deletions.
11 changes: 11 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2898,6 +2898,17 @@ pub struct AttrItem {
pub tokens: Option<LazyAttrTokenStream>,
}

impl AttrItem {
pub fn is_valid_for_outer_style(&self) -> bool {
self.path == sym::cfg_attr
|| self.path == sym::cfg
|| self.path == sym::forbid
|| self.path == sym::warn
|| self.path == sym::allow
|| self.path == sym::deny
}
}

/// `TraitRef`s appear in impls.
///
/// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all
Expand Down
38 changes: 26 additions & 12 deletions compiler/rustc_parse/src/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl<'a> Parser<'a> {
token::CommentKind::Line => OuterAttributeType::DocComment,
token::CommentKind::Block => OuterAttributeType::DocBlockComment,
},
true,
) {
err.note(fluent::parse_note);
err.span_suggestion_verbose(
Expand Down Expand Up @@ -130,7 +131,11 @@ impl<'a> Parser<'a> {

// Emit error if inner attribute is encountered and forbidden.
if style == ast::AttrStyle::Inner {
this.error_on_forbidden_inner_attr(attr_sp, inner_parse_policy);
this.error_on_forbidden_inner_attr(
attr_sp,
inner_parse_policy,
item.is_valid_for_outer_style(),
);
}

Ok(attr::mk_attr_from_item(&self.psess.attr_id_generator, item, None, style, attr_sp))
Expand All @@ -142,6 +147,7 @@ impl<'a> Parser<'a> {
err: &mut Diag<'_>,
span: Span,
attr_type: OuterAttributeType,
suggest_to_outer: bool,
) -> Option<Span> {
let mut snapshot = self.create_snapshot_for_diagnostic();
let lo = span.lo()
Expand Down Expand Up @@ -176,16 +182,18 @@ impl<'a> Parser<'a> {
// FIXME(#100717)
err.arg("item", item.kind.descr());
err.span_label(item.span, fluent::parse_label_does_not_annotate_this);
err.span_suggestion_verbose(
replacement_span,
fluent::parse_sugg_change_inner_to_outer,
match attr_type {
OuterAttributeType::Attribute => "",
OuterAttributeType::DocBlockComment => "*",
OuterAttributeType::DocComment => "/",
},
rustc_errors::Applicability::MachineApplicable,
);
if suggest_to_outer {
err.span_suggestion_verbose(
replacement_span,
fluent::parse_sugg_change_inner_to_outer,
match attr_type {
OuterAttributeType::Attribute => "",
OuterAttributeType::DocBlockComment => "*",
OuterAttributeType::DocComment => "/",
},
rustc_errors::Applicability::MachineApplicable,
);
}
return None;
}
Err(item_err) => {
Expand All @@ -196,7 +204,12 @@ impl<'a> Parser<'a> {
Some(replacement_span)
}

pub(super) fn error_on_forbidden_inner_attr(&self, attr_sp: Span, policy: InnerAttrPolicy) {
pub(super) fn error_on_forbidden_inner_attr(
&self,
attr_sp: Span,
policy: InnerAttrPolicy,
suggest_to_outer: bool,
) {
if let InnerAttrPolicy::Forbidden(reason) = policy {
let mut diag = match reason.as_ref().copied() {
Some(InnerAttrForbiddenReason::AfterOuterDocComment { prev_doc_comment_span }) => {
Expand Down Expand Up @@ -230,6 +243,7 @@ impl<'a> Parser<'a> {
&mut diag,
attr_sp,
OuterAttributeType::Attribute,
suggest_to_outer,
)
.is_some()
{
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,16 @@ impl<'a> Parser<'a> {
pub fn parse_block(&mut self) -> PResult<'a, P<Block>> {
let (attrs, block) = self.parse_inner_attrs_and_block()?;
if let [.., last] = &*attrs {
let suggest_to_outer = match &last.kind {
ast::AttrKind::Normal(attr) => attr.item.is_valid_for_outer_style(),
_ => false,
};
self.error_on_forbidden_inner_attr(
last.span,
super::attr::InnerAttrPolicy::Forbidden(Some(
InnerAttrForbiddenReason::InCodeBlock,
)),
suggest_to_outer,
);
}
Ok(block)
Expand Down
5 changes: 0 additions & 5 deletions tests/ui/delegation/inner-attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ LL | fn main() {}
| ------------ the inner attribute doesn't annotate this function
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
help: to annotate the function, change the attribute from inner to outer style
|
LL - reuse a as b { #![rustc_dummy] self }
LL + reuse a as b { #[rustc_dummy] self }
|

error: aborting due to 1 previous error

15 changes: 0 additions & 15 deletions tests/ui/parser/attribute/attr-stmt-expr-attr-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,6 @@ LL | #[cfg(FALSE)] fn s() { #[attr] #![attr] foo!(); }
| previous outer attribute
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
help: to annotate the item macro invocation, change the attribute from inner to outer style
|
LL - #[cfg(FALSE)] fn s() { #[attr] #![attr] foo!(); }
LL + #[cfg(FALSE)] fn s() { #[attr] #[attr] foo!(); }
|

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

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

error[E0586]: inclusive range with no end
--> $DIR/attr-stmt-expr-attr-bad.rs:85:35
Expand Down
5 changes: 0 additions & 5 deletions tests/ui/parser/attribute/attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ LL | fn foo() {}
| ----------- the inner attribute doesn't annotate this function
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
help: to annotate the function, change the attribute from inner to outer style
|
LL - #![lang = "foo"]
LL + #[lang = "foo"]
|

error: aborting due to 1 previous error

5 changes: 0 additions & 5 deletions tests/ui/parser/inner-attr-after-doc-comment.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ LL | fn main() {}
| ------------ the inner attribute doesn't annotate this function
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
help: to annotate the function, change the attribute from inner to outer style
|
LL - #![recursion_limit="100"]
LL + #[recursion_limit="100"]
|

error: aborting due to 1 previous error

5 changes: 0 additions & 5 deletions tests/ui/parser/inner-attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ LL | fn main() {}
| ------------ the inner attribute doesn't annotate this function
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
help: to annotate the function, change the attribute from inner to outer style
|
LL - #![recursion_limit="100"]
LL + #[recursion_limit="100"]
|

error: aborting due to 1 previous error

10 changes: 10 additions & 0 deletions tests/ui/parser/issues/isgg-invalid-outer-attttr-issue-127930.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![allow(dead_code)]
fn foo() {}

#![feature(iter_array_chunks)] //~ ERROR an inner attribute is not permitted in this context
fn bar() {}

fn main() {
foo();
bar();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: an inner attribute is not permitted in this context
--> $DIR/isgg-invalid-outer-attttr-issue-127930.rs:4:1
|
LL | #![feature(iter_array_chunks)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | fn bar() {}
| ----------- the inner attribute doesn't annotate this function
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files

error: aborting due to 1 previous error

2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-30318.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn foo() { }
//~^ ERROR expected outer doc comment
fn bar() { } //~ NOTE the inner doc comment doesn't annotate this function

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/issues/issue-30318.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn foo() { }
//~^ ERROR expected outer doc comment
fn bar() { } //~ NOTE the inner doc comment doesn't annotate this function

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

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/parser/issues/issue-30318.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ LL | /// Misplaced comment...
error: an inner attribute is not permitted in this context
--> $DIR/issue-30318.rs:9:1
|
LL | #![test]
| ^^^^^^^^
LL | #![cfg(test)]
| ^^^^^^^^^^^^^
LL | fn baz() { }
| ------------ the inner attribute doesn't annotate this function
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
help: to annotate the function, change the attribute from inner to outer style
|
LL - #![test]
LL + #[test]
LL - #![cfg(test)]
LL + #[cfg(test)]
|

error[E0753]: expected outer doc comment
Expand Down

0 comments on commit 22aa104

Please sign in to comment.