diff --git a/src/Sema.zig b/src/Sema.zig index 8c6e3cf05c..840e8a4c6c 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -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 diff --git a/test/cases/compile_errors/comptime_try_non_error.zig b/test/cases/compile_errors/comptime_try_non_error.zig new file mode 100644 index 0000000000..9b342f7934 --- /dev/null +++ b/test/cases/compile_errors/comptime_try_non_error.zig @@ -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'