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
47 changes: 44 additions & 3 deletions compiler/rustc_attr_parsing/src/attributes/doc.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use rustc_ast::ast::{AttrStyle, LitKind, MetaItemLit};
use rustc_ast::ExprKind;
use rustc_ast::ast::{self, AttrArgs, AttrKind, AttrStyle, LitKind, MetaItemLit};
use rustc_attr_ir::target::Target;
use rustc_attr_ir::{
AttributeKind, CfgEntry, CfgHideShow, DocAttribute, DocCfgHideShow, DocCfgHideShowValue,
DocInline, HideOrShow,
};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, IndexEntry};
use rustc_errors::Applicability;
use rustc_errors::{Applicability, Diagnostic, MultiSpan};
use rustc_feature::AttributeStability;
use rustc_lint_defs::LintId;
use rustc_lint_defs::builtin::{INVALID_DOC_ATTRIBUTES, UNUSED_ATTRIBUTES};
use rustc_span::{Span, Symbol, edition, sym};

use super::prelude::{ALL_TARGETS, AllowedTargets};
use super::{AcceptMapping, AttributeParser, template};
use crate::EmitAttribute;
use crate::context::{AcceptContext, FinalizeContext};
use crate::diagnostics::{
AttrCrateLevelOnly, DocAliasBadChar, DocAliasDuplicated, DocAliasEmpty, DocAliasMalformed,
Expand All @@ -21,7 +24,7 @@ use crate::diagnostics::{
DocAutoCfgHideShowValuesMix, DocAutoCfgWrongLiteral, DocKeywordNotKeyword, DocTestLiteral,
DocTestTakesList, DocTestUnknown, DocUnknownAny, DocUnknownInclude, DocUnknownPasses,
DocUnknownPlugins, DocUnknownSpotlight, ExpectedNameValue, ExpectedNoArgs,
IllFormedAttributeInput, MalformedDoc, UnusedDuplicate,
IllFormedAttributeInput, InvalidExprInDocAttr, MalformedDoc, UnusedDuplicate,
};
use crate::parser::{
ArgParser, MetaItemListParser, MetaItemOrLitParser, MetaItemParser, OwnedPathParser,
Expand Down Expand Up @@ -827,3 +830,41 @@ impl AttributeParser for DocParser {
}
}
}

/// Is this a `#[doc = mac!()]`?
///
/// Or perhaps something as spicy as this?
/// ```ignore,_
/// #[doc = {
/// let a = 1;
/// let b = 1;
/// let sum = a + b;
/// assert_eq!(sum, 2);
/// }]
/// println!();
/// ```
pub(crate) fn lint_non_lit_doc_attr(
mut emit_lint: impl FnMut(LintId, MultiSpan, EmitAttribute),
attr: &ast::Attribute,
) -> bool {
if !attr.has_name(sym::doc) {
return false;
}
let AttrKind::Normal(n) = &attr.kind else { return false };
let AttrArgs::Eq { expr, .. } = &n.item.args else { return false };
if matches!(expr.kind, ExprKind::Lit(_)) {
return false;
};

let attr_span = attr.span;
let expr_span = expr.span;

emit_lint(
LintId::of(rustc_lint_defs::builtin::ILL_FORMED_ATTRIBUTE_INPUT),
attr_span.into(),
EmitAttribute(Box::new(move |dcx, level, _| {
InvalidExprInDocAttr { attr_span, expr_span }.into_diag(dcx, level)
})),
);
true
}
9 changes: 9 additions & 0 deletions compiler/rustc_attr_parsing/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,15 @@ pub(crate) enum InvalidTargetHelp {
UseRustcAlignStatic,
}

#[derive(Diagnostic)]
#[diag("invalid expression in `doc` attribute on macro invocation")]
pub(crate) struct InvalidExprInDocAttr {
#[primary_span]
pub expr_span: Span,
#[suggestion("remove the attribute", code = "", applicability = "machine-applicable")]
pub attr_span: Span,
}

#[derive(Diagnostic)]
#[diag("invalid alignment value: {$error_part}", code = E0589)]
pub(crate) struct InvalidAlignmentValue {
Expand Down
12 changes: 2 additions & 10 deletions compiler/rustc_attr_parsing/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rustc_session::Session;
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};

