Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions compiler/rustc_attr_parsing/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,20 +334,6 @@ impl<'sess> AttributeParser<'sess> {
}
}

fn is_doc_non_lit_expr(attr: &ast::Attribute) -> bool {
if !attr.has_name(sym::doc) {
return false;
}
let ast::AttrKind::Normal(n) = &attr.kind else { return false };
let ast::AttrArgs::Eq { expr, .. } = &n.item.args else { return false };
!matches!(expr.kind, ast::ExprKind::Lit(_))
}

// FIXME accidentally allowed on Stable Rust
if target == Target::MacroCall && is_doc_non_lit_expr(attr) {
continue;
}

let attr_span = lower_span(attr.span);
match &attr.kind {
ast::AttrKind::DocComment(comment_kind, symbol) => {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/attributes/attr-on-mac-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,19 @@ fn main() {
#[deprecated = concat!("woah", "dude")]
//~^ ERROR attribute value must be a literal
#[doc = concat!("woah", "dude")]
//~^ ERROR attribute value must be a literal
unreachable!();
#[doc = {
let a = 1;
let b = 1;
let sum = a + b;
assert_eq!(sum, 2);

@JonathanBrouwer JonathanBrouwer Aug 21, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add an example without a macro call in it here? That is a stronger regression test

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

}]
//~^^^^^^ ERROR attribute value must be a literal
unreachable!();
#[doc = {
let expressions @ r#in @ doc @ attributes @ at = home;
}]
//~^^^ ERROR attribute value must be a literal
unreachable!();
}
29 changes: 28 additions & 1 deletion tests/ui/attributes/attr-on-mac-call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ error: attribute value must be a literal
LL | #[deprecated = concat!("woah", "dude")]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: attribute value must be a literal
--> $DIR/attr-on-mac-call.rs:115:13
|
LL | #[doc = concat!("woah", "dude")]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: attribute value must be a literal
--> $DIR/attr-on-mac-call.rs:118:13
|
LL | #[doc = {
| _____________^
LL | | let a = 1;
LL | | let b = 1;
LL | | let sum = a + b;
LL | | assert_eq!(sum, 2);
LL | | }]
| |_____^

error: attribute value must be a literal
--> $DIR/attr-on-mac-call.rs:126:13
|
LL | #[doc = {
| _____________^
LL | | let expressions @ r#in @ doc @ attributes @ at = home;
LL | | }]
| |_____^

warning: the `export_name` attribute cannot be used on macro calls
--> $DIR/attr-on-mac-call.rs:8:7
|
Expand Down Expand Up @@ -347,6 +374,6 @@ LL | #[repr(Rust)]
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: placing this attribute on a macro invocation does nothing even if the macro expands to what would be a valid target for the attribute

error: aborting due to 5 previous errors; 30 warnings emitted
error: aborting due to 8 previous errors; 30 warnings emitted

For more information about this error, try `rustc --explain E0658`.
1 change: 1 addition & 0 deletions tests/ui/lint/unused/unused-doc-comments-for-macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ fn main() {
assert_eq!(sum, 2);
}]
//~^^^^^^ ERROR: unused doc comment
//~| ERROR attribute value must be a literal
foo!();
}
14 changes: 13 additions & 1 deletion tests/ui/lint/unused/unused-doc-comments-for-macros.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
error: attribute value must be a literal
--> $DIR/unused-doc-comments-for-macros.rs:19:13
|
LL | #[doc = {
| _____________^
LL | | let a = 1;
LL | | let b = 1;
LL | | let sum = a + b;
LL | | assert_eq!(sum, 2);
LL | | }]
| |_____^

error: unused doc comment
--> $DIR/unused-doc-comments-for-macros.rs:7:5
|
Expand Down Expand Up @@ -40,5 +52,5 @@ LL | | }]
|
= help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion

error: aborting due to 3 previous errors
error: aborting due to 4 previous errors

Loading