Skip to content

Commit daab25d

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

File tree

5 files changed

+159
-43
lines changed

5 files changed

+159
-43
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
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#![feature(macro_metavar_expr_concat)]
2+
#![allow(non_camel_case_types)]
3+
4+
macro_rules! int_struct {
5+
($n: literal) => {
6+
struct ${concat(E, $n)};
7+
//~^ ERROR floats are not supported as metavariables of `${concat(..)}`
8+
//~| ERROR metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
9+
//~| ERROR integer metavariables of `${concat(..)}` must not be suffixed
10+
//~| ERROR integer metavariables of `${concat(..)}` must not be suffixed
11+
}
12+
}
13+
14+
int_struct!(1_0);
15+
int_struct!(2);
16+
int_struct!(3___0);
17+
int_struct!(4f64);
18+
int_struct!(4.0);
19+
int_struct!(5u8);
20+
int_struct!(6_u8);
21+
int_struct!(7_);
22+
int_struct!(08);
23+
24+
fn main() {
25+
E1_0;
26+
E2;
27+
E3___0;
28+
E7_;
29+
E08;
30+
31+
E4; //~ ERROR cannot find value `E4` in this scope [E0425]
32+
E4.0; //~ ERROR cannot find value `E4` in this scope [E0425]
33+
E4f64; //~ ERROR cannot find value `E4f64` in this scope [E0425]
34+
E5; //~ ERROR cannot find value `E5` in this scope [E0425]
35+
E5u8; //~ ERROR cannot find value `E5u8` in this scope [E0425]
36+
E6; //~ ERROR cannot find value `E6` in this scope [E0425]
37+
E6_; //~ ERROR cannot find value `E6_` in this scope [E0425]
38+
E6_u8; //~ ERROR cannot find value `E6_u8` in this scope [E0425]
39+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
error: floats are not supported as metavariables of `${concat(..)}`
2+
--> $DIR/concat-integer-literals.rs:6:29
3+
|
4+
LL | struct ${concat(E, $n)};
5+
| ^
6+
7+
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
8+
--> $DIR/concat-integer-literals.rs:6:29
9+
|
10+
LL | struct ${concat(E, $n)};
11+
| ^
12+
|
13+
= note: currently only string and integer literals are supported
14+
15+
error: integer metavariables of `${concat(..)}` must not be suffixed
16+
--> $DIR/concat-integer-literals.rs:6:29
17+
|
18+
LL | struct ${concat(E, $n)};
19+
| ^
20+
21+
error: integer metavariables of `${concat(..)}` must not be suffixed
22+
--> $DIR/concat-integer-literals.rs:6:29
23+
|
24+
LL | struct ${concat(E, $n)};
25+
| ^
26+
|
27+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
28+
29+
error[E0425]: cannot find value `E4` in this scope
30+
--> $DIR/concat-integer-literals.rs:31:5
31+
|
32+
LL | struct ${concat(E, $n)};
33+
| -------- similarly named unit struct `E2` defined here
34+
...
35+
LL | E4;
36+
| ^^ help: a unit struct with a similar name exists: `E2`
37+
38+
error[E0425]: cannot find value `E4` in this scope
39+
--> $DIR/concat-integer-literals.rs:32:5
40+
|
41+
LL | struct ${concat(E, $n)};
42+
| -------- similarly named unit struct `E2` defined here
43+
...
44+
LL | E4.0;
45+
| ^^ help: a unit struct with a similar name exists: `E2`
46+
47+
error[E0425]: cannot find value `E4f64` in this scope
48+
--> $DIR/concat-integer-literals.rs:33:5
49+
|
50+
LL | E4f64;
51+
| ^^^^^ not found in this scope
52+
53+
error[E0425]: cannot find value `E5` in this scope
54+
--> $DIR/concat-integer-literals.rs:34:5
55+
|
56+
LL | struct ${concat(E, $n)};
57+
| -------- similarly named unit struct `E2` defined here
58+
...
59+
LL | E5;
60+
| ^^ help: a unit struct with a similar name exists: `E2`
61+
62+
error[E0425]: cannot find value `E5u8` in this scope
63+
--> $DIR/concat-integer-literals.rs:35:5
64+
|
65+
LL | E5u8;
66+
| ^^^^ not found in this scope
67+
68+
error[E0425]: cannot find value `E6` in this scope
69+
--> $DIR/concat-integer-literals.rs:36:5
70+
|
71+
LL | struct ${concat(E, $n)};
72+
| -------- similarly named unit struct `E2` defined here
73+
...
74+
LL | E6;
75+
| ^^ help: a unit struct with a similar name exists: `E2`
76+
77+
error[E0425]: cannot find value `E6_` in this scope
78+
--> $DIR/concat-integer-literals.rs:37:5
79+
|
80+
LL | struct ${concat(E, $n)};
81+
| -------- similarly named unit struct `E7_` defined here
82+
...
83+
LL | E6_;
84+
| ^^^ help: a unit struct with a similar name exists: `E7_`
85+
86+
error[E0425]: cannot find value `E6_u8` in this scope
87+
--> $DIR/concat-integer-literals.rs:38:5
88+
|
89+
LL | E6_u8;
90+
| ^^^^^ not found in this scope
91+
92+
error: aborting due to 12 previous errors
93+
94+
For more information about this error, try `rustc --explain E0425`.

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ 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
144142
}
145143
}
146144

