Rollup merge of #146308 - cyrgani:concat-integer-literals, r=jackh726

support integer literals in `${concat()}`

Tracking issue: rust-lang/rust#124225

Adds support for using integer literals as arguments to `${concat()}` macro expressions.
Integer formatting such as `1_000` is preserved by this.
This commit is contained in:
Stuart Cook
2025-09-12 20:02:11 +10:00
committed by GitHub
4 changed files with 64 additions and 26 deletions

View File

@@ -940,11 +940,27 @@ fn extract_symbol_from_pnr<'a>(
{
Ok(*symbol)
}
ParseNtResult::Literal(expr)
if let ExprKind::Lit(lit @ Lit { kind: LitKind::Integer, symbol, suffix }) =
&expr.kind =>
{
if lit.is_semantic_float() {
Err(dcx
.struct_err("floats are not supported as metavariables of `${concat(..)}`")
.with_span(span_err))
} else if suffix.is_none() {
Ok(*symbol)
} else {
Err(dcx
.struct_err("integer metavariables of `${concat(..)}` must not be suffixed")
.with_span(span_err))
}
}
_ => Err(dcx
.struct_err(
"metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`",
)
.with_note("currently only string literals are supported")
.with_note("currently only string and integer literals are supported")
.with_span(span_err)),
}
}

View File

@@ -92,6 +92,12 @@ macro_rules! combinations {
}};
}
macro_rules! int_struct {
($n: literal) => {
struct ${concat(E, $n)};
}
}
fn main() {
create_things!(behold);
behold_separated_idents_in_a_fn();
@@ -112,4 +118,16 @@ fn main() {
assert_eq!(VAR_123, 2);
combinations!(_hello, "a", b, "b");
int_struct!(1_0);
int_struct!(2);
int_struct!(3___0);
int_struct!(7_);
int_struct!(08);
let _ = E1_0;
let _ = E2;
let _ = E3___0;
let _ = E7_;
let _ = E08;
}

View File

@@ -140,7 +140,9 @@ macro_rules! bad_literal_non_string {
//~| ERROR metavariables of `${concat(..)}` must be of type
//~| ERROR metavariables of `${concat(..)}` must be of type
//~| ERROR metavariables of `${concat(..)}` must be of type
//~| ERROR metavariables of `${concat(..)}` must be of type
//~| ERROR floats are not supported as metavariables of `${concat(..)}`
//~| ERROR integer metavariables of `${concat(..)}` must not be suffixed
//~| ERROR integer metavariables of `${concat(..)}` must not be suffixed
}
}
@@ -149,7 +151,6 @@ macro_rules! bad_tt_literal {
const ${concat(_foo, $tt)}: () = ();
//~^ ERROR metavariables of `${concat(..)}` must be of type
//~| ERROR metavariables of `${concat(..)}` must be of type
//~| ERROR metavariables of `${concat(..)}` must be of type
}
}
@@ -178,13 +179,14 @@ fn main() {
bad_literal_string!("1.0");
bad_literal_string!("'1'");
bad_literal_non_string!(1);
bad_literal_non_string!(-1);
bad_literal_non_string!(1.0);
bad_literal_non_string!('1');
bad_literal_non_string!(false);
bad_literal_non_string!(4f64);
bad_literal_non_string!(5u8);
bad_literal_non_string!(6_u8);
bad_tt_literal!(1);
bad_tt_literal!(1.0);
bad_tt_literal!('1');
}

View File

@@ -130,7 +130,7 @@ error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `t
LL | ${concat($ex, aaaa)}
| ^^
|
= note: currently only string literals are supported
= note: currently only string and integer literals are supported
error: variable `foo` is not recognized in meta-variable expression
--> $DIR/concat-usage-errors.rs:37:30
@@ -276,7 +276,7 @@ error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `t
LL | const ${concat(_foo, $literal)}: () = ();
| ^^^^^^^
|
= note: currently only string literals are supported
= note: currently only string and integer literals are supported
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
--> $DIR/concat-usage-errors.rs:138:31
@@ -284,7 +284,7 @@ error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `t
LL | const ${concat(_foo, $literal)}: () = ();
| ^^^^^^^
|
= note: currently only string literals are supported
= note: currently only string and integer literals are supported
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
@@ -293,7 +293,7 @@ error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `t
LL | const ${concat(_foo, $literal)}: () = ();
| ^^^^^^^
|
= note: currently only string literals are supported
= note: currently only string and integer literals are supported
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
@@ -302,43 +302,45 @@ error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `t
LL | const ${concat(_foo, $literal)}: () = ();
| ^^^^^^^
|
= note: currently only string literals are supported
= note: currently only string and integer literals are supported
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
error: floats are not supported as metavariables of `${concat(..)}`
--> $DIR/concat-usage-errors.rs:138:31
|
LL | const ${concat(_foo, $literal)}: () = ();
| ^^^^^^^
error: integer metavariables of `${concat(..)}` must not be suffixed
--> $DIR/concat-usage-errors.rs:138:31
|
LL | const ${concat(_foo, $literal)}: () = ();
| ^^^^^^^
error: integer metavariables of `${concat(..)}` must not be suffixed
--> $DIR/concat-usage-errors.rs:138:31
|
LL | const ${concat(_foo, $literal)}: () = ();
| ^^^^^^^
|
= note: currently only string literals are supported
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
--> $DIR/concat-usage-errors.rs:149:31
--> $DIR/concat-usage-errors.rs:151:31
|
LL | const ${concat(_foo, $tt)}: () = ();
| ^^
|
= note: currently only string literals are supported
= note: currently only string and integer literals are supported
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
--> $DIR/concat-usage-errors.rs:149:31
--> $DIR/concat-usage-errors.rs:151:31
|
LL | const ${concat(_foo, $tt)}: () = ();
| ^^
|
= note: currently only string literals are supported
= note: currently only string and integer literals are supported
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
--> $DIR/concat-usage-errors.rs:149:31
|
LL | const ${concat(_foo, $tt)}: () = ();
| ^^
|
= note: currently only string literals are supported
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 43 previous errors
error: aborting due to 44 previous errors