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

unexpected truncation behavior with comptime_int larger than u64 range #9299

Closed
leesongun opened this issue Jul 4, 2021 · 0 comments
Closed

Comments

@leesongun
Copy link
Contributor

leesongun commented Jul 4, 2021

Trying to compile the following code

comptime {
    var a : u8 = @truncate(u1, 1 << 64);
}

results in (the value 139978047751248 changes every time)

./main.zig:2:18: error: integer value 139978047751248 cannot be coerced to type 'u8'
    var a : u8 = @truncate(u1, 1 << 64);
                 ^

This seems to be reason behind the following problems :

  1. The following code reaches unreachable.
test "large comptime_int truncation" {
    if (@truncate(u1, 0x1_0000_0000_0000_0000) != 0)
        unreachable;
}
  1. The following code prints 0.
const print = @import("std").debug.print;
test "print large comptime_int truncation" {
    print("{}", .{@truncate(u1, 0x1_0000_0000_0000_0001)});
}
leesongun added a commit to leesongun/zig that referenced this issue Jul 4, 2021
@Vexu Vexu closed this as completed in cff7ece Jul 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant