Sema: check type of comptime try operand

Resolves: #14693
This commit is contained in:
mlugg 2023-02-23 14:07:06 +00:00 committed by Veikka Tuominen
parent 3e99afdbfe
commit 6d7fb8f19c
2 changed files with 19 additions and 1 deletions

View file

@ -1612,6 +1612,12 @@ fn analyzeBodyInner(
const extra = sema.code.extraData(Zir.Inst.Try, inst_data.payload_index);
const inline_body = sema.code.extra[extra.end..][0..extra.data.body_len];
const err_union = try sema.resolveInst(extra.data.operand);
const err_union_ty = sema.typeOf(err_union);
if (err_union_ty.zigTypeTag() != .ErrorUnion) {
return sema.fail(block, operand_src, "expected error union type, found '{}'", .{
err_union_ty.fmt(sema.mod),
});
}
const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
assert(is_non_err != .none);
const is_non_err_tv = sema.resolveInstConst(block, operand_src, is_non_err, "try operand inside comptime block must be comptime-known") catch |err| {
@ -1619,7 +1625,6 @@ fn analyzeBodyInner(
return err;
};
if (is_non_err_tv.val.toBool()) {
const err_union_ty = sema.typeOf(err_union);
break :blk try sema.analyzeErrUnionPayload(block, src, err_union_ty, err_union, operand_src, false);
}
const break_data = (try sema.analyzeBodyBreak(block, inline_body)) orelse

View file

@ -0,0 +1,13 @@
export fn foo() void {
try bar();
}
pub fn bar() u8 {
return 0;
}
// error
// backend=stage2
// target=native
//
// :2:12: error: expected error union type, found 'u8'