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

Odd/incorrect error message with @as in function with type parameter #10429

Open
euclidianAce opened this issue Dec 28, 2021 · 5 comments
Open
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@euclidianAce
Copy link

Zig Version

0.10.0-dev.74+426679574

Steps to Reproduce

try to zig run the following programs

// a.zig
pub fn main() void {
    const x: i32 = 4;
    const y = @as(f32, x) * 1.75;
    const z: i32 = @as(i32, y);
    @import("std").log.debug("x={}, y={}, z={}", .{ x, y, z });
}
// b.zig
pub fn main() void {
    const z = scale(4, f32, 1.75);
    @import("std").log.debug("z={}", .{z});
}

fn scale(n: i32, comptime Factor: type, factor: Factor) Factor {
    // first attempt was:
    //   return @intCast(i32, @as(Factor, n) * factor)
    // attempting to split it up like so results in the same error
    var new = @as(Factor, n);
    new *= factor;
    return @as(i32, new);
}

Expected Behavior

Both of the above programs to compile and run, logging the same value for z

Actual Behavior

a.zig runs fine, b.zig has a compile error:

$ zig run b.zig
.\b.zig:11:27: error: expected type 'f32', found 'i32'
    var new = @as(Factor, n);
                          ^
.\b.zig:3:20: note: called from here
    const z = scale(4, f32, 1.75);
                   ^
.\b.zig:2:20: note: called from here
pub fn main() void {
                   ^

After messing around, I think it could have something to do with the fact that @as doesn't like certain float to int conversions (hence the existence of @intToFloat and @floatToInt), and the error reporting gets confused and reports this as expected <destination type>, found <actual argument type> rather than something like unable to cast value of type <type>, to type <type>.

I've adapted the actual program to check at compile time whether Factor is integral or floating point and done the appropriate @intToFloat/@floatToInt, but this error message through me for a loop

@euclidianAce euclidianAce added the bug Observed behavior contradicts documented or intended behavior label Dec 28, 2021
@Vexu Vexu added the stage1 The process of building from source via WebAssembly and the C backend. label Dec 28, 2021
@Vexu Vexu added this to the 0.11.0 milestone Dec 28, 2021
@Vexu
Copy link
Member

Vexu commented Jan 19, 2023

Self-hosted also gets this wrong:

a.zig:41:5: error: expected type 'f32', found 'i32'
    var new = @as(Factor, n);
    ^~~~~~~~~~~~~~~~~~~~~~~~

@Vexu Vexu removed the stage1 The process of building from source via WebAssembly and the C backend. label Jan 19, 2023
@mlugg
Copy link
Member

mlugg commented Jan 19, 2023

Is it defined that the compiler will allow any int<->float conversion with @as when they're comptime-known to be safe? If not, it's actually a bug that a.zig compiles here. (But yes the error message could definitely be improved)

@Vexu
Copy link
Member

Vexu commented Jan 19, 2023

Is it defined that the compiler will allow any int<->float conversion with @as when they're comptime-known to be safe

Yes.

@nektro
Copy link
Contributor

nektro commented Jan 19, 2023

this test isnt happening at comptime

@mlugg
Copy link
Member

mlugg commented Jan 19, 2023

this test isnt happening at comptime

No, but in a.zig, x is comptime-known when y is defined, whereas in b.zig it's not (due to being a function parameter).

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

4 participants