zig icon indicating copy to clipboard operation
zig copied to clipboard

Comptime known bitcount ops don't return `comptime_int`

Open rpkak opened this issue 1 year ago • 1 comments

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 avatar Jul 24 '24 21:07 rpkak

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)

nektro avatar Jul 24 '24 22:07 nektro

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.

rpkak avatar Jul 25 '24 04:07 rpkak