authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-02-23 14:07:06+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-03-09 02:02:19+02:00
log6d7fb8f19c864f04d3472e5aec161957193e1e7c
treece9c815592cf46cb5ee6ee3f59ead9554a8e4e99
parent3e99afdbfe9a66bc5461148a4754c1e88b33fb88

Sema: check type of comptime try operand

Resolves: #14693

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

src/Sema.zig+6-1
......@@ -1612,6 +1612,12 @@ fn analyzeBodyInner(
16121612 const extra = sema.code.extraData(Zir.Inst.Try, inst_data.payload_index);
16131613 const inline_body = sema.code.extra[extra.end..][0..extra.data.body_len];
16141614 const err_union = try sema.resolveInst(extra.data.operand);
1615 const err_union_ty = sema.typeOf(err_union);
1616 if (err_union_ty.zigTypeTag() != .ErrorUnion) {
1617 return sema.fail(block, operand_src, "expected error union type, found '{}'", .{
1618 err_union_ty.fmt(sema.mod),
1619 });
1620 }
16151621 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
16161622 assert(is_non_err != .none);
16171623 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(
16191625 return err;
16201626 };
16211627 if (is_non_err_tv.val.toBool()) {
1622 const err_union_ty = sema.typeOf(err_union);
16231628 break :blk try sema.analyzeErrUnionPayload(block, src, err_union_ty, err_union, operand_src, false);
16241629 }
16251630 const break_data = (try sema.analyzeBodyBreak(block, inline_body)) orelse
test/cases/compile_errors/comptime_try_non_error.zig created+13
......@@ -0,0 +1,13 @@
1export fn foo() void {
2 try bar();
3}
4
5pub fn bar() u8 {
6 return 0;
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :2:12: error: expected error union type, found 'u8'