Skip to content

Commit 1bb4319

Browse files
authored
Rollup merge of #137622 - jdonszelmann:fix-137589, r=compiler-errors
fix attribute-related ICE when parsing macro on the rhs of a name-value attribute r? ``@oli-obk`` Closes: #137589
2 parents 0d4af08 + 4bf66c5 commit 1bb4319

8 files changed

+87
-6
lines changed

compiler/rustc_attr_parsing/src/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,10 @@ impl<'sess> AttributeParser<'sess> {
333333
{
334334
lit
335335
} else {
336-
let guar = self.dcx().has_errors().unwrap();
336+
let guar = self.dcx().span_delayed_bug(
337+
args.span().unwrap_or(DUMMY_SP),
338+
"expr in place where literal is expected (builtin attr parsing)",
339+
);
337340
ast::MetaItemLit {
338341
symbol: kw::Empty,
339342
suffix: None,

compiler/rustc_attr_parsing/src/parser.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_ast_pretty::pprust;
1313
use rustc_errors::DiagCtxtHandle;
1414
use rustc_hir::{self as hir, AttrPath};
1515
use rustc_span::symbol::{Ident, kw};
16-
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol};
16+
use rustc_span::{ErrorGuaranteed, Span, Symbol};
1717

1818
pub struct SegmentIterator<'a> {
1919
offset: usize,
@@ -127,7 +127,7 @@ impl<'a> ArgParser<'a> {
127127
}
128128
AttrArgs::Eq { eq_span, expr } => Self::NameValue(NameValueParser {
129129
eq_span: *eq_span,
130-
value: expr_to_lit(dcx, &expr),
130+
value: expr_to_lit(dcx, &expr, *eq_span),
131131
value_span: expr.span,
132132
}),
133133
}
@@ -348,16 +348,19 @@ impl NameValueParser {
348348
}
349349
}
350350

351-
fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr) -> MetaItemLit {
351+
fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr, span: Span) -> MetaItemLit {
352352
// In valid code the value always ends up as a single literal. Otherwise, a dummy
353353
// literal suffices because the error is handled elsewhere.
354354
if let ExprKind::Lit(token_lit) = expr.kind
355355
&& let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span)
356356
{
357357
lit
358358
} else {
359-
let guar = dcx.has_errors().unwrap();
360-
MetaItemLit { symbol: kw::Empty, suffix: None, kind: LitKind::Err(guar), span: DUMMY_SP }
359+
let guar = dcx.span_delayed_bug(
360+
span,
361+
"expr in place where literal is expected (builtin attr parsing)",
362+
);
363+
MetaItemLit { symbol: kw::Empty, suffix: None, kind: LitKind::Err(guar), span }
361364
}
362365
}
363366

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Tests for the issue in #137589
2+
#[crate_type = foo!()]
3+
//~^ ERROR cannot find macro `foo` in this scope
4+
5+
macro_rules! foo {} //~ ERROR unexpected end of macro invocation
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: unexpected end of macro invocation
2+
--> $DIR/crate-type-macro-empty.rs:5:1
3+
|
4+
LL | macro_rules! foo {}
5+
| ^^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments
6+
7+
error: cannot find macro `foo` in this scope
8+
--> $DIR/crate-type-macro-empty.rs:2:16
9+
|
10+
LL | #[crate_type = foo!()]
11+
| ^^^ consider moving the definition of `foo` before this call
12+
|
13+
note: a macro with the same name exists, but it appears later
14+
--> $DIR/crate-type-macro-empty.rs:5:14
15+
|
16+
LL | macro_rules! foo {}
17+
| ^^^
18+
19+
error: aborting due to 2 previous errors
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Tests for the issue in #137589
2+
3+
4+
macro_rules! foo {
5+
($x:expr) => {"rlib"}
6+
}
7+
8+
#[crate_type = foo!()] //~ ERROR unexpected end of macro invocation
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: unexpected end of macro invocation
2+
--> $DIR/crate-type-macro-not-crate.rs:8:16
3+
|
4+
LL | macro_rules! foo {
5+
| ---------------- when calling this macro
6+
...
7+
LL | #[crate_type = foo!()]
8+
| ^^^^^^ missing tokens in macro arguments
9+
|
10+
note: while trying to match meta-variable `$x:expr`
11+
--> $DIR/crate-type-macro-not-crate.rs:5:6
12+
|
13+
LL | ($x:expr) => {"rlib"}
14+
| ^^^^^^^
15+
16+
error: aborting due to 1 previous error
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Tests for the issue in #137589
2+
#[crate_type = foo!()] //~ ERROR cannot find macro `foo` in this scope
3+
4+
macro_rules! foo {
5+
($x:expr) => {"rlib"}
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: cannot find macro `foo` in this scope
2+
--> $DIR/crate-type-macro-not-found.rs:2:16
3+
|
4+
LL | #[crate_type = foo!()]
5+
| ^^^ consider moving the definition of `foo` before this call
6+
|
7+
note: a macro with the same name exists, but it appears later
8+
--> $DIR/crate-type-macro-not-found.rs:4:14
9+
|
10+
LL | macro_rules! foo {
11+
| ^^^
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)