Skip to content

Unable to determine comptime value from if-statement inside switch #4255

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

Closed
jeffkdev opened this issue Jan 21, 2020 · 1 comment
Closed

Unable to determine comptime value from if-statement inside switch #4255

jeffkdev opened this issue Jan 21, 2020 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@jeffkdev
Copy link

jeffkdev commented Jan 21, 2020

pub fn main() void {
    var condition = true;
    var wave_type :u32 = 2;
    var sample_f32 : f32 = switch(wave_type) {
        0 => if(condition) 0.5 else -0.5,
        1 => 100.0,
        2 => 200.0,
        3 => 300.0,
        else => unreachable,
    };

    var sample_i32 : i32 = switch(wave_type) {
        0 => if(condition) 2 else 3,
        1 => 100,
        2 => 200,
        3 => 300,
        else => unreachable,
    };
}

Both switch statements result in compile errors:
.\scratch.zig:5:14: error: values of type 'comptime_float' must be comptime known
0 => if(condition) 0.5 else -0.5,
^
I was thinking it should be able to determine the type inside the if statements from the result of the switch statement.

Casting the values using @as(f32,0.5), etc is a workaround for now. I couldn't find another open issue for this, but feel free to close if duplicate.

0.5.0 +a6f6d8d2f

@hryx
Copy link
Contributor

hryx commented Jan 21, 2020

Related to #3750, I think. The rules about peer type resolution and result location type seem to be fuzzy when the type inference is nested, at the moment.

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Jan 28, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Jan 28, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
Vexu added a commit to Vexu/zig that referenced this issue Dec 31, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 31, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Jan 1, 2023
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