From 7d0b6956c0935807f0e5862c45da857e8e065c6b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 15 Mar 2022 13:29:54 -0700 Subject: [PATCH] stage2: resolve type fully when resolving inferred allocs We must resolve the type fully so that pointer children (i.e. slices) are resolved. Additionally, we must resolve even if we can know the value at comptime because the `alloc_inferred` ZIR always produces a constant in the AIR. Fixes #11181 --- src/Sema.zig | 7 ++++++- test/behavior.zig | 1 + test/behavior/bugs/11181.zig | 31 +++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/behavior/bugs/11181.zig diff --git a/src/Sema.zig b/src/Sema.zig index 8fc6b19e467bca3e715dfaa89ccc3acca27a5e55..8c81f96edf1f016bc7a464b516ee18090c68ac7d 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -2672,11 +2672,16 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com // ZIR handles it later, so we can just use the ty ref here. air_datas[ptr_inst].ty_pl.ty = air_datas[bitcast_inst].ty_op.ty; + // Unless the block is comptime, `alloc_inferred` always produces + // a runtime constant. The final inferred type needs to be + // fully resolved so it can be lowered in codegen. + try sema.resolveTypeFully(block, ty_src, final_elem_ty); + return; } try sema.requireRuntimeBlock(block, src); - try sema.resolveTypeLayout(block, ty_src, final_elem_ty); + try sema.resolveTypeFully(block, ty_src, final_elem_ty); // Change it to a normal alloc. sema.air_instructions.set(ptr_inst, .{ diff --git a/test/behavior.zig b/test/behavior.zig index 9e0cb50558aaeaf76c373b623787cdec4153c3b0..edc9953e59b08f074f0b3bf04769a3c51b5a8df1 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -66,6 +66,7 @@ test { _ = @import("behavior/bugs/11046.zig"); _ = @import("behavior/bugs/11139.zig"); _ = @import("behavior/bugs/11165.zig"); + _ = @import("behavior/bugs/11181.zig"); _ = @import("behavior/call.zig"); _ = @import("behavior/cast.zig"); _ = @import("behavior/comptime_memory.zig"); diff --git a/test/behavior/bugs/11181.zig b/test/behavior/bugs/11181.zig new file mode 100644 index 0000000000000000000000000000000000000000..f7baed5774aa7ff1532f92394994174e93ed83f0 --- /dev/null +++ b/test/behavior/bugs/11181.zig @@ -0,0 +1,31 @@ +const builtin = @import("builtin"); + +test "const inferred array of slices" { + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + + const T = struct { v: bool }; + + const decls = [_][]const T{ + &[_]T{ + .{ .v = false }, + }, + }; + _ = decls; +} + +test "var inferred array of slices" { + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + + const T = struct { v: bool }; + + var decls = [_][]const T{ + &[_]T{ + .{ .v = false }, + }, + }; + _ = decls; +} -- 2.54.0