Skip to content

Commit 634ea73

Browse files
Rollup merge of #152803 - TaKO8Ki:fix-152744-diagnostic-attr-ice, r=JonathanBrouwer
Avoid delayed-bug ICE for malformed diagnostic attrs Fixes #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.
2 parents da9292b + 61c9af2 commit 634ea73

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,13 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
522522
return self.parser.dcx().create_err(err);
523523
}
524524

525+
if let ShouldEmit::ErrorsAndLints { recovery: Recovery::Forbidden } = self.should_emit {
526+
// Do not attempt to suggest anything in `Recovery::Forbidden` mode.
527+
// Malformed diagnostic-attr arguments that start with an `if` expression can lead to
528+
// an ICE (https://github.com/rust-lang/rust/issues/152744), because callers may cancel the `InvalidMetaItem` error.
529+
return self.parser.dcx().create_err(err);
530+
}
531+
525532
// Suggest quoting idents, e.g. in `#[cfg(key = value)]`. We don't use `Token::ident` and
526533
// don't `uninterpolate` the token to avoid suggesting anything butchered or questionable
527534
// when macro metavariables are involved.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ check-pass
2+
//@ reference: attributes.diagnostic.do_not_recommend.syntax
3+
4+
trait Foo {}
5+
6+
#[diagnostic::do_not_recommend(if not_accepted)]
7+
//~^ WARNING `#[diagnostic::do_not_recommend]` does not expect any arguments
8+
impl Foo for () {}
9+
10+
fn main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
2+
--> $DIR/malformed-diagnostic-attributes-if-expression.rs:6:1
3+
|
4+
LL | #[diagnostic::do_not_recommend(if not_accepted)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(malformed_diagnostic_attributes)]` (part of `#[warn(unknown_or_malformed_diagnostic_attributes)]`) on by default
8+
9+
warning: 1 warning emitted
10+

0 commit comments

Comments
 (0)