authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-26 18:20:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-28 13:24:43-07:00
log82fc360613e7070ffe7124fb070b151a382bd31b
treeba114f2d8b46afc955d257a22553b75fd59d21a0
parenta67dec1c9f86b5c74c91cd4fd3e7fe06c8440586

stage2: avoid panicking for unimplemented compiler code

Prevents the compiler from crashing when using `@memset` on extern unions, for example.

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

src/Sema.zig+3
......@@ -26954,10 +26954,12 @@ fn storePtrVal(
2695426954 reinterpret.val_ptr.*.writeToMemory(mut_kit.ty, sema.mod, buffer) catch |err| switch (err) {
2695526955 error.ReinterpretDeclRef => unreachable,
2695626956 error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
26957 error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{mut_kit.ty.fmt(sema.mod)}),
2695726958 };
2695826959 operand_val.writeToMemory(operand_ty, sema.mod, buffer[reinterpret.byte_offset..]) catch |err| switch (err) {
2695926960 error.ReinterpretDeclRef => unreachable,
2696026961 error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
26962 error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{mut_kit.ty.fmt(sema.mod)}),
2696126963 };
2696226964
2696326965 const arena = mut_kit.beginArena(sema.mod);
......@@ -27908,6 +27910,7 @@ fn bitCastVal(
2790827910 val.writeToMemory(old_ty, sema.mod, buffer) catch |err| switch (err) {
2790927911 error.ReinterpretDeclRef => return null,
2791027912 error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
27913 error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{old_ty.fmt(sema.mod)}),
2791127914 };
2791227915 return try Value.readFromMemory(new_ty, sema.mod, buffer[buffer_offset..], sema.arena);
2791327916}
src/value.zig+6-4
......@@ -1281,6 +1281,7 @@ pub const Value = extern union {
12811281 pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) error{
12821282 ReinterpretDeclRef,
12831283 IllDefinedMemoryLayout,
1284 Unimplemented,
12841285 }!void {
12851286 const target = mod.getTarget();
12861287 const endian = target.cpu.arch.endian();
......@@ -1370,19 +1371,19 @@ pub const Value = extern union {
13701371 },
13711372 .Union => switch (ty.containerLayout()) {
13721373 .Auto => return error.IllDefinedMemoryLayout,
1373 .Extern => @panic("TODO implement writeToMemory for extern unions"),
1374 .Extern => return error.Unimplemented,
13741375 .Packed => {
13751376 const byte_count = (@intCast(usize, ty.bitSize(target)) + 7) / 8;
13761377 return writeToPackedMemory(val, ty, mod, buffer[0..byte_count], 0);
13771378 },
13781379 },
13791380 .Pointer => {
1380 assert(!ty.isSlice()); // No well defined layout.
1381 if (ty.isSlice()) return error.IllDefinedMemoryLayout;
13811382 if (val.isDeclRef()) return error.ReinterpretDeclRef;
13821383 return val.writeToMemory(Type.usize, mod, buffer);
13831384 },
13841385 .Optional => {
1385 assert(ty.isPtrLikeOptional());
1386 if (!ty.isPtrLikeOptional()) return error.IllDefinedMemoryLayout;
13861387 var buf: Type.Payload.ElemType = undefined;
13871388 const child = ty.optionalChild(&buf);
13881389 const opt_val = val.optionalValue();
......@@ -1392,7 +1393,7 @@ pub const Value = extern union {
13921393 return writeToMemory(Value.zero, Type.usize, mod, buffer);
13931394 }
13941395 },
1395 else => @panic("TODO implement writeToMemory for more types"),
1396 else => return error.Unimplemented,
13961397 }
13971398 }
13981399
......@@ -5401,6 +5402,7 @@ pub const Value = extern union {
54015402 // code late in compilation. So, this error handling is too aggressive and
54025403 // causes some false negatives, causing less-than-ideal code generation.
54035404 error.IllDefinedMemoryLayout => return null,
5405 error.Unimplemented => return null,
54045406 };
54055407 const first_byte = byte_buffer[0];
54065408 for (byte_buffer[1..]) |byte| {