authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-08-11 22:45:55-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-08-12 01:57:11-04:00
log2b5bd56a67a26cd4adff76b6e3bf542e97f91cc4
tree1fb2a33f508353cf91a9486b83d3706706607b2d
parentffc116de78dee6db8b3f2e0474f21bd88ef3895c

AstGen: fix src loc for invalid coercions in tuple literals


2 files changed, 26 insertions(+), 1 deletions(-)

src/AstGen.zig+4-1
...@@ -11359,7 +11359,10 @@ const GenZir = struct {...@@ -11359,7 +11359,10 @@ const GenZir = struct {
11359 parent_gz.instructions.items.len -= src - dst;11359 parent_gz.instructions.items.len -= src - dst;
11360 as_scope.instructions_top = GenZir.unstacked_top;11360 as_scope.instructions_top = GenZir.unstacked_top;
11361 // as_scope now unstacked, can add new instructions to parent_gz11361 // as_scope now unstacked, can add new instructions to parent_gz
11362 const casted_result = try parent_gz.addBin(.as, dest_type, result);11362 const casted_result = try parent_gz.addPlNode(.as_node, src_node, Zir.Inst.As{
11363 .dest_type = dest_type,
11364 .operand = result,
11365 });
11363 return rvalue(parent_gz, ri, casted_result, src_node);11366 return rvalue(parent_gz, ri, casted_result, src_node);
11364 } else {11367 } else {
11365 // implicitly move all as_scope instructions to parent_gz11368 // implicitly move all as_scope instructions to parent_gz
test/cases/compile_errors/invalid_coercion_in_aggregate_literal.zig created+22
...@@ -0,0 +1,22 @@
1export fn invalidArrayElem() u8 {
2 const array_literal = [1]u8{@as(u8, 256)};
3 return array_literal[0];
4}
5
6export fn invalidTupleElem() u8 {
7 const tuple_literal = struct { u8 }{@as(u8, 256)};
8 return tuple_literal[0];
9}
10
11export fn invalidStructField() u8 {
12 const struct_literal = struct { field: u8 }{ .field = @as(u8, 256) };
13 return struct_literal.field;
14}
15
16// error
17// backend=stage2
18// target=native
19//
20// :2:41: error: type 'u8' cannot represent integer value '256'
21// :7:49: error: type 'u8' cannot represent integer value '256'
22// :12:67: error: type 'u8' cannot represent integer value '256'