From 7461309b730f4a25f56d71b1a4d6c7dc6b16a39a Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 19 Feb 2024 15:52:57 +0000 Subject: [PATCH] Sema: validate that runtime-known inferred alloc does not have comptime-only type Resolves: #18997 --- src/Sema.zig | 7 +++++++ .../runtime_condition_comptime_type_in_destructure.zig | 10 ++++++++++ 2 files changed, 17 insertions(+) create mode 100644 test/cases/compile_errors/runtime_condition_comptime_type_in_destructure.zig diff --git a/src/Sema.zig b/src/Sema.zig index 1ee036c15cf8aca6a4cb6cd954f622c677b19908..81ee58b7c2756cd813b86fca7a8a4b01d1cb3609 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -4149,6 +4149,13 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com return; } + if (try sema.typeRequiresComptime(final_elem_ty)) { + // The alloc wasn't comptime-known per the above logic, so the + // type cannot be comptime-only. + // TODO: source location of runtime control flow + return sema.fail(block, src, "value with comptime-only type '{}' depends on runtime control flow", .{final_elem_ty.fmt(mod)}); + } + try sema.queueFullTypeResolution(final_elem_ty); // Change it to a normal alloc. diff --git a/test/cases/compile_errors/runtime_condition_comptime_type_in_destructure.zig b/test/cases/compile_errors/runtime_condition_comptime_type_in_destructure.zig new file mode 100644 index 0000000000000000000000000000000000000000..a6624d03ed656cb57dd19f89abc6f70902ae637b --- /dev/null +++ b/test/cases/compile_errors/runtime_condition_comptime_type_in_destructure.zig @@ -0,0 +1,10 @@ +export fn foobar() void { + var t = true; + _ = &t; + const a, _ = if (t) .{ .a, {} } else .{ .b, {} }; + _ = a; +} + +// error +// +// :4:5: error: value with comptime-only type '@TypeOf(.enum_literal)' depends on runtime control flow -- 2.54.0