diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-03-06 21:24:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-06 21:24:24 +0000 |
commit | b82408fd6a17762fcb6fc7b5efe9307a3e59f8b7 (patch) | |
tree | d1b65b146861961f38d1e292407610e3a435fbc0 /gcc | |
parent | e2bccf43ed1bf33d5f454eab39a7a4a1f115b0bd (diff) | |
parent | 82fc107e3d1929ec41135353d62a11c42979f831 (diff) | |
download | gcc-b82408fd6a17762fcb6fc7b5efe9307a3e59f8b7.zip gcc-b82408fd6a17762fcb6fc7b5efe9307a3e59f8b7.tar.gz gcc-b82408fd6a17762fcb6fc7b5efe9307a3e59f8b7.tar.bz2 |
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>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/expand/rust-macro-substitute-ctx.cc | 13 | ||||
-rw-r--r-- | gcc/testsuite/rust/execute/torture/macros16.rs | 14 | ||||
-rw-r--r-- | gcc/testsuite/rust/execute/torture/macros17.rs | 17 | ||||
-rw-r--r-- | gcc/testsuite/rust/execute/torture/macros18.rs | 14 |
4 files changed, 55 insertions, 3 deletions
diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.cc b/gcc/rust/expand/rust-macro-substitute-ctx.cc index 8542614..3d07b48 100644 --- a/gcc/rust/expand/rust-macro-substitute-ctx.cc +++ b/gcc/rust/expand/rust-macro-substitute-ctx.cc @@ -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; @@ -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}); } @@ -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++) { diff --git a/gcc/testsuite/rust/execute/torture/macros16.rs b/gcc/testsuite/rust/execute/torture/macros16.rs new file mode 100644 index 0000000..47ab241 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/macros16.rs @@ -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 +} diff --git a/gcc/testsuite/rust/execute/torture/macros17.rs b/gcc/testsuite/rust/execute/torture/macros17.rs new file mode 100644 index 0000000..e007bb3 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/macros17.rs @@ -0,0 +1,17 @@ +macro_rules! two { + (2) => { + 3 + }; +} + +macro_rules! one { + (1) => { + two!(2) + }; +} + +fn main() -> i32 { + let a = one!(1); + + a - 3 +} diff --git a/gcc/testsuite/rust/execute/torture/macros18.rs b/gcc/testsuite/rust/execute/torture/macros18.rs new file mode 100644 index 0000000..61df17e --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/macros18.rs @@ -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 +} |