authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-05 19:08:05+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-02-05 19:08:05+00:00
log456f3c026b3d6fb289f42e442b202e0be152c9d3
tree49510e75264bff79e08b4539f34843624816a365
parent3ce857d0543780e96bfd50d75d53d870ff308fef
signaturelock-open Commit is signed but in an unrecognized format.

Sema: fix crash on `@tagName` of undefined enum literal

Resolves: #20826

2 files changed, 9 insertions(+), 1 deletions(-)

src/Sema.zig+1-1
...@@ -21401,7 +21401,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -21401,7 +21401,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
21401 try operand_ty.resolveLayout(pt);21401 try operand_ty.resolveLayout(pt);
21402 const enum_ty = switch (operand_ty.zigTypeTag(zcu)) {21402 const enum_ty = switch (operand_ty.zigTypeTag(zcu)) {
21403 .enum_literal => {21403 .enum_literal => {
21404 const val = try sema.resolveConstDefinedValue(block, LazySrcLoc.unneeded, operand, undefined);21404 const val = (try sema.resolveDefinedValue(block, operand_src, operand)).?;
21405 const tag_name = ip.indexToKey(val.toIntern()).enum_literal;21405 const tag_name = ip.indexToKey(val.toIntern()).enum_literal;
21406 return sema.addNullTerminatedStrLit(tag_name);21406 return sema.addNullTerminatedStrLit(tag_name);
21407 },21407 },
test/cases/compile_errors/tagName_on_undef_enum_literal.zig created+8
...@@ -0,0 +1,8 @@
1comptime {
2 const undef: @Type(.enum_literal) = undefined;
3 _ = @tagName(undef);
4}
5
6// error
7//
8// :3:18: error: use of undefined value here causes undefined behavior