Skip to content

Commit 89fe0f3

Browse files
committed
support integer literals in ${concat()}
1 parent fbd8f95 commit 89fe0f3

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ macro_rules! bad_literal_non_string {
139139
//~^ ERROR metavariables of `${concat(..)}` must be of type
140140
//~| ERROR metavariables of `${concat(..)}` must be of type
141141
//~| ERROR metavariables of `${concat(..)}` must be of type
142-
//~| ERROR metavariables of `${concat(..)}` must be of type
143-
//~| ERROR metavariables of `${concat(..)}` must be of type
142+
//~| ERROR floats are not supported as metavariables of `${concat(..)}`
143+
//~| ERROR integer metavariables of `${concat(..)}` must not be suffixed
144+
//~| ERROR integer metavariables of `${concat(..)}` must not be suffixed
144145
}
145146
}
146147

@@ -149,7 +150,6 @@ macro_rules! bad_tt_literal {
149150
const ${concat(_foo, $tt)}: () = ();
150151
//~^ ERROR metavariables of `${concat(..)}` must be of type
151152
//~| ERROR metavariables of `${concat(..)}` must be of type
152-
//~| ERROR metavariables of `${concat(..)}` must be of type
153153
}
154154
}
155155

@@ -178,13 +178,13 @@ fn main() {
178178
bad_literal_string!("1.0");
179179
bad_literal_string!("'1'");
180180

181-
bad_literal_non_string!(1);
182-
bad_literal_non_string!(-1);
183181
bad_literal_non_string!(1.0);
184182
bad_literal_non_string!('1');
185183
bad_literal_non_string!(false);
184+
bad_literal_non_string!(4f64);
185+
bad_literal_non_string!(5u8);
186+
bad_literal_non_string!(6_u8);
186187

187-
bad_tt_literal!(1);
188188
bad_tt_literal!(1.0);
189189
bad_tt_literal!('1');
190190
}

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

Lines changed: 15 additions & 22 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,51 +293,44 @@ 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

299-
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
299+
error: floats are not supported as metavariables of `${concat(..)}`
300300
--> $DIR/concat-usage-errors.rs:138:31
301301
|
302302
LL | const ${concat(_foo, $literal)}: () = ();
303303
| ^^^^^^^
304-
|
305-
= note: currently only string literals are supported
306-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
307304

308-
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
305+
error: integer metavariables of `${concat(..)}` must not be suffixed
309306
--> $DIR/concat-usage-errors.rs:138:31
310307
|
311308
LL | const ${concat(_foo, $literal)}: () = ();
312309
| ^^^^^^^
313-
|
314-
= note: currently only string literals are supported
315-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
316310

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

325319
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
326-
--> $DIR/concat-usage-errors.rs:149:31
320+
--> $DIR/concat-usage-errors.rs:150:31
327321
|
328322
LL | const ${concat(_foo, $tt)}: () = ();
329323
| ^^
330324
|
331-
= note: currently only string literals are supported
332-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
325+
= note: currently only string and integer literals are supported
333326

334327
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
335-
--> $DIR/concat-usage-errors.rs:149:31
328+
--> $DIR/concat-usage-errors.rs:150:31
336329
|
337330
LL | const ${concat(_foo, $tt)}: () = ();
338331
| ^^
339332
|
340-
= note: currently only string literals are supported
333+
= note: currently only string and integer literals are supported
341334
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
342335

343336
error: aborting due to 43 previous errors

0 commit comments

Comments
 (0)