Skip to content

The new builtin function result type inference doesn't consistently work inside array/vector initializer. #16226

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
IntegratedQuantum opened this issue Jun 26, 2023 · 1 comment · Fixed by #16228
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@IntegratedQuantum
Copy link
Contributor

IntegratedQuantum commented Jun 26, 2023

Zig Version

0.11.0-dev.3833+0a868dacd

Steps to Reproduce and Observed Behavior

fn takesAVector(vec: @Vector(1, f64)) void {
	_ = vec;
}
fn takesAnArray(arr: [1]f64) void {
	_ = arr;
}

pub fn main() void {
    var x: u64 = 0;
    var arr = [1]f64{@floatFromInt(x)}; // Works
    _ = arr;
    // _ = [1]f64{@floatFromInt(x)}; // Error
    // _ = takesAnArray([1]f64{@floatFromInt(x)}); // Error
    var vec = @Vector(1, f64){@floatFromInt(x)}; // Works
    _ = vec;
    // _ = @Vector(1, f64){@floatFromInt(x)}; // Error
    // _ = @Vector(1, f64){1} + @Vector(1, f64){@floatFromInt(x)}; // Error
    // _ = takesAVector(@Vector(1, f64){@floatFromInt(x)}); // Error
}

Uncommenting any of the above leads to the following error:

$ zig run test.zig
test.zig:5:17: error: @floatFromInt must have a known result type
     _ = [1]f64{@floatFromInt(x)}; // Error
                ^~~~~~~~~~~~~~~~

Expected Behavior

Given that the type is explicitly given in front of the initializer, the compiler should know what the field types are.

@IntegratedQuantum IntegratedQuantum added the bug Observed behavior contradicts documented or intended behavior label Jun 26, 2023
@mlugg
Copy link
Member

mlugg commented Jun 26, 2023

Note that this is only an issue when the result is discarded (const y = [1]f64{@floatFromInt(x)} works fine).

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

Successfully merging a pull request may close this issue.

3 participants