Skip to content

Commit

Permalink
Sema: fail if analyzing return in noreturn-declared function before…
Browse files Browse the repository at this point in the history
… coercing `undefined`

Just switches logic around in coerceExtra to check for returning in a noreturn function before coercing undefined to anything
  • Loading branch information
gabeuehlein authored Oct 14, 2024
1 parent 9fd61f7 commit 7b8fc18
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30346,11 +30346,6 @@ fn coerceExtra(
else => {},
}

// undefined to anything. We do this after the big switch above so that
// special logic has a chance to run first, such as `*[N]T` to `[]T` which
// should initialize the length field of the slice.
if (maybe_inst_val) |val| if (val.toIntern() == .undef) return pt.undefRef(dest_ty);

if (!opts.report_err) return error.NotCoercible;

if (opts.is_ret and dest_ty.zigTypeTag(zcu) == .noreturn) {
Expand All @@ -30368,6 +30363,11 @@ fn coerceExtra(
return sema.failWithOwnedErrorMsg(block, msg);
}

// undefined to anything. We do this after the big switch above so that
// special logic has a chance to run first, such as `*[N]T` to `[]T` which
// should initialize the length field of the slice.
if (maybe_inst_val) |val| if (val.toIntern() == .undef) return pt.undefRef(dest_ty);

const msg = msg: {
const msg = try sema.errMsg(inst_src, "expected type '{}', found '{}'", .{ dest_ty.fmt(pt), inst_ty.fmt(pt) });
errdefer msg.destroy(sema.gpa);
Expand Down
10 changes: 10 additions & 0 deletions test/cases/compile_errors/return_undefined_from_noreturn.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export fn entry() noreturn {
return undefined;
}

// error
// backend=stage2
// target=native
//
// :2:12: error: function declared 'noreturn' returns
// :1:19: note: 'noreturn' declared here

0 comments on commit 7b8fc18

Please sign in to comment.