Rollup merge of #152803 - TaKO8Ki:fix-152744-diagnostic-attr-ice, r=JonathanBrouwer

Avoid delayed-bug ICE for malformed diagnostic attrs

Fixes rust-lang/rust#152744

Skip suggestions in `expected_lit` when parsing with `Recovery::Forbidden`, since this may later cancel `InvalidMetaItem`. This avoids creating delayed bugs that can be promoted to ICE.
This commit is contained in:
Jonathan Brouwer 2026-02-18 18:55:22 +01:00 committed by GitHub
commit 634ea7379a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 0 deletions

View file

@ -522,6 +522,13 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
return self.parser.dcx().create_err(err);
}
if let ShouldEmit::ErrorsAndLints { recovery: Recovery::Forbidden } = self.should_emit {
// Do not attempt to suggest anything in `Recovery::Forbidden` mode.
// Malformed diagnostic-attr arguments that start with an `if` expression can lead to
// an ICE (https://github.com/rust-lang/rust/issues/152744), because callers may cancel the `InvalidMetaItem` error.
return self.parser.dcx().create_err(err);
}
// Suggest quoting idents, e.g. in `#[cfg(key = value)]`. We don't use `Token::ident` and
// don't `uninterpolate` the token to avoid suggesting anything butchered or questionable
// when macro metavariables are involved.

View file

@ -0,0 +1,10 @@
//@ check-pass
//@ reference: attributes.diagnostic.do_not_recommend.syntax
trait Foo {}
#[diagnostic::do_not_recommend(if not_accepted)]
//~^ WARNING `#[diagnostic::do_not_recommend]` does not expect any arguments
impl Foo for () {}
fn main() {}

View file

@ -0,0 +1,10 @@
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
--> $DIR/malformed-diagnostic-attributes-if-expression.rs:6:1
|
LL | #[diagnostic::do_not_recommend(if not_accepted)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
warning: 1 warning emitted