Skip to content

Commit 9a7d66d

Browse files
committed
fix ICE on stable related to attrs on macros
1 parent 7f9f26b commit 9a7d66d

File tree

7 files changed

+58
-30
lines changed

7 files changed

+58
-30
lines changed

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl Stage for Early {
263263
sess: &'sess Session,
264264
diag: impl for<'x> Diagnostic<'x>,
265265
) -> ErrorGuaranteed {
266-
self.should_emit().emit_err_or_delay(sess.dcx().create_err(diag))
266+
self.should_emit().emit_err(sess.dcx().create_err(diag))
267267
}
268268

269269
fn should_emit(&self) -> ShouldEmit {
@@ -665,29 +665,12 @@ pub enum ShouldEmit {
665665
}
666666

667667
impl ShouldEmit {
668-
pub(crate) fn emit_err_or_delay(&self, diag: Diag<'_>) -> ErrorGuaranteed {
668+
pub(crate) fn emit_err(&self, diag: Diag<'_>) -> ErrorGuaranteed {
669669
match self {
670670
ShouldEmit::EarlyFatal if diag.level() == Level::DelayedBug => diag.emit(),
671671
ShouldEmit::EarlyFatal => diag.upgrade_to_fatal().emit(),
672672
ShouldEmit::ErrorsAndLints => diag.emit(),
673673
ShouldEmit::Nothing => diag.delay_as_bug(),
674674
}
675675
}
676-
677-
pub(crate) fn maybe_emit_err(&self, diag: Diag<'_>) {
678-
match self {
679-
ShouldEmit::EarlyFatal if diag.level() == Level::DelayedBug => {
680-
diag.emit();
681-
}
682-
ShouldEmit::EarlyFatal => {
683-
diag.upgrade_to_fatal().emit();
684-
}
685-
ShouldEmit::ErrorsAndLints => {
686-
diag.emit();
687-
}
688-
ShouldEmit::Nothing => {
689-
diag.cancel();
690-
}
691-
}
692-
}
693676
}

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,14 @@ fn expr_to_lit(
328328
match res {
329329
Ok(lit) => {
330330
if token_lit.suffix.is_some() {
331-
should_emit.emit_err_or_delay(
331+
should_emit.emit_err(
332332
psess.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span }),
333333
);
334334
None
335335
} else {
336336
if !lit.kind.is_unsuffixed() {
337337
// Emit error and continue, we can still parse the attribute as if the suffix isn't there
338-
should_emit.maybe_emit_err(
338+
should_emit.emit_err(
339339
psess.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span }),
340340
);
341341
}
@@ -355,19 +355,19 @@ fn expr_to_lit(
355355
}
356356
}
357357
} else {
358+
if matches!(should_emit, ShouldEmit::Nothing) {
359+
return None;
360+
}
361+
358362
// Example cases:
359363
// - `#[foo = 1+1]`: results in `ast::ExprKind::BinOp`.
360364
// - `#[foo = include_str!("nonexistent-file.rs")]`:
361365
// results in `ast::ExprKind::Err`. In that case we delay
362366
// the error because an earlier error will have already
363367
// been reported.
364368
let msg = "attribute value must be a literal";
365-
let mut err = psess.dcx().struct_span_err(span, msg);
366-
if let ExprKind::Err(_) = expr.kind {
367-
err.downgrade_to_delayed_bug();
368-
}
369-
370-
should_emit.emit_err_or_delay(err);
369+
let err = psess.dcx().struct_span_err(span, msg);
370+
should_emit.emit_err(err);
371371
None
372372
}
373373
}
@@ -400,7 +400,7 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
400400

401401
if !lit.kind.is_unsuffixed() {
402402
// Emit error and continue, we can still parse the attribute as if the suffix isn't there
403-
self.should_emit.maybe_emit_err(
403+
self.should_emit.emit_err(
404404
self.parser.dcx().create_err(SuffixedLiteralInAttribute { span: lit.span }),
405405
);
406406
}
@@ -542,7 +542,7 @@ impl<'a> MetaItemListParser<'a> {
542542
) {
543543
Ok(s) => Some(s),
544544
Err(e) => {
545-
should_emit.emit_err_or_delay(e);
545+
should_emit.emit_err(e);
546546
None
547547
}
548548
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ check-pass
2+
#[deprecated = concat !()]
3+
macro_rules! a {
4+
() => {};
5+
}
6+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![deny(unused)]
2+
3+
#[crate_name = concat !()]
4+
//~^ ERROR crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]
5+
macro_rules! a {
6+
//~^ ERROR unused macro definition
7+
() => {};
8+
}
9+
fn main() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: unused macro definition: `a`
2+
--> $DIR/concat-in-crate-name-issue-137687.rs:5:14
3+
|
4+
LL | macro_rules! a {
5+
| ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/concat-in-crate-name-issue-137687.rs:1:9
9+
|
10+
LL | #![deny(unused)]
11+
| ^^^^^^
12+
= note: `#[deny(unused_macros)]` implied by `#[deny(unused)]`
13+
14+
error: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
15+
--> $DIR/concat-in-crate-name-issue-137687.rs:3:1
16+
|
17+
LL | #[crate_name = concat !()]
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
= note: `#[deny(unused_attributes)]` implied by `#[deny(unused)]`
21+
22+
error: aborting due to 2 previous errors
23+

tests/ui/resolve/path-attr-in-const-block.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ fn main() {
55
const {
66
#![path = foo!()]
77
//~^ ERROR: cannot find macro `foo` in this scope
8+
//~| ERROR: attribute value must be a literal
89
}
910
}

tests/ui/resolve/path-attr-in-const-block.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@ error: cannot find macro `foo` in this scope
44
LL | #![path = foo!()]
55
| ^^^
66

7-
error: aborting due to 1 previous error
7+
error: attribute value must be a literal
8+
--> $DIR/path-attr-in-const-block.rs:6:19
9+
|
10+
LL | #![path = foo!()]
11+
| ^^^^^^
12+
13+
error: aborting due to 2 previous errors
814

0 commit comments

Comments
 (0)