authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-18 13:56:20+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-20 20:11:12+03:00
log3b60a6de2fd68d7d02de7a10b8b88c919aa088f0
tree4dd4233eaf364efec301663c3383501d0afaaf2a
parentc95a34b68f6075d7a9d305d17a6b03bc9fd1fff2

Sema: fix using runtime instructions inside typeof in comptime only blocks

Closes #13210 Follow up to 3ccd4907fbcd04ecddffb618a4b14581fd080279

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

src/Sema.zig+7-1
......@@ -6433,7 +6433,7 @@ fn analyzeInlineCallArg(
64336433 .ty = param_ty,
64346434 .val = arg_val,
64356435 };
6436 } else if ((try sema.resolveMaybeUndefVal(arg_block, arg_src, casted_arg) == null) or
6436 } else if (((try sema.resolveMaybeUndefVal(arg_block, arg_src, casted_arg)) == null) or
64376437 try sema.typeRequiresComptime(param_ty) or zir_tags[inst] == .param_comptime)
64386438 {
64396439 try sema.inst_map.putNoClobber(sema.gpa, inst, casted_arg);
......@@ -14521,6 +14521,12 @@ fn zirClosureGet(
1452114521 return sema.failWithOwnedErrorMsg(msg);
1452214522 }
1452314523
14524 if (tv.val.tag() == .unreachable_value) {
14525 assert(block.is_typeof);
14526 // We need a dummy runtime instruction with the correct type.
14527 return block.addTy(.alloc, tv.ty);
14528 }
14529
1452414530 return sema.addConstant(tv.ty, tv.val);
1452514531}
1452614532
test/behavior/sizeof_and_typeof.zig+24
......@@ -314,3 +314,27 @@ test "lazy size cast to float" {
314314test "bitSizeOf comptime_int" {
315315 try expect(@bitSizeOf(comptime_int) == 0);
316316}
317
318test "runtime instructions inside typeof in comptime only scope" {
319 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
320 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
321
322 {
323 var y: i8 = 2;
324 const i: [2]i8 = [_]i8{ 1, y };
325 const T = struct {
326 a: @TypeOf(i) = undefined, // causes crash
327 b: @TypeOf(i[0]) = undefined, // causes crash
328 };
329 try expect(@TypeOf((T{}).a) == [2]i8);
330 try expect(@TypeOf((T{}).b) == i8);
331 }
332 {
333 var y: i8 = 2;
334 const i = .{ 1, y };
335 const T = struct {
336 b: @TypeOf(i[1]) = undefined,
337 };
338 try expect(@TypeOf((T{}).b) == i8);
339 }
340}