Skip to content

Commit 790759f

Browse files
committed
Auto merge of rust-lang#13854 - lowr:fix/mbe-glue-punct, r=Veykril
Support multi-character punct tokens in MBE Fixes rust-lang#11497 In the context of MBE, consecutive puncts are parsed as multi-character punct tokens whenever possible. For example, `:::` is parsed as ``[Punct(`::`), Punct(`:`)]`` and shouldn't get matched to patterns like `: : :` or `: ::`. We have implemented this behavior only for when we match puncts against `tt` fragments, but not when we match puncts literally. This PR extracts the multi-character punct handling procedure into a separate method and extends its support for literal matching. For good measure, this PR adds support for `<-` token, which is still [considered as one token in rustc](https://github.com/rust-lang/rust/blob/e3961864075eaa9e855e5eec6b4f148029684539/compiler/rustc_ast/src/token.rs#L249) despite the placement syntax having been removed.
2 parents e986de0 + a7d4114 commit 790759f

File tree

6 files changed

+255
-123
lines changed

6 files changed

+255
-123
lines changed

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

+45
Original file line numberDiff line numberDiff line change
@@ -1630,3 +1630,48 @@ const _: i32 = -0--1--2;
16301630
"#]],
16311631
);
16321632
}
1633+
1634+
#[test]
1635+
fn test_punct_without_space() {
1636+
// Puncts are "glued" greedily.
1637+
check(
1638+
r#"
1639+
macro_rules! foo {
1640+
(: : :) => { "1 1 1" };
1641+
(: ::) => { "1 2" };
1642+
(:: :) => { "2 1" };
1643+
1644+
(: : : :) => { "1 1 1 1" };
1645+
(:: : :) => { "2 1 1" };
1646+
(: :: :) => { "1 2 1" };
1647+
(: : ::) => { "1 1 2" };
1648+
(:: ::) => { "2 2" };
1649+
}
1650+
1651+
fn test() {
1652+
foo!(:::);
1653+
foo!(: :::);
1654+
foo!(::::);
1655+
}
1656+
"#,
1657+
expect![[r#"
1658+
macro_rules! foo {
1659+
(: : :) => { "1 1 1" };
1660+
(: ::) => { "1 2" };
1661+
(:: :) => { "2 1" };
1662+
1663+
(: : : :) => { "1 1 1 1" };
1664+
(:: : :) => { "2 1 1" };
1665+
(: :: :) => { "1 2 1" };
1666+
(: : ::) => { "1 1 2" };
1667+
(:: ::) => { "2 2" };
1668+
}
1669+
1670+
fn test() {
1671+
"2 1";
1672+
"1 2 1";
1673+
"2 2";
1674+
}
1675+
"#]],
1676+
);
1677+
}

crates/mbe/src/benchmark.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ fn invocation_fixtures(rules: &FxHashMap<String, DeclarativeMacro>) -> Vec<(Stri
141141
None => (),
142142
Some(kind) => panic!("Unhandled kind {kind:?}"),
143143
},
144-
Op::Leaf(leaf) => parent.token_trees.push(leaf.clone().into()),
144+
Op::Literal(it) => parent.token_trees.push(tt::Leaf::from(it.clone()).into()),
145+
Op::Ident(it) => parent.token_trees.push(tt::Leaf::from(it.clone()).into()),
146+
Op::Punct(puncts) => {
147+
for punct in puncts {
148+
parent.token_trees.push(tt::Leaf::from(punct.clone()).into());
149+
}
150+
}
145151
Op::Repeat { tokens, kind, separator } => {
146152
let max = 10;
147153
let cnt = match kind {

0 commit comments

Comments
 (0)