Skip to content

Commit 819c626

Browse files
committed
fix #137589
1 parent ad27045 commit 819c626

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

compiler/rustc_attr_parsing/src/parser.rs

+11-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, value.span().unwrap()),
131131
value_span: expr.span,
132132
}),
133133
}
@@ -348,16 +348,22 @@ impl NameValueParser {
348348
}
349349
}
350350

351-
fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr) -> MetaItemLit {
351+
fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr, attr_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 mut err = dcx.struct_span_err(attr_span, "attribute value must be a literal");
360+
if let ExprKind::Err(_) = expr.kind {
361+
err.downgrade_to_delayed_bug();
362+
}
363+
364+
let e = err.emit();
365+
366+
MetaItemLit { symbol: kw::Empty, suffix: None, kind: LitKind::Err(e), span: expr.span }
361367
}
362368
}
363369

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Tests for the issue in #137589
2+
3+
4+
#[crate_type = foo!()] //~ ERROR attribute value must be a literal
5+
//~^ ERROR cannot find macro `foo` in this scope
6+
7+
macro_rules! foo {} //~ ERROR unexpected end of macro invocation
8+
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: attribute value must be a literal
2+
--> $DIR/empty-expansion-macro.rs:1:1
3+
|
4+
LL | #[crate_type = foo!()]
5+
| ^^^^^^^^^^^^^^
6+
7+
error: unexpected end of macro invocation
8+
--> $DIR/empty-expansion-macro.rs:4:1
9+
|
10+
LL | macro_rules! foo {}
11+
| ^^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments
12+
13+
error: cannot find macro `foo` in this scope
14+
--> $DIR/empty-expansion-macro.rs:1:16
15+
|
16+
LL | #[crate_type = foo!()]
17+
| ^^^ consider moving the definition of `foo` before this call
18+
|
19+
note: a macro with the same name exists, but it appears later
20+
--> $DIR/empty-expansion-macro.rs:4:14
21+
|
22+
LL | macro_rules! foo {}
23+
| ^^^
24+
25+
error: aborting due to 3 previous errors
26+

0 commit comments

Comments
 (0)