use crate::attributes::AttributeSafety;
use crate::attributes::doc::lint_non_lit_doc_attr;
use crate::context::{
ATTRIBUTE_PARSERS, AcceptContext, FinalizeCheckContext, FinalizeCheckFn, FinalizeContext,
FinalizeFn, FinalizeOutput, SharedContext,
Expand Down Expand Up @@ -332,17 +333,8 @@ 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) {
if target == Target::MacroCall && lint_non_lit_doc_attr(&mut emit_lint, attr) {
continue;
}

Expand Down
10 changes: 10 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,22 @@ fn main() {
#[deprecated = concat!("woah", "dude")]
//~^ ERROR attribute value must be a literal
#[doc = concat!("woah", "dude")]
//~^ ERROR invalid expression in `doc` attribute on macro invocation
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
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 invalid expression in `doc` attribute on macro invocation
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
unreachable!();
#[doc = {
let expressions @ r#in @ doc @ attributes @ at = home;
}]
//~^^^ ERROR invalid expression in `doc` attribute on macro invocation
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
unreachable!();
}
93 changes: 92 additions & 1 deletion tests/ui/attributes/attr-on-mac-call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,97 @@ 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: invalid expression in `doc` attribute on macro invocation
--> $DIR/attr-on-mac-call.rs:115:13
|
LL | #[doc = concat!("woah", "dude")]
| --------^^^^^^^^^^^^^^^^^^^^^^^- help: remove the attribute
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
= note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

error: invalid expression in `doc` attribute on macro invocation
--> $DIR/attr-on-mac-call.rs:119:13
|
LL | #[doc = {
| ______- ^
| | _____________|
LL | || let a = 1;
LL | || let b = 1;
LL | || let sum = a + b;
LL | || assert_eq!(sum, 2);
LL | || }]
| ||_____^- help: remove the attribute
| |_____|
|
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>

error: invalid expression in `doc` attribute on macro invocation
--> $DIR/attr-on-mac-call.rs:128:13
|
LL | #[doc = {
| ______- ^
| | _____________|
LL | || let expressions @ r#in @ doc @ attributes @ at = home;
LL | || }]
| ||_____^- help: remove the attribute
| |_____|
|
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>

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

For more information about this error, try `rustc --explain E0658`.
Future incompatibility report: Future breakage diagnostic:
error: invalid expression in `doc` attribute on macro invocation
--> $DIR/attr-on-mac-call.rs:115:13
|
LL | #[doc = concat!("woah", "dude")]
| --------^^^^^^^^^^^^^^^^^^^^^^^- help: remove the attribute
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
= note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

Future breakage diagnostic:
error: invalid expression in `doc` attribute on macro invocation
--> $DIR/attr-on-mac-call.rs:119:13
|
LL | #[doc = {
| ______- ^
| | _____________|
LL | || let a = 1;
LL | || let b = 1;
LL | || let sum = a + b;
LL | || assert_eq!(sum, 2);
LL | || }]
| ||_____^- help: remove the attribute
| |_____|
|
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
= note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

Future breakage diagnostic:
error: invalid expression in `doc` attribute on macro invocation
--> $DIR/attr-on-mac-call.rs:128:13
|
LL | #[doc = {
| ______- ^
| | _____________|
LL | || let expressions @ r#in @ doc @ attributes @ at = home;
LL | || }]
| ||_____^- help: remove the attribute
| |_____|
|
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
= note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

2 changes: 2 additions & 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,7 @@ fn main() {
assert_eq!(sum, 2);
}]
//~^^^^^^ ERROR: unused doc comment
//~| ERROR invalid expression in `doc` attribute on macro invocation
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
foo!();
}
41 changes: 40 additions & 1 deletion tests/ui/lint/unused/unused-doc-comments-for-macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ LL | | /// line3
|
= help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion

error: invalid expression in `doc` attribute on macro invocation
--> $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 | || }]
| ||_____^- help: remove the attribute
| |_____|
|
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
= note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

error: unused doc comment
--> $DIR/unused-doc-comments-for-macros.rs:19:5
|
Expand All @@ -40,5 +59,25 @@ 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

Future incompatibility report: Future breakage diagnostic:
error: invalid expression in `doc` attribute on macro invocation
--> $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 | || }]
| ||_____^- help: remove the attribute
| |_____|
|
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
= note: `#[deny(ill_formed_attribute_input)]` (part of `#[deny(future_incompatible)]`) on by default

Loading