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

Closes #13210
Follow up to 3ccd4907fb
This commit is contained in:
Veikka Tuominen 2022-10-18 13:56:20 +03:00
parent c95a34b68f
commit 3b60a6de2f
2 changed files with 31 additions and 1 deletions

View File

@ -6433,7 +6433,7 @@ fn analyzeInlineCallArg(
.ty = param_ty,
.val = arg_val,
};
} else if ((try sema.resolveMaybeUndefVal(arg_block, arg_src, casted_arg) == null) or
} else if (((try sema.resolveMaybeUndefVal(arg_block, arg_src, casted_arg)) == null) or
try sema.typeRequiresComptime(param_ty) or zir_tags[inst] == .param_comptime)
{
try sema.inst_map.putNoClobber(sema.gpa, inst, casted_arg);
@ -14521,6 +14521,12 @@ fn zirClosureGet(
return sema.failWithOwnedErrorMsg(msg);
}
if (tv.val.tag() == .unreachable_value) {
assert(block.is_typeof);
// We need a dummy runtime instruction with the correct type.
return block.addTy(.alloc, tv.ty);
}
return sema.addConstant(tv.ty, tv.val);
}

View File

@ -314,3 +314,27 @@ test "lazy size cast to float" {
test "bitSizeOf comptime_int" {
try expect(@bitSizeOf(comptime_int) == 0);
}
test "runtime instructions inside typeof in comptime only scope" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
{
var y: i8 = 2;
const i: [2]i8 = [_]i8{ 1, y };
const T = struct {
a: @TypeOf(i) = undefined, // causes crash
b: @TypeOf(i[0]) = undefined, // causes crash
};
try expect(@TypeOf((T{}).a) == [2]i8);
try expect(@TypeOf((T{}).b) == i8);
}
{
var y: i8 = 2;
const i = .{ 1, y };
const T = struct {
b: @TypeOf(i[1]) = undefined,
};
try expect(@TypeOf((T{}).b) == i8);
}
}