Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}
18 changes: 18 additions & 0 deletions tests/ui/macros/metavar-expressions/concat-allowed-operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
10 changes: 6 additions & 4 deletions tests/ui/macros/metavar-expressions/concat-usage-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand Down Expand Up @@ -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');
}
40 changes: 21 additions & 19 deletions tests/ui/macros/metavar-expressions/concat-usage-errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -276,15 +276,15 @@ 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
|
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`
Expand All @@ -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`
Expand All @@ -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
|
= note: currently only string literals are supported
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
LL | const ${concat(_foo, $literal)}: () = ();
| ^^^^^^^

error: metavariables of `${concat(..)}` must be of type `ident`, `literal` or `tt`
--> $DIR/concat-usage-errors.rs:149:31
error: integer metavariables of `${concat(..)}` must not be suffixed
--> $DIR/concat-usage-errors.rs:138:31
|
LL | const ${concat(_foo, $tt)}: () = ();
| ^^
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: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
= 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: aborting due to 43 previous errors
error: aborting due to 44 previous errors

Loading