Commit bfe02ff61a

tison <wander4096@gmail.com>
2023-05-24 02:01:48
make `@boolToInt` always return a u1
Signed-off-by: tison <wander4096@gmail.com>
1 parent dcc1b4f
Changed files (6)
doc/langref.html.in
@@ -7850,10 +7850,6 @@ comptime {
       Converts {#syntax#}true{#endsyntax#} to {#syntax#}@as(u1, 1){#endsyntax#} and {#syntax#}false{#endsyntax#} to
                   {#syntax#}@as(u1, 0){#endsyntax#}.
       </p>
-      <p>
-      If the value is known at compile-time, the return type is {#syntax#}comptime_int{#endsyntax#}
-          instead of {#syntax#}u1{#endsyntax#}.
-      </p>
       {#header_close#}
 
       {#header_open|@bitSizeOf#}
lib/std/math/ilogb.zig
@@ -48,7 +48,7 @@ fn ilogbX(comptime T: type, x: T) i32 {
         }
 
         // offset sign bit, exponent bits, and integer bit (if present) + bias
-        const offset = 1 + exponentBits + @boolToInt(T == f80) - exponentBias;
+        const offset = 1 + exponentBits + @as(comptime_int, @boolToInt(T == f80)) - exponentBias;
         return offset - @intCast(i32, @clz(u));
     }
 
lib/std/math.zig
@@ -1684,7 +1684,7 @@ pub fn break_f80(x: f80) F80 {
 pub inline fn sign(i: anytype) @TypeOf(i) {
     const T = @TypeOf(i);
     return switch (@typeInfo(T)) {
-        .Int, .ComptimeInt => @as(T, @boolToInt(i > 0)) - @boolToInt(i < 0),
+        .Int, .ComptimeInt => @as(T, @boolToInt(i > 0)) - @as(T, @boolToInt(i < 0)),
         .Float, .ComptimeFloat => @intToFloat(T, @boolToInt(i > 0)) - @intToFloat(T, @boolToInt(i < 0)),
         .Vector => |vinfo| blk: {
             switch (@typeInfo(vinfo.child)) {
src/Sema.zig
@@ -18445,9 +18445,9 @@ fn zirBoolToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
     const inst_data = sema.code.instructions.items(.data)[inst].un_node;
     const operand = try sema.resolveInst(inst_data.operand);
     if (try sema.resolveMaybeUndefVal(operand)) |val| {
-        if (val.isUndef()) return sema.addConstUndef(Type.initTag(.u1));
-        const bool_ints = [2]Air.Inst.Ref{ .zero, .one };
-        return bool_ints[@boolToInt(val.toBool())];
+        if (val.isUndef()) return sema.addConstUndef(Type.u1);
+        if (val.toBool()) return sema.addConstant(Type.u1, Value.one);
+        return sema.addConstant(Type.u1, Value.zero);
     }
     return block.addUnOp(.bool_to_int, operand);
 }
src/translate_c.zig
@@ -2494,6 +2494,7 @@ fn transCCast(
 
         if (isBoolRes(src_int_expr)) {
             src_int_expr = try Tag.bool_to_int.create(c.arena, src_int_expr);
+            return Tag.as.create(c.arena, .{ .lhs = dst_node, .rhs = src_int_expr });
         }
 
         switch (cIntTypeCmp(dst_type, src_type)) {
test/behavior/bool.zig
@@ -1,6 +1,7 @@
 const std = @import("std");
 const builtin = @import("builtin");
 const expect = std.testing.expect;
+const expectEqual = std.testing.expectEqual;
 
 test "bool literals" {
     try expect(true);
@@ -12,14 +13,22 @@ test "cast bool to int" {
 
     const t = true;
     const f = false;
-    try expect(@boolToInt(t) == @as(u32, 1));
-    try expect(@boolToInt(f) == @as(u32, 0));
+    try expectEqual(@as(u32, 1), @boolToInt(t));
+    try expectEqual(@as(u32, 0), @boolToInt(f));
+    try expectEqual(-1, @bitCast(i1, @boolToInt(t)));
+    try expectEqual(0, @bitCast(i1, @boolToInt(f)));
+    try expectEqual(u1, @TypeOf(@boolToInt(t)));
+    try expectEqual(u1, @TypeOf(@boolToInt(f)));
     try nonConstCastBoolToInt(t, f);
 }
 
 fn nonConstCastBoolToInt(t: bool, f: bool) !void {
-    try expect(@boolToInt(t) == @as(u32, 1));
-    try expect(@boolToInt(f) == @as(u32, 0));
+    try expectEqual(@as(u32, 1), @boolToInt(t));
+    try expectEqual(@as(u32, 0), @boolToInt(f));
+    try expectEqual(@as(i1, -1), @bitCast(i1, @boolToInt(t)));
+    try expectEqual(@as(i1, 0), @bitCast(i1, @boolToInt(f)));
+    try expectEqual(u1, @TypeOf(@boolToInt(t)));
+    try expectEqual(u1, @TypeOf(@boolToInt(f)));
 }
 
 test "bool cmp" {