Skip to content

Commit a902931

Browse files
committed
- A raw-vararg splat in a macro call inside a defer, like defer { foo(0, ...y); }, crashed the compiler. #3661
1 parent e358dab commit a902931

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎releasenotes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- A runtime value for a bitstruct field set through an array element or range in an initializer, like `Bits[2] b = { [1].x = t };`, crashed the compiler.
6060
- A runtime value for a `@bigendian` bitstruct field set through a struct member in an initializer was stored without the byte swap.
6161
- A ternary on unions could lose data stored in the padding of the union's largest member, like `(c ? a : b).wide[1]` on a union of `struct { ulong lo; ushort hi; }` and `ulong[2] wide`. #3662
62+
- A raw-vararg splat in a macro call inside a `defer`, like `defer { foo(0, ...y); }`, crashed the compiler. #3661
6263

6364
### Stdlib changes
6465
- `String.tokenize` and `String.tokenize_all` with an empty split now return each character.

‎src/compiler/copying.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,8 @@ Decl *copy_decl(CopyStruct *c, Decl *decl)
11411141
MACRO_COPY_DECL_METHODS(copy->method_table);
11421142
break;
11431143
case DECL_DECLARRAY:
1144+
MACRO_COPY_DECL_LIST(copy->decls);
1145+
break;
11441146
case DECL_GROUP:
11451147
UNREACHABLE
11461148
case DECL_BITSTRUCT:
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module defer_vaarg_splat;
2+
3+
int total;
4+
faultdef BAD;
5+
6+
macro void add_all(...)
7+
{
8+
$for var $i = 0; $i < $vaarg.len; $i++:
9+
total += $vaarg[$i];
10+
$endfor
11+
}
12+
13+
macro void forward(...) => add_all(...$vaarg);
14+
15+
fn int two_exits(int r)
16+
{
17+
defer { int[3] y = { 1, 2, 3 }; add_all(0, ...y); }
18+
if (r) return 1;
19+
return 0;
20+
}
21+
22+
fn void? on_fault(bool fail)
23+
{
24+
defer (catch e) { int[2] y = { 10, 20 }; add_all(...y); }
25+
if (fail) return BAD~;
26+
}
27+
28+
fn void nested(int r)
29+
{
30+
defer
31+
{
32+
int[2] y = { 100, 200 };
33+
forward(1, ...y);
34+
}
35+
if (r) return;
36+
}
37+
38+
fn void defer_with_vaarg_splat() @test
39+
{
40+
total = 0;
41+
two_exits(1);
42+
two_exits(0);
43+
assert(total == 12);
44+
total = 0;
45+
(void)on_fault(false);
46+
assert(total == 0);
47+
(void)on_fault(true);
48+
assert(total == 30);
49+
total = 0;
50+
nested(1);
51+
nested(0);
52+
assert(total == 602);
53+
}

0 commit comments

Comments
 (0)