forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#118958 - c410-f3r:concat-again, r=petrochenkov
Add a new concat metavar expr Revival of rust-lang#111930 Giving it another try now that rust-lang#117050 was merged. With the new rules, meta-variable expressions must be referenced with a dollar sign (`$`) and this can cause misunderstands with `$concat`. ```rust macro_rules! foo { ( $bar:ident ) => { const ${concat(VAR, bar)}: i32 = 1; }; } // Will produce `VARbar` instead of `VAR_123` foo!(_123); ``` In other words, forgetting the dollar symbol can produce undesired outputs. cc rust-lang#29599 cc rust-lang#124225
- Loading branch information
Showing
15 changed files
with
551 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
tests/ui/feature-gates/feature-gate-macro-metavar-expr-concat.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
macro_rules! join { | ||
($lhs:ident, $rhs:ident) => { | ||
let ${concat($lhs, $rhs)}: () = (); | ||
//~^ ERROR the `concat` meta-variable expression is unstable | ||
}; | ||
} | ||
|
||
fn main() { | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/ui/feature-gates/feature-gate-macro-metavar-expr-concat.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error[E0658]: the `concat` meta-variable expression is unstable | ||
--> $DIR/feature-gate-macro-metavar-expr-concat.rs:3:14 | ||
| | ||
LL | let ${concat($lhs, $rhs)}: () = (); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #124225 <https://github.com/rust-lang/rust/issues/124225> for more information | ||
= help: add `#![feature(macro_metavar_expr_concat)]` to the crate attributes to enable | ||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
58 changes: 58 additions & 0 deletions
58
tests/ui/macros/macro-metavar-expr-concat/allowed-operations.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//@ run-pass | ||
|
||
#![allow(dead_code, non_camel_case_types, non_upper_case_globals)] | ||
#![feature(macro_metavar_expr_concat)] | ||
|
||
macro_rules! create_things { | ||
($lhs:ident) => { | ||
struct ${concat($lhs, _separated_idents_in_a_struct)} { | ||
foo: i32, | ||
${concat($lhs, _separated_idents_in_a_field)}: i32, | ||
} | ||
|
||
mod ${concat($lhs, _separated_idents_in_a_module)} { | ||
pub const FOO: () = (); | ||
} | ||
|
||
fn ${concat($lhs, _separated_idents_in_a_fn)}() {} | ||
}; | ||
} | ||
|
||
macro_rules! many_idents { | ||
($a:ident, $c:ident) => { | ||
const ${concat($a, B, $c, D)}: i32 = 1; | ||
}; | ||
} | ||
|
||
macro_rules! valid_tts { | ||
($_0:tt, $_1:tt) => { | ||
const ${concat($_0, $_1)}: i32 = 1; | ||
} | ||
} | ||
|
||
macro_rules! without_dollar_sign_is_an_ident { | ||
($ident:ident) => { | ||
const ${concat(VAR, ident)}: i32 = 1; | ||
const ${concat(VAR, $ident)}: i32 = 2; | ||
}; | ||
} | ||
|
||
fn main() { | ||
create_things!(behold); | ||
behold_separated_idents_in_a_fn(); | ||
let _ = behold_separated_idents_in_a_module::FOO; | ||
let _ = behold_separated_idents_in_a_struct { | ||
foo: 1, | ||
behold_separated_idents_in_a_field: 2, | ||
}; | ||
|
||
many_idents!(A, C); | ||
assert_eq!(ABCD, 1); | ||
|
||
valid_tts!(X, YZ); | ||
assert_eq!(XYZ, 1); | ||
|
||
without_dollar_sign_is_an_ident!(_123); | ||
assert_eq!(VARident, 1); | ||
assert_eq!(VAR_123, 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![feature(macro_metavar_expr_concat)] | ||
|
||
macro_rules! join { | ||
($lhs:ident, $rhs:ident) => { | ||
${concat($lhs, $rhs)} | ||
//~^ ERROR cannot find value `abcdef` in this scope | ||
}; | ||
} | ||
|
||
fn main() { | ||
let abcdef = 1; | ||
let _another = join!(abc, def); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0425]: cannot find value `abcdef` in this scope | ||
--> $DIR/hygiene.rs:5:10 | ||
| | ||
LL | ${concat($lhs, $rhs)} | ||
| ^^^^^^^^^^^^^^^^^^^^ not found in this scope | ||
... | ||
LL | let _another = join!(abc, def); | ||
| --------------- in this macro invocation | ||
| | ||
= note: this error originates in the macro `join` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0425`. |
Oops, something went wrong.