authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-19 15:52:57+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-19 21:48:50+00:00
log7461309b730f4a25f56d71b1a4d6c7dc6b16a39a
treeb63e62593a113af5de2e9665a6c4d953e124e6e9
parent80f3ef6e14a1213d1c3b31d515870afb43cc9379

Sema: validate that runtime-known inferred alloc does not have comptime-only type

Resolves: #18997

2 files changed, 17 insertions(+), 0 deletions(-)

src/Sema.zig+7
...@@ -4149,6 +4149,13 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com...@@ -4149,6 +4149,13 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
4149 return;4149 return;
4150 }4150 }
41514151
4152 if (try sema.typeRequiresComptime(final_elem_ty)) {
4153 // The alloc wasn't comptime-known per the above logic, so the
4154 // type cannot be comptime-only.
4155 // TODO: source location of runtime control flow
4156 return sema.fail(block, src, "value with comptime-only type '{}' depends on runtime control flow", .{final_elem_ty.fmt(mod)});
4157 }
4158
4152 try sema.queueFullTypeResolution(final_elem_ty);4159 try sema.queueFullTypeResolution(final_elem_ty);
41534160
4154 // Change it to a normal alloc.4161 // Change it to a normal alloc.
test/cases/compile_errors/runtime_condition_comptime_type_in_destructure.zig created+10
...@@ -0,0 +1,10 @@
1export fn foobar() void {
2 var t = true;
3 _ = &t;
4 const a, _ = if (t) .{ .a, {} } else .{ .b, {} };
5 _ = a;
6}
7
8// error
9//
10// :4:5: error: value with comptime-only type '@TypeOf(.enum_literal)' depends on runtime control flow