This commit is contained in:
Jana Dönszelmann 2025-02-25 16:53:10 +01:00
parent ad27045c31
commit 4bf66c57fa
No known key found for this signature in database
8 changed files with 87 additions and 6 deletions

View file

@ -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,

View file

@ -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,
@ -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,
}),
}
@ -348,7 +348,7 @@ 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
@ -356,8 +356,11 @@ fn expr_to_lit(dcx: DiagCtxtHandle<'_>, expr: &Expr) -> MetaItemLit {
{
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 }
}
}

View file

@ -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() {}

View file

@ -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

View file

@ -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() {}

View file

@ -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

View file

@ -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() {}

View file

@ -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