Skip to content

Commit 6d229f8

Browse files
authored
Rollup merge of rust-lang#146308 - cyrgani:concat-integer-literals, r=jackh726
support integer literals in `${concat()}` Tracking issue: rust-lang#124225 Adds support for using integer literals as arguments to `${concat()}` macro expressions. Integer formatting such as `1_000` is preserved by this.
2 parents 0909c3b + 1cb749a commit 6d229f8

File tree

4 files changed

+62
-24
lines changed

4 files changed

+62
-24
lines changed

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,11 +940,27 @@ fn extract_symbol_from_pnr<'a>(
940940
{
941941
Ok(*symbol)
942942
}
943+
ParseNtResult::Literal(expr)
944+
if let ExprKind::Lit(lit @ Lit { kind: LitKind::Integer, symbol, suffix }) =
945+
&expr.kind =>
946+
{
947+
if lit.is_semantic_float() {
948+
Err(dcx
949+
.struct_err("floats are not supported as metavariables of `${concat(..)}`")
950+
.with_span(span_err))
951+
} else if suffix.is_none() {
952+
Ok(*symbol)
953+
} else {
954+
Err(dcx
955+
.struct_err("integer metavariables of `${concat(..)}` must not be suffixed")
956+
.with_span(span_err))
957+
}
958+
}
943959
_ => Err(dcx
944960
.struct_err(
945961
"metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`",
946962
)
947-
.with_note("currently only string literals are supported")
963+
.with_note("currently only string and integer literals are supported")
948964
.with_span(span_err)),
949965
}
950966
}

tests/ui/macros/metavar-expressions/concat-allowed-operations.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ macro_rules! combinations {
9292
}};
9393
}
9494

95+
macro_rules! int_struct {
96+
($n: literal) => {
97+
struct ${concat(E, $n)};
98+
}
99+
}
100+
95101
fn main() {
96102
create_things!(behold);
97103
behold_separated_idents_in_a_fn();
@@ -112,4 +118,16 @@ fn main() {
112118
assert_eq!(VAR_123, 2);
113119

114120
combinations!(_hello, "a", b, "b");
121+
122+
int_struct!(1_0);
123+
int_struct!(2);
124+
int_struct!(3___0);
125+
int_struct!(7_);
126+
int_struct!(08);
127+
128+
let _ = E1_0;
129+
let _ = E2;
130+
let _ = E3___0;
131+
let _ = E7_;
132+
let _ = E08;
115133
}

tests/ui/macros/metavar-expressions/concat-usage-errors.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ macro_rules! bad_literal_non_string {
140140
//~| ERROR metavariables of `${concat(..)}` must be of type
141141
//~| ERROR metavariables of `${concat(..)}` must be of type
142142
//~| ERROR metavariables of `${concat(..)}` must be of type
143-
//~| ERROR metavariables of `${concat(..)}` must be of type
143+
//~| ERROR floats are not supported as metavariables of `${concat(..)}`
144+
//~| ERROR integer metavariables of `${concat(..)}` must not be suffixed
145+
//~| ERROR integer metavariables of `${concat(..)}` must not be suffixed
144146
}
145147
}
146148

@@ -149,7 +151,6 @@ macro_rules! bad_tt_literal {
149151
const ${concat(_foo, $tt)}: () = ();
150152
//~^ ERROR metavariables of `${concat(..)}` must be of type
151153
//~| ERROR metavariables of `${concat(..)}` must be of type
152-
//~| ERROR metavariables of `${concat(..)}` must be of type
153154
}
154155
}
155156

@@ -178,13 +179,14 @@ fn main() {
178179
bad_literal_string!("1.0");
179180
bad_literal_string!("'1'");
180181

181-
bad_literal_non_string!(1);
182182
bad_literal_non_string!(-1);
183183
bad_literal_non_string!(1.0);
184184
bad_literal_non_string!('1');
185185
bad_literal_non_string!(false);
186+
bad_literal_non_string!(4f64);
187+
bad_literal_non_string!(5u8);
188+
bad_literal_non_string!(6_u8);
186189

187-
bad_tt_literal!(1);
188190
bad_tt_literal!(1.0);
189191
bad_tt_literal!('1');
190192
}

tests/ui/macros/metavar-expressions/concat-usage-errors.stderr

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `t
130130
LL | ${concat($ex, aaaa)}
131131
| ^^
132132
|
133-
= note: currently only string literals are supported
133+
= note: currently only string and integer literals are supported
134134

135135
error: variable `foo` is not recognized in meta-variable expression
136136
--> $DIR/concat-usage-errors.rs:37:30
@@ -276,15 +276,15 @@ error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `t
276276
LL | const ${concat(_foo, $literal)}: () = ();
277277
| ^^^^^^^
278278
|
279-
= note: currently only string literals are supported
279+
= note: currently only string and integer literals are supported
280280

281281
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
282282
--> $DIR/concat-usage-errors.rs:138:31
283283
|
284284
LL | const ${concat(_foo, $literal)}: () = ();
285285
| ^^^^^^^
286286
|
287-
= note: currently only string literals are supported
287+
= note: currently only string and integer literals are supported
288288
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
289289

290290
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
293293
LL | const ${concat(_foo, $literal)}: () = ();
294294
| ^^^^^^^
295295
|
296-
= note: currently only string literals are supported
296+
= note: currently only string and integer literals are supported
297297
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
298298

299299
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
302302
LL | const ${concat(_foo, $literal)}: () = ();
303303
| ^^^^^^^
304304
|
305-
= note: currently only string literals are supported
305+
= note: currently only string and integer literals are supported
306306
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
307307

308-
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
308+
error: floats are not supported as metavariables of `${concat(..)}`
309309
--> $DIR/concat-usage-errors.rs:138:31
310310
|
311311
LL | const ${concat(_foo, $literal)}: () = ();
312312
| ^^^^^^^
313+
314+
error: integer metavariables of `${concat(..)}` must not be suffixed
315+
--> $DIR/concat-usage-errors.rs:138:31
313316
|
314-
= note: currently only string literals are supported
315-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
317+
LL | const ${concat(_foo, $literal)}: () = ();
318+
| ^^^^^^^
316319

317-
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
318-
--> $DIR/concat-usage-errors.rs:149:31
320+
error: integer metavariables of `${concat(..)}` must not be suffixed
321+
--> $DIR/concat-usage-errors.rs:138:31
319322
|
320-
LL | const ${concat(_foo, $tt)}: () = ();
321-
| ^^
323+
LL | const ${concat(_foo, $literal)}: () = ();
324+
| ^^^^^^^
322325
|
323-
= note: currently only string literals are supported
326+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
324327

325328
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
326-
--> $DIR/concat-usage-errors.rs:149:31
329+
--> $DIR/concat-usage-errors.rs:151:31
327330
|
328331
LL | const ${concat(_foo, $tt)}: () = ();
329332
| ^^
330333
|
331-
= note: currently only string literals are supported
332-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
334+
= note: currently only string and integer literals are supported
333335

334336
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
335-
--> $DIR/concat-usage-errors.rs:149:31
337+
--> $DIR/concat-usage-errors.rs:151:31
336338
|
337339
LL | const ${concat(_foo, $tt)}: () = ();
338340
| ^^
339341
|
340-
= note: currently only string literals are supported
342+
= note: currently only string and integer literals are supported
341343
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
342344

343-
error: aborting due to 43 previous errors
345+
error: aborting due to 44 previous errors
344346

0 commit comments

Comments
 (0)