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

wrong type inference #19379

Closed
dimdin opened this issue Mar 21, 2024 · 4 comments · Fixed by #19413
Closed

wrong type inference #19379

dimdin opened this issue Mar 21, 2024 · 4 comments · Fixed by #19413
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@dimdin
Copy link

dimdin commented Mar 21, 2024

Zig Version

0.12.0-dev.3381+7057bffc1

Steps to Reproduce and Observed Behavior

To reproduce run:

pub fn main() !void {
    const olength: u32 = 1;
    const precision: ?usize = null;
    const exponent: i32 = -1; 
    const req_bytes = 2 + @max(@abs(exponent) + olength, precision orelse 0);
    _ = req_bytes;
}
❯ zig run test.zig
test.zig:5:25: error: overflow of integer type 'u2' with value '4'
    const req_bytes = 2 + @max(@abs(exponent) + olength, precision orelse 0);
                      ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is an actual library line:

2 + @max(@abs(f.exponent) + olength, precision orelse 0);

It happens only in comptime

Expected Behavior

result type must be u32 or usize instead of u2

@dimdin dimdin added the bug Observed behavior contradicts documented or intended behavior label Mar 21, 2024
@xdBronch
Copy link
Contributor

xdBronch commented Mar 21, 2024

since all the arguments to @max are known at comptime it returns the smallest fitting integer type that can represent its value, that being a u2 in this case
see related issues #18365 #16139 #14039

@dimdin
Copy link
Author

dimdin commented Mar 21, 2024

It behaves differently in comptime and in runtime.
Code that is perfect in runtime cannot be used in comptime.
I came to this while trying to format a float at comptime, and zig float format currently is broken.

@xdBronch
Copy link
Contributor

i understand the problem statement. i believe the fix here is to provide an intermediate type coercion in the line that errors e.g. 2 + @as(u32, @max(@abs(f.exponent) + olength, precision orelse 0))

@dimdin
Copy link
Author

dimdin commented Mar 21, 2024

I agree. The line came from this merge: #19229

tiehuis added a commit to tiehuis/zig that referenced this issue Mar 23, 2024
tiehuis added a commit to tiehuis/zig that referenced this issue Mar 23, 2024
tiehuis added a commit to tiehuis/zig that referenced this issue Mar 23, 2024
@Vexu Vexu added this to the 0.12.0 milestone Mar 24, 2024
@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label Mar 24, 2024
Vexu pushed a commit that referenced this issue Mar 24, 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 standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants