Skip to content

Commit

Permalink
Merge #986
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and CohenArthur authored Mar 6, 2022
2 parents e2bccf4 + 82fc107 commit b82408f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
13 changes: 10 additions & 3 deletions gcc/rust/expand/rust-macro-substitute-ctx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ SubstituteCtx::substitute_metavar (std::unique_ptr<AST::Token> &metavar)
else
{
// Replace
// We only care about the vector when expanding repetitions. Just access
// the first element of the vector.
// We only care about the vector when expanding repetitions.
// Just access the first element of the vector.
// FIXME: Clean this up so it makes more sense
auto &frag = it->second[0];
for (size_t offs = frag.token_offset_begin; offs < frag.token_offset_end;
Expand Down Expand Up @@ -103,7 +103,13 @@ SubstituteCtx::substitute_repetition (size_t pattern_start, size_t pattern_end)
for (auto &kv_match : fragments)
{
std::vector<MatchedFragment> sub_vec;
sub_vec.emplace_back (kv_match.second[i]);

// FIXME: Hack: If a fragment is not repeated, how does it fit in the
// submap? Do we really want to expand it? Is this normal behavior?
if (kv_match.second.size () == 1)
sub_vec.emplace_back (kv_match.second[0]);
else
sub_vec.emplace_back (kv_match.second[i]);

sub_map.insert ({kv_match.first, sub_vec});
}
Expand Down Expand Up @@ -177,6 +183,7 @@ std::vector<std::unique_ptr<AST::Token>>
SubstituteCtx::substitute_tokens ()
{
std::vector<std::unique_ptr<AST::Token>> replaced_tokens;
rust_debug ("expanding tokens");

for (size_t i = 0; i < macro.size (); i++)
{
Expand Down
14 changes: 14 additions & 0 deletions gcc/testsuite/rust/execute/torture/macros16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
macro_rules! add {
($e:literal) => {
0 + $e
};
($e:literal $($es:literal)*) => {
$e + add!($($es)*)
};
}

fn main() -> i32 {
let a = add!(1 2 3 10); // 16

a - 16
}
17 changes: 17 additions & 0 deletions gcc/testsuite/rust/execute/torture/macros17.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
macro_rules! two {
(2) => {
3
};
}

macro_rules! one {
(1) => {
two!(2)
};
}

fn main() -> i32 {
let a = one!(1);

a - 3
}
14 changes: 14 additions & 0 deletions gcc/testsuite/rust/execute/torture/macros18.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
macro_rules! add {
($e:literal) => {
0 + $e
};
($e:literal $($es:literal)*) => {
$e + add!($($es)*)
};
}

fn main() -> i32 {
let a = add!(3 4); // 7

a - 7
}

0 comments on commit b82408f

Please sign in to comment.