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

Unreachable in analyze.cpp from comptime function call that takes optional error parameter #11236

Closed
alinebee opened this issue Mar 20, 2022 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@alinebee
Copy link

alinebee commented Mar 20, 2022

Zig Version

0.10.0-dev.1447+1bd595cee

Steps to Reproduce

Compile and test the following program with zig test:

fn doSomethingWithRequiredError(_: anyerror) void {}
fn doSomethingWithOptionalError(_: ?anyerror) void {}

test "✅ Function call that takes required error compiles when called at runtime" {
    doSomethingWithRequiredError(error.OutOfMemory);
}

test "✅ Function call that takes required error compiles when called at compile time" {
    comptime doSomethingWithRequiredError(error.OutOfMemory);
}

test "✅ Function call that takes optional error compiles when called at runtime" {
    doSomethingWithOptionalError(error.OutOfMemory);
}

test "💥 Function call that takes optional error fails to compile when called at compile time" {
    // Comment this out and compilation will succeed
    comptime doSomethingWithOptionalError(error.OutOfMemory);
}

Expected Behavior

Running zig test on the program above compiles and executes the tests, or fails with a compilation error on the offending line.

Actual Behavior

Running zig test on the program above panics with the following message:

[snip]/zig/src/stage1/analyze.cpp:5884 in can_mutate_comptime_var_state. This is a bug in the Zig compiler.thread 2877888 panic: 
[snip]/zig/src/stage1.zig:178:5: 0x1044b114f in stage2_panic (zig1)
    @panic(ptr[0..len]);
    ^
???:?:?: 0x1096db95f in __Z9zig_panicPKcz (???)
???:?:?: 0x10532b933 in __ZL29can_mutate_comptime_var_stateP8ZigValue (???)
???:?:?: 0x10532b86f in __ZL29can_mutate_comptime_var_stateP8ZigValue (???)
???:?:?: 0x10532b44b in __Z17fn_eval_cacheableP5ScopeP7ZigType (???)
???:?:?: 0x105423ab3 in __ZL18ir_analyze_fn_callP9IrAnalyzeP5ScopeP7AstNodeP5ZigFnP7ZigTypeP13Stage1AirInstSA_S4_12CallModifierSA_S4_bPSA_mSA_P9ResultLoc (???)
???:?:?: 0x10542336f in __ZL22ir_analyze_fn_call_srcP9IrAnalyzeP17Stage1ZirInstCallP5ZigFnP7ZigTypeP13Stage1AirInstS8_P7AstNode12CallModifier (???)
???:?:?: 0x1053e2e23 in __ZL27ir_analyze_instruction_callP9IrAnalyzeP17Stage1ZirInstCall (???)
???:?:?: 0x1053d7693 in __ZL27ir_analyze_instruction_baseP9IrAnalyzeP13Stage1ZirInst (???)
???:?:?: 0x1053d68e7 in __Z10ir_analyzeP7CodeGenP9Stage1ZirP9Stage1AirPmS5_P7ZigTypeP7AstNodeP8ZigValueP5ZigFn (???)
???:?:?: 0x105339ee7 in __ZL13analyze_fn_irP7CodeGenP5ZigFnP7AstNode (???)
???:?:?: 0x1053288b3 in __ZL15analyze_fn_bodyP7CodeGenP5ZigFn (???)
???:?:?: 0x10532972b in __Z16semantic_analyzeP7CodeGen (???)
???:?:?: 0x10538a8eb in __ZL15gen_root_sourceP7CodeGen (???)
???:?:?: 0x10538942b in __Z20codegen_build_objectP7CodeGen (???)
???:?:?: 0x10545c1b7 in _zig_stage1_build_object (???)
[snip]/zig/src/stage1.zig:149:32: 0x1048501c7 in Module.build_object (zig1)
        zig_stage1_build_object(mod);
                               ^
[snip]/zig/src/Compilation.zig:5010:31: 0x10482df37 in Compilation.updateStage1Module (zig1)
    stage1_module.build_object();
                              ^
[snip]/zig/src/Compilation.zig:3076:36: 0x10462901b in Compilation.processOneJob (zig1)
            comp.updateStage1Module(main_progress_node) catch |err| {
                                   ^
[snip]/zig/src/Compilation.zig:2685:30: 0x10461e21b in Compilation.performAllTheWork (zig1)
            try processOneJob(self, work_item, main_progress_node);
                             ^
[snip]/zig/src/Compilation.zig:2090:31: 0x104618bbf in Compilation.update (zig1)
    try comp.performAllTheWork();
                              ^
[snip]/zig/src/main.zig:3094:20: 0x1045b7e9f in main.updateModule (zig1)
    try comp.update();
                   ^
[snip]/zig/src/main.zig:2781:17: 0x1044e6313 in main.buildOutputType (zig1)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
[snip]/zig/src/main.zig:216:31: 0x1044afe9b in main.mainArgs (zig1)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
[snip]/zig/src/stage1.zig:48:24: 0x1044af7c3 in main (zig1)
        stage2.mainArgs(gpa, arena, args) catch unreachable;
@alinebee alinebee added the bug Observed behavior contradicts documented or intended behavior label Mar 20, 2022
alinebee referenced this issue in alinebee/AZiggierWorld Mar 20, 2022
`MockRepository.Instance.init` was causing the compiler to panic on zig 0.9.1 and the latest master, owing to it taking an optional error as a parameter. Runtime calls to such functions do compile, but comptime calls (such as the ones that create the global mock repository instances in memory.zig) do not.

I'm still working on a reduced test case for this compiler bug. To unblock myself until it's fixed, I've simplified MockRepository to take a boolean `read_should_fail` parameter instead, which is sufficient for the current unit tests.
@Vexu Vexu added the stage1 The process of building from source via WebAssembly and the C backend. label Mar 20, 2022
@Vexu Vexu added this to the 0.11.0 milestone Mar 20, 2022
Vexu added a commit to Vexu/zig that referenced this issue Mar 28, 2024
Vexu added a commit to Vexu/zig that referenced this issue Mar 28, 2024
Vexu added a commit to Vexu/zig that referenced this issue Mar 28, 2024
Vexu added a commit to Vexu/zig that referenced this issue Mar 28, 2024
@andrewrk andrewrk modified the milestones: 0.15.0, 0.12.0 Mar 28, 2024
Rexicon226 pushed a commit to Rexicon226/zig that referenced this issue Mar 29, 2024
TUSF pushed a commit to TUSF/zig that referenced this issue May 9, 2024
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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

3 participants