Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavior of noreturn (and noreturn like) variables #13807

Open
Vexu opened this issue Dec 7, 2022 · 0 comments
Open

Behavior of noreturn (and noreturn like) variables #13807

Vexu opened this issue Dec 7, 2022 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@Vexu
Copy link
Member

Vexu commented Dec 7, 2022

Currently using noreturn and noreturn like types as variables is a bit broken.

Using noreturn directly gives a compiler error but applying the suggestions triggers an assertion:

pub export fn entry() void {
    var n: noreturn = undefined; // error: variable of type 'noreturn' must be const or comptime
    _ = n;
}

pub export fn entry() void {
    comptime var n: noreturn = undefined;
    _ = n;
}
// thread 27040 panic: reached unreachable code
// lib/std/debug.zig:278:14: 0x79b7c16 in assert (zig)
//     if (!ok) unreachable; // assertion failure
//              ^
// src/Sema.zig:5275:122: 0x80c7260 in analyzeBlockBody (zig)
//     assert(sema.typeOf(Air.indexToRef(child_block.instructions.items[child_block.instructions.items.len - 1])).isNoReturn());

An empty error set causes invalid LLVM IR and making it const or comptime hits the same assertion:

pub export fn entry() void {
    const E = error{};
    var e: E = undefined;
    _ = e;
}
// LLVM Emit Object... 
// Basic Block in function 'entry' does not have terminator!
// label %Entry

// thread 27288 panic: LLVM module verification failed

Empty enums and unions are not currently recognized as noreturn types so this example compiles just fine but if recognized as such then it hits the same issues.

pub export fn entry() void {
    const E = enum{};
    var e: E = undefined;
    _ = e;
}

One solution that should handle generic code nicely would be to add a panic to loading a noreturn variable or dereferencing a noreturn pointer but I'm not sure if it's the best solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

1 participant