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

Comptime known bitcount ops don't return comptime_int #20780

Open
rpkak opened this issue Jul 24, 2024 · 2 comments · May be fixed by #20837
Open

Comptime known bitcount ops don't return comptime_int #20780

rpkak opened this issue Jul 24, 2024 · 2 comments · May be fixed by #20837
Labels
bug Observed behavior contradicts documented or intended behavior docs
Milestone

Comments

@rpkak
Copy link

rpkak commented Jul 24, 2024

Zig Version

0.10.0, 0.10.1, 0.11.0, 0.12.0, 0.13.0, 0.14.0-dev.541+2e8acdf6f

Steps to Reproduce and Observed Behavior

Execute this code:

export fn foo() void {
    @compileLog(@TypeOf(@popCount(@as(u64, 0xabc))));
    @compileLog(@TypeOf(@clz(@as(u64, 0xabc))));
    @compileLog(@TypeOf(@ctz(@as(u64, 0xabc))));
}

The comptime log is:

@as(type, u7)
@as(type, u7)
@as(type, u7)

Expected Behavior

According to the docs of @clz, @ctz and @popCount the comptime log should be this because @as(u64, 0xabc) is a comptime known integer:

@as(type, comptime_int)
@as(type, comptime_int)
@as(type, comptime_int)
In zig 0.9.0 and 0.9.1 this seems to work

the output of

export fn foo() void {
    @compileLog(@TypeOf(@popCount(u64, 0xabc)));
    @compileLog(@TypeOf(@clz(u64, 0xabc)));
    @compileLog(@TypeOf(@ctz(u64, 0xabc)));
}

is this:

| comptime_int
| comptime_int
| comptime_int

In my option the current behaviour is good and this is a documentation error.

@rpkak rpkak added the bug Observed behavior contradicts documented or intended behavior label Jul 24, 2024
@nektro
Copy link
Contributor

nektro commented Jul 24, 2024

a number not being comptime_int does not mean it's necessarily not comptime-known. it is a u7 because that is what is returned from std.math.IntFittingRange(0, 64)

@rpkak
Copy link
Author

rpkak commented Jul 25, 2024

a number not being comptime_int does not mean it's necessarily not comptime-known. it is a u7 because that is what is returned from std.math.IntFittingRange(0, 64)

This current compiler behavior makes sense to me, but following sentence is included in the docs of all three builtins:
If operand is a comptime-known integer, the return type is comptime_int.

@Vexu Vexu added the docs label Jul 25, 2024
@Vexu Vexu added this to the 0.14.0 milestone Jul 25, 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 docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants