diff --git a/compiler/rustc_attr_parsing/src/parser.rs b/compiler/rustc_attr_parsing/src/parser.rs index 973635f432e8..354fbab9cfcf 100644 --- a/compiler/rustc_attr_parsing/src/parser.rs +++ b/compiler/rustc_attr_parsing/src/parser.rs @@ -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. diff --git a/tests/ui/diagnostic_namespace/do_not_recommend/malformed-diagnostic-attributes-if-expression.rs b/tests/ui/diagnostic_namespace/do_not_recommend/malformed-diagnostic-attributes-if-expression.rs new file mode 100644 index 000000000000..3e00e42f9d9d --- /dev/null +++ b/tests/ui/diagnostic_namespace/do_not_recommend/malformed-diagnostic-attributes-if-expression.rs @@ -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() {} diff --git a/tests/ui/diagnostic_namespace/do_not_recommend/malformed-diagnostic-attributes-if-expression.stderr b/tests/ui/diagnostic_namespace/do_not_recommend/malformed-diagnostic-attributes-if-expression.stderr new file mode 100644 index 000000000000..d93a9f868a74 --- /dev/null +++ b/tests/ui/diagnostic_namespace/do_not_recommend/malformed-diagnostic-attributes-if-expression.stderr @@ -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 +