Skip to content

Commit

Permalink
fix #137589
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Feb 25, 2025
1 parent ad27045 commit 3a3efc5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
16 changes: 11 additions & 5 deletions compiler/rustc_attr_parsing/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_ast_pretty::pprust;
use rustc_errors::DiagCtxtHandle;
use rustc_hir::{self as hir, AttrPath};
use rustc_span::symbol::{Ident, kw};
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol};
use rustc_span::{ErrorGuaranteed, Span, Symbol};

pub struct SegmentIterator<'a> {
offset: usize,
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<'a> ArgParser<'a> {
}
AttrArgs::Eq { eq_span, expr } => Self::NameValue(NameValueParser {
eq_span: *eq_span,
value: expr_to_lit(dcx, &expr),
value: expr_to_lit(dcx, &expr, value.span().unwrap()),
value_span: expr.span,
}),
}
Expand Down Expand Up @@ -348,16 +348,22 @@ impl NameValueParser {
}
}

fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr) -> MetaItemLit {
fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr, attr_span: Span) -> MetaItemLit {
// In valid code the value always ends up as a single literal. Otherwise, a dummy
// literal suffices because the error is handled elsewhere.
if let ExprKind::Lit(token_lit) = expr.kind
&& let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span)
{
lit
} else {
let guar = dcx.has_errors().unwrap();
MetaItemLit { symbol: kw::Empty, suffix: None, kind: LitKind::Err(guar), span: DUMMY_SP }
let mut err = dcx.struct_span_err(attr_span, "attribute value must be a literal");
if let ExprKind::Err(_) = expr.kind {
err.downgrade_to_delayed_bug();
}

let e = err.emit();

MetaItemLit { symbol: kw::Empty, suffix: None, kind: LitKind::Err(e), span: expr.span }
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/attributes/empty-expansion-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[crate_type = foo!()] //~ ERROR attribute value must be a literal
//~^ ERROR cannot find macro `foo` in this scope

macro_rules! foo {} //~ ERROR unexpected end of macro invocation


fn main() {}
26 changes: 26 additions & 0 deletions tests/ui/attributes/empty-expansion-macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: attribute value must be a literal
--> $DIR/empty-expansion-macro.rs:1:1
|
LL | #[crate_type = foo!()]
| ^^^^^^^^^^^^^^

error: unexpected end of macro invocation
--> $DIR/empty-expansion-macro.rs:4:1
|
LL | macro_rules! foo {}
| ^^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments

error: cannot find macro `foo` in this scope
--> $DIR/empty-expansion-macro.rs:1:16
|
LL | #[crate_type = foo!()]
| ^^^ consider moving the definition of `foo` before this call
|
note: a macro with the same name exists, but it appears later
--> $DIR/empty-expansion-macro.rs:4:14
|
LL | macro_rules! foo {}
| ^^^

error: aborting due to 3 previous errors

0 comments on commit 3a3efc5

Please sign in to comment.