authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-15 16:41:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-15 16:41:10-07:00
log2c434cddd6624cfd5c71f081b9445936d25c7703
treeb7512d9b316275efeff0c93fb034b47ae045d6d5
parentd4a0d5f959b88ffc23edc4593bc75b6168acaea9

AstGen: add missing coercion for const locals

A const local which had its init expression write to the result pointer, but then gets elided to directly initialize, was missing the coercion to the type annotation.

3 files changed, 22 insertions(+), 6 deletions(-)

lib/std/special/compiler_rt.zig+2-4
......@@ -303,10 +303,8 @@ comptime {
303303
304304 const __floatunsisf = @import("compiler_rt/floatunsisf.zig").__floatunsisf;
305305 @export(__floatunsisf, .{ .name = "__floatunsisf", .linkage = linkage });
306 if (builtin.zig_backend == .stage1) { // TODO it's crashing on a switch expression
307 const __floatundisf = @import("compiler_rt/floatundisf.zig").__floatundisf;
308 @export(__floatundisf, .{ .name = "__floatundisf", .linkage = linkage });
309 }
306 const __floatundisf = @import("compiler_rt/floatundisf.zig").__floatundisf;
307 @export(__floatundisf, .{ .name = "__floatundisf", .linkage = linkage });
310308 const __floatunsidf = @import("compiler_rt/floatunsidf.zig").__floatunsidf;
311309 @export(__floatunsidf, .{ .name = "__floatunsidf", .linkage = linkage });
312310 const __floatundidf = @import("compiler_rt/floatundidf.zig").__floatundidf;
src/AstGen.zig+9-2
......@@ -2762,11 +2762,18 @@ fn varDecl(
27622762 }
27632763 gz.instructions.items.len = dst;
27642764
2765 // In case the result location did not do the coercion
2766 // for us so we must do it here.
2767 const coerced_init = if (opt_type_inst != .none)
2768 try gz.addBin(.as, opt_type_inst, init_inst)
2769 else
2770 init_inst;
2771
27652772 if (!gz.force_comptime) {
27662773 _ = try gz.add(.{ .tag = .dbg_var_val, .data = .{
27672774 .str_op = .{
27682775 .str = ident_name,
2769 .operand = init_inst,
2776 .operand = coerced_init,
27702777 },
27712778 } });
27722779 }
......@@ -2776,7 +2783,7 @@ fn varDecl(
27762783 .parent = scope,
27772784 .gen_zir = gz,
27782785 .name = ident_name,
2779 .inst = init_inst,
2786 .inst = coerced_init,
27802787 .token_src = name_token,
27812788 .id_cat = .@"local constant",
27822789 };
test/behavior/eval.zig+11
......@@ -766,3 +766,14 @@ test "two comptime calls with array default initialized to undefined" {
766766 S.CrossTarget.parse();
767767 }
768768}
769
770test "const type-annotated local initialized with function call has correct type" {
771 const S = struct {
772 fn foo() comptime_int {
773 return 1234;
774 }
775 };
776 const x: u64 = S.foo();
777 try expect(@TypeOf(x) == u64);
778 try expect(x == 1234);
779}