Skip to content

Commit 150082b

Browse files
committed
Auto merge of #14878 - lowr:feat/metavar-expr-count, r=Veykril
Implement `${count()}` metavariable expression This PR implements `${count()}` metavariable expression for MBE as described in [RFC 3086][rfc]. See [this section][sec-count] of the RFC for its semantics. Additionally, this PR changes the type of depth parameter of `${index()}` from u32 to usize so as to match how [rustc parses it][index-usize]. Part of #11952 Fixes #14871 [rfc]: rust-lang/rfcs#3086 [sec-count]: https://github.com/rust-lang/rfcs/blob/master/text/3086-macro-metavar-expr.md#count [index-usize]:https://github.com/rust-lang/rust/blob/ddad0576caf8d0515ed453e04b468977c7d3dfc1/compiler/rustc_expand/src/mbe/metavar_expr.rs#L22
2 parents f6e3a87 + 0d4d1d7 commit 150082b

File tree

8 files changed

+481
-115
lines changed

8 files changed

+481
-115
lines changed

crates/hir-def/src/macro_expansion_tests/mbe.rs

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
mod tt_conversion;
55
mod matching;
66
mod meta_syntax;
7+
mod metavar_expr;
78
mod regression;
89

910
use expect_test::expect;
@@ -1614,92 +1615,6 @@ struct Foo;
16141615
)
16151616
}
16161617

1617-
#[test]
1618-
fn test_dollar_dollar() {
1619-
check(
1620-
r#"
1621-
macro_rules! register_struct { ($Struct:ident) => {
1622-
macro_rules! register_methods { ($$($method:ident),*) => {
1623-
macro_rules! implement_methods { ($$$$($$val:expr),*) => {
1624-
struct $Struct;
1625-
impl $Struct { $$(fn $method() -> &'static [u32] { &[$$$$($$$$val),*] })*}
1626-
}}
1627-
}}
1628-
}}
1629-
1630-
register_struct!(Foo);
1631-
register_methods!(alpha, beta);
1632-
implement_methods!(1, 2, 3);
1633-
"#,
1634-
expect![[r#"
1635-
macro_rules! register_struct { ($Struct:ident) => {
1636-
macro_rules! register_methods { ($$($method:ident),*) => {
1637-
macro_rules! implement_methods { ($$$$($$val:expr),*) => {
1638-
struct $Struct;
1639-
impl $Struct { $$(fn $method() -> &'static [u32] { &[$$$$($$$$val),*] })*}
1640-
}}
1641-
}}
1642-
}}
1643-
1644-
macro_rules !register_methods {
1645-
($($method: ident), *) = > {
1646-
macro_rules!implement_methods {
1647-
($$($val: expr), *) = > {
1648-
struct Foo;
1649-
impl Foo {
1650-
$(fn $method()-> &'static[u32] {
1651-
&[$$($$val), *]
1652-
}
1653-
)*
1654-
}
1655-
}
1656-
}
1657-
}
1658-
}
1659-
macro_rules !implement_methods {
1660-
($($val: expr), *) = > {
1661-
struct Foo;
1662-
impl Foo {
1663-
fn alpha()-> &'static[u32] {
1664-
&[$($val), *]
1665-
}
1666-
fn beta()-> &'static[u32] {
1667-
&[$($val), *]
1668-
}
1669-
}
1670-
}
1671-
}
1672-
struct Foo;
1673-
impl Foo {
1674-
fn alpha() -> &'static[u32] {
1675-
&[1, 2, 3]
1676-
}
1677-
fn beta() -> &'static[u32] {
1678-
&[1, 2, 3]
1679-
}
1680-
}
1681-
"#]],
1682-
)
1683-
}
1684-
1685-
#[test]
1686-
fn test_metavar_exprs() {
1687-
check(
1688-
r#"
1689-
macro_rules! m {
1690-
( $( $t:tt )* ) => ( $( ${ignore(t)} -${index()} )-* );
1691-
}
1692-
const _: i32 = m!(a b c);
1693-
"#,
1694-
expect![[r#"
1695-
macro_rules! m {
1696-
( $( $t:tt )* ) => ( $( ${ignore(t)} -${index()} )-* );
1697-
}
1698-
const _: i32 = -0--1--2;
1699-
"#]],
1700-
);
1701-
}
1702-
17031618
#[test]
17041619
fn test_punct_without_space() {
17051620
// Puncts are "glued" greedily.

0 commit comments

Comments
 (0)