Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix attribute-related ICE when parsing macro on the rhs of a name-value attribute #137622

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,10 @@ impl<'sess> AttributeParser<'sess> {
{
lit
} else {
let guar = self.dcx().has_errors().unwrap();
let guar = self.dcx().span_delayed_bug(
args.span().unwrap_or(DUMMY_SP),
"expr in place where literal is expected (builtin attr parsing)",
);
ast::MetaItemLit {
symbol: kw::Empty,
suffix: None,
Expand Down
13 changes: 8 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, *eq_span),
value_span: expr.span,
}),
}
Expand Down Expand Up @@ -348,16 +348,19 @@ impl NameValueParser {
}
}

fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr) -> MetaItemLit {
fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr, 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 guar = dcx.span_delayed_bug(
span,
"expr in place where literal is expected (builtin attr parsing)",
);
MetaItemLit { symbol: kw::Empty, suffix: None, kind: LitKind::Err(guar), span }
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/attributes/crate-type-macro-empty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Tests for the issue in #137589
#[crate_type = foo!()]
//~^ ERROR cannot find macro `foo` in this scope

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

fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/attributes/crate-type-macro-empty.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: unexpected end of macro invocation
--> $DIR/crate-type-macro-empty.rs:5:1
|
LL | macro_rules! foo {}
| ^^^^^^^^^^^^^^^^^^^ missing tokens in macro arguments

error: cannot find macro `foo` in this scope
--> $DIR/crate-type-macro-empty.rs:2: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/crate-type-macro-empty.rs:5:14
|
LL | macro_rules! foo {}
| ^^^

error: aborting due to 2 previous errors

9 changes: 9 additions & 0 deletions tests/ui/attributes/crate-type-macro-not-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Tests for the issue in #137589


macro_rules! foo {
($x:expr) => {"rlib"}
}

#[crate_type = foo!()] //~ ERROR unexpected end of macro invocation
fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/attributes/crate-type-macro-not-crate.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: unexpected end of macro invocation
--> $DIR/crate-type-macro-not-crate.rs:8:16
|
LL | macro_rules! foo {
| ---------------- when calling this macro
...
LL | #[crate_type = foo!()]
| ^^^^^^ missing tokens in macro arguments
|
note: while trying to match meta-variable `$x:expr`
--> $DIR/crate-type-macro-not-crate.rs:5:6
|
LL | ($x:expr) => {"rlib"}
| ^^^^^^^

error: aborting due to 1 previous error

8 changes: 8 additions & 0 deletions tests/ui/attributes/crate-type-macro-not-found.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Tests for the issue in #137589
#[crate_type = foo!()] //~ ERROR cannot find macro `foo` in this scope

macro_rules! foo {
($x:expr) => {"rlib"}
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/attributes/crate-type-macro-not-found.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: cannot find macro `foo` in this scope
--> $DIR/crate-type-macro-not-found.rs:2: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/crate-type-macro-not-found.rs:4:14
|
LL | macro_rules! foo {
| ^^^

error: aborting due to 1 previous error

Loading