diff options
| author | Jana Dönszelmann <jana@donsz.nl> | 2025-08-23 19:42:14 +0200 |
|---|---|---|
| committer | Jana Dönszelmann <jana@donsz.nl> | 2025-08-24 09:20:57 +0200 |
| commit | 48a4e2d2dde8d68e1d00d3eac07b2c6155f3239d (patch) | |
| tree | 21e4c65b03513947890bc1bd38947a7c0ba75f68 | |
| parent | 59ceb02d65f13a20d29422f4d923ecde429d4c0c (diff) | |
| download | rust-48a4e2d2dde8d68e1d00d3eac07b2c6155f3239d.tar.gz rust-48a4e2d2dde8d68e1d00d3eac07b2c6155f3239d.zip | |
fix ICE on stable related to attrs on macros
7 files changed, 58 insertions, 30 deletions
diff --git a/compiler/rustc_attr_parsing/src/context.rs b/compiler/rustc_attr_parsing/src/context.rs index 128ecd67d2a..d4b9cfe00ad 100644 --- a/compiler/rustc_attr_parsing/src/context.rs +++ b/compiler/rustc_attr_parsing/src/context.rs @@ -265,7 +265,7 @@ impl Stage for Early { sess: &'sess Session, diag: impl for<'x> Diagnostic<'x>, ) -> ErrorGuaranteed { - self.should_emit().emit_err_or_delay(sess.dcx().create_err(diag)) + self.should_emit().emit_err(sess.dcx().create_err(diag)) } fn should_emit(&self) -> ShouldEmit { @@ -667,7 +667,7 @@ pub enum ShouldEmit { } impl ShouldEmit { - pub(crate) fn emit_err_or_delay(&self, diag: Diag<'_>) -> ErrorGuaranteed { + pub(crate) fn emit_err(&self, diag: Diag<'_>) -> ErrorGuaranteed { match self { ShouldEmit::EarlyFatal if diag.level() == Level::DelayedBug => diag.emit(), ShouldEmit::EarlyFatal => diag.upgrade_to_fatal().emit(), @@ -675,21 +675,4 @@ impl ShouldEmit { ShouldEmit::Nothing => diag.delay_as_bug(), } } - - pub(crate) fn maybe_emit_err(&self, diag: Diag<'_>) { - match self { - ShouldEmit::EarlyFatal if diag.level() == Level::DelayedBug => { - diag.emit(); - } - ShouldEmit::EarlyFatal => { - diag.upgrade_to_fatal().emit(); - } - ShouldEmit::ErrorsAndLints => { - diag.emit(); - } - ShouldEmit::Nothing => { - diag.cancel(); - } - } - } } diff --git a/compiler/rustc_attr_parsing/src/parser.rs b/compiler/rustc_attr_parsing/src/parser.rs index 3ba188938c6..6d3cf684296 100644 --- a/compiler/rustc_attr_parsing/src/parser.rs +++ b/compiler/rustc_attr_parsing/src/parser.rs @@ -328,14 +328,14 @@ fn expr_to_lit( match res { Ok(lit) => { if token_lit.suffix.is_some() { - should_emit.emit_err_or_delay( + should_emit.emit_err( psess.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span }), ); None } else { if !lit.kind.is_unsuffixed() { // Emit error and continue, we can still parse the attribute as if the suffix isn't there - should_emit.maybe_emit_err( + should_emit.emit_err( psess.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span }), ); } @@ -355,6 +355,10 @@ fn expr_to_lit( } } } else { + if matches!(should_emit, ShouldEmit::Nothing) { + return None; + } + // Example cases: // - `#[foo = 1+1]`: results in `ast::ExprKind::BinOp`. // - `#[foo = include_str!("nonexistent-file.rs")]`: @@ -362,12 +366,8 @@ fn expr_to_lit( // the error because an earlier error will have already // been reported. let msg = "attribute value must be a literal"; - let mut err = psess.dcx().struct_span_err(span, msg); - if let ExprKind::Err(_) = expr.kind { - err.downgrade_to_delayed_bug(); - } - - should_emit.emit_err_or_delay(err); + let err = psess.dcx().struct_span_err(span, msg); + should_emit.emit_err(err); None } } @@ -400,7 +400,7 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> { if !lit.kind.is_unsuffixed() { // Emit error and continue, we can still parse the attribute as if the suffix isn't there - self.should_emit.maybe_emit_err( + self.should_emit.emit_err( self.parser.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span }), ); } @@ -542,7 +542,7 @@ impl<'a> MetaItemListParser<'a> { ) { Ok(s) => Some(s), Err(e) => { - should_emit.emit_err_or_delay(e); + should_emit.emit_err(e); None } } diff --git a/tests/ui/lint/unused/concat-in-crate-deprecated-issue-137687.rs b/tests/ui/lint/unused/concat-in-crate-deprecated-issue-137687.rs new file mode 100644 index 00000000000..22dd55f4421 --- /dev/null +++ b/tests/ui/lint/unused/concat-in-crate-deprecated-issue-137687.rs @@ -0,0 +1,6 @@ +//@ check-pass +#[deprecated = concat !()] +macro_rules! a { + () => {}; +} +fn main() {} diff --git a/tests/ui/lint/unused/concat-in-crate-name-issue-137687.rs b/tests/ui/lint/unused/concat-in-crate-name-issue-137687.rs new file mode 100644 index 00000000000..37fbf93ffa1 --- /dev/null +++ b/tests/ui/lint/unused/concat-in-crate-name-issue-137687.rs @@ -0,0 +1,9 @@ +#![deny(unused)] + +#[crate_name = concat !()] +//~^ ERROR crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo] +macro_rules! a { + //~^ ERROR unused macro definition + () => {}; +} +fn main() {} diff --git a/tests/ui/lint/unused/concat-in-crate-name-issue-137687.stderr b/tests/ui/lint/unused/concat-in-crate-name-issue-137687.stderr new file mode 100644 index 00000000000..4ffb55d493a --- /dev/null +++ b/tests/ui/lint/unused/concat-in-crate-name-issue-137687.stderr @@ -0,0 +1,23 @@ +error: unused macro definition: `a` + --> $DIR/concat-in-crate-name-issue-137687.rs:5:14 + | +LL | macro_rules! a { + | ^ + | +note: the lint level is defined here + --> $DIR/concat-in-crate-name-issue-137687.rs:1:9 + | +LL | #![deny(unused)] + | ^^^^^^ + = note: `#[deny(unused_macros)]` implied by `#[deny(unused)]` + +error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]` + --> $DIR/concat-in-crate-name-issue-137687.rs:3:1 + | +LL | #[crate_name = concat !()] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[deny(unused_attributes)]` implied by `#[deny(unused)]` + +error: aborting due to 2 previous errors + diff --git a/tests/ui/resolve/path-attr-in-const-block.rs b/tests/ui/resolve/path-attr-in-const-block.rs index 076511d26d6..0ca356b1ddf 100644 --- a/tests/ui/resolve/path-attr-in-const-block.rs +++ b/tests/ui/resolve/path-attr-in-const-block.rs @@ -5,5 +5,6 @@ fn main() { const { #![path = foo!()] //~^ ERROR: cannot find macro `foo` in this scope + //~| ERROR: attribute value must be a literal } } diff --git a/tests/ui/resolve/path-attr-in-const-block.stderr b/tests/ui/resolve/path-attr-in-const-block.stderr index 8f9e58157c8..19d2745577b 100644 --- a/tests/ui/resolve/path-attr-in-const-block.stderr +++ b/tests/ui/resolve/path-attr-in-const-block.stderr @@ -4,5 +4,11 @@ error: cannot find macro `foo` in this scope LL | #![path = foo!()] | ^^^ -error: aborting due to 1 previous error +error: attribute value must be a literal + --> $DIR/path-attr-in-const-block.rs:6:19 + | +LL | #![path = foo!()] + | ^^^^^^ + +error: aborting due to 2 previous errors |
