Skip to content

Commit b82408f

Browse files
Merge #986
986: Fix ICE on recursive macro invocation r=CohenArthur a=CohenArthur Closes #982 We can now do fancy lispy things! ```rust macro_rules! add { ($e:literal) => { 0 + $e }; ($e:literal $($es:literal)*) => { $e + add!($($es)*) }; } ``` I've switched the order of the commits around so that the buildbot is happy Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2 parents e2bccf4 + 82fc107 commit b82408f

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

gcc/rust/expand/rust-macro-substitute-ctx.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ SubstituteCtx::substitute_metavar (std::unique_ptr<AST::Token> &metavar)
1717
else
1818
{
1919
// Replace
20-
// We only care about the vector when expanding repetitions. Just access
21-
// the first element of the vector.
20+
// We only care about the vector when expanding repetitions.
21+
// Just access the first element of the vector.
2222
// FIXME: Clean this up so it makes more sense
2323
auto &frag = it->second[0];
2424
for (size_t offs = frag.token_offset_begin; offs < frag.token_offset_end;
@@ -103,7 +103,13 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end)
103103
for (auto &kv_match : fragments)
104104
{
105105
std::vector<MatchedFragment> sub_vec;
106-
sub_vec.emplace_back (kv_match.second[i]);
106+
107+
// FIXME: Hack: If a fragment is not repeated, how does it fit in the
108+
// submap? Do we really want to expand it? Is this normal behavior?
109+
if (kv_match.second.size () == 1)
110+
sub_vec.emplace_back (kv_match.second[0]);
111+
else
112+
sub_vec.emplace_back (kv_match.second[i]);
107113

108114
sub_map.insert ({kv_match.first, sub_vec});
109115
}
@@ -177,6 +183,7 @@ std::vector<std::unique_ptr<AST::Token>>
177183
SubstituteCtx::substitute_tokens ()
178184
{
179185
std::vector<std::unique_ptr<AST::Token>> replaced_tokens;
186+
rust_debug ("expanding tokens");
180187

181188
for (size_t i = 0; i < macro.size (); i++)
182189
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
macro_rules! add {
2+
($e:literal) => {
3+
0 + $e
4+
};
5+
($e:literal $($es:literal)*) => {
6+
$e + add!($($es)*)
7+
};
8+
}
9+
10+
fn main() -> i32 {
11+
let a = add!(1 2 3 10); // 16
12+
13+
a - 16
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
macro_rules! two {
2+
(2) => {
3+
3
4+
};
5+
}
6+
7+
macro_rules! one {
8+
(1) => {
9+
two!(2)
10+
};
11+
}
12+
13+
fn main() -> i32 {
14+
let a = one!(1);
15+
16+
a - 3
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
macro_rules! add {
2+
($e:literal) => {
3+
0 + $e
4+
};
5+
($e:literal $($es:literal)*) => {
6+
$e + add!($($es)*)
7+
};
8+
}
9+
10+
fn main() -> i32 {
11+
let a = add!(3 4); // 7
12+
13+
a - 7
14+
}

0 commit comments

Comments
 (0)