@@ -149,7 +147,6 @@ macro_rules! bad_tt_literal {
149147
const ${concat(_foo, $tt)}: () = ();
150148
//~^ ERROR metavariables of `${concat(..)}` must be of type
151149
//~| ERROR metavariables of `${concat(..)}` must be of type
152-
//~| ERROR metavariables of `${concat(..)}` must be of type
153150
}
154151
}
155152

@@ -178,13 +175,10 @@ fn main() {
178175
bad_literal_string!("1.0");
179176
bad_literal_string!("'1'");
180177

181-
bad_literal_non_string!(1);
182-
bad_literal_non_string!(-1);
183178
bad_literal_non_string!(1.0);
184179
bad_literal_non_string!('1');
185180
bad_literal_non_string!(false);
186181

187-
bad_tt_literal!(1);
188182
bad_tt_literal!(1.0);
189183
bad_tt_literal!('1');
190184
}

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

Lines changed: 9 additions & 36 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,52 +293,25 @@ 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`
300-
--> $DIR/concat-usage-errors.rs:138:31
301-
|
302-
LL | const ${concat(_foo, $literal)}: () = ();
303-
| ^^^^^^^
304-
|
305-
= note: currently only string literals are supported
306-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
307-
308-
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
309-
--> $DIR/concat-usage-errors.rs:138:31
310-
|
311-
LL | const ${concat(_foo, $literal)}: () = ();
312-
| ^^^^^^^
313-
|
314-
= note: currently only string literals are supported
315-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
316-
317-
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
318-
--> $DIR/concat-usage-errors.rs:149:31
300+
--> $DIR/concat-usage-errors.rs:147:31
319301
|
320302
LL | const ${concat(_foo, $tt)}: () = ();
321303
| ^^
322304
|
323-
= note: currently only string literals are supported
324-
325-
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
326-
--> $DIR/concat-usage-errors.rs:149:31
327-
|
328-
LL | const ${concat(_foo, $tt)}: () = ();
329-
| ^^
330-
|
331-
= note: currently only string literals are supported
332-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
305+
= note: currently only string and integer literals are supported
333306

334307
error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
335-
--> $DIR/concat-usage-errors.rs:149:31
308+
--> $DIR/concat-usage-errors.rs:147:31
336309
|
337310
LL | const ${concat(_foo, $tt)}: () = ();
338311
| ^^
339312
|
340-
= note: currently only string literals are supported
313+
= note: currently only string and integer literals are supported
341314
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
342315

343-
error: aborting due to 43 previous errors
316+
error: aborting due to 40 previous errors
344317

0 commit comments

Comments
 (0)