authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-25 00:40:03-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-25 12:00:48-07:00
logb9c4857ed64fb18fa8cbc69112fd4fb4f8a5ac68
tree48f6dde6a95bf44cadca07492f92fad5aca2ec85
parent17e12960b5c12dd1dfb68940e70e9541dda856a2

AstGen: add source location to certain const initializers

Before: assign_local_bad_coercion.zig:5:1: error: expected type 'u32', found 'u64' export fn constEntry() u32 { ^~~~~~ assign_local_bad_coercion.zig:11:19: error: expected type 'u32', found 'u64' var x: u32 = g(); ~^~ After: assign_local_bad_coercion.zig:6:21: error: expected type 'u32', found 'u64' const x: u32 = g(); ~^~ assign_local_bad_coercion.zig:11:19: error: expected type 'u32', found 'u64' var x: u32 = g(); ~^~

5 files changed, 31 insertions(+), 6 deletions(-)

src/AstGen.zig+4-1
......@@ -3209,7 +3209,10 @@ fn varDecl(
32093209 // In case the result location did not do the coercion
32103210 // for us so we must do it here.
32113211 const coerced_init = if (opt_type_inst != .none)
3212 try gz.addBin(.as, opt_type_inst, init_inst)
3212 try gz.addPlNode(.as_node, var_decl.ast.init_node, Zir.Inst.As{
3213 .dest_type = opt_type_inst,
3214 .operand = init_inst,
3215 })
32133216 else
32143217 init_inst;
32153218
test/cases/compile_errors/assign_local_bad_coercion.zig created+22
......@@ -0,0 +1,22 @@
1fn g() u64 {
2 return 0;
3}
4
5export fn constEntry() u32 {
6 const x: u32 = g();
7 return x;
8}
9
10export fn varEntry() u32 {
11 var x: u32 = g();
12 return x;
13}
14
15// error
16// backend=stage2
17// target=native
18//
19// :6:21: error: expected type 'u32', found 'u64'
20// :6:21: note: unsigned 32-bit int cannot represent all possible unsigned 64-bit values
21// :11:19: error: expected type 'u32', found 'u64'
22// :11:19: note: unsigned 32-bit int cannot represent all possible unsigned 64-bit values
test/cases/compile_errors/break_void_result_location.zig+1-1
......@@ -29,4 +29,4 @@ export fn f4() void {
2929// :2:22: error: expected type 'usize', found 'void'
3030// :7:9: error: expected type 'usize', found 'void'
3131// :14:9: error: expected type 'usize', found 'void'
32// :18:1: error: expected type 'usize', found 'void'
32// :19:27: error: expected type 'usize', found 'void'
test/cases/compile_errors/nested_error_set_mismatch.zig+3-3
......@@ -14,6 +14,6 @@ fn foo() ?OtherError!i32 {
1414// backend=llvm
1515// target=native
1616//
17// :4:1: error: expected type '?error{NextError}!i32', found '?error{OutOfMemory}!i32'
18// :4:1: note: optional type child 'error{OutOfMemory}!i32' cannot cast into optional type child 'error{NextError}!i32'
19// :4:1: note: 'error.OutOfMemory' not a member of destination error set
17// :5:34: error: expected type '?error{NextError}!i32', found '?error{OutOfMemory}!i32'
18// :5:34: note: optional type child 'error{OutOfMemory}!i32' cannot cast into optional type child 'error{NextError}!i32'
19// :5:34: note: 'error.OutOfMemory' not a member of destination error set
test/cases/compile_errors/union_init_with_none_or_multiple_fields.zig+1-1
......@@ -27,7 +27,7 @@ export fn u2m() void {
2727// backend=stage2
2828// target=native
2929//
30// :9:1: error: union initializer must initialize one field
30// :10:20: error: union initializer must initialize one field
3131// :1:12: note: union declared here
3232// :14:20: error: cannot initialize multiple union fields at once; unions can only have one active field
3333// :14:31: note: additional initializer here