Skip to content

Commit bfe02ff

Browse files
authored
make @boolToInt always return a u1
Signed-off-by: tison <[email protected]>
1 parent dcc1b4f commit bfe02ff

File tree

6 files changed

+19
-13
lines changed

6 files changed

+19
-13
lines changed

doc/langref.html.in

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7850,10 +7850,6 @@ comptime {
78507850
Converts {#syntax#}true{#endsyntax#} to {#syntax#}@as(u1, 1){#endsyntax#} and {#syntax#}false{#endsyntax#} to
78517851
{#syntax#}@as(u1, 0){#endsyntax#}.
78527852
</p>
7853-
<p>
7854-
If the value is known at compile-time, the return type is {#syntax#}comptime_int{#endsyntax#}
7855-
instead of {#syntax#}u1{#endsyntax#}.
7856-
</p>
78577853
{#header_close#}
78587854

78597855
{#header_open|@bitSizeOf#}

lib/std/math.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ pub fn break_f80(x: f80) F80 {
16841684
pub inline fn sign(i: anytype) @TypeOf(i) {
16851685
const T = @TypeOf(i);
16861686
return switch (@typeInfo(T)) {
1687-
.Int, .ComptimeInt => @as(T, @boolToInt(i > 0)) - @boolToInt(i < 0),
1687+
.Int, .ComptimeInt => @as(T, @boolToInt(i > 0)) - @as(T, @boolToInt(i < 0)),
16881688
.Float, .ComptimeFloat => @intToFloat(T, @boolToInt(i > 0)) - @intToFloat(T, @boolToInt(i < 0)),
16891689
.Vector => |vinfo| blk: {
16901690
switch (@typeInfo(vinfo.child)) {

lib/std/math/ilogb.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn ilogbX(comptime T: type, x: T) i32 {
4848
}
4949

5050
// offset sign bit, exponent bits, and integer bit (if present) + bias
51-
const offset = 1 + exponentBits + @boolToInt(T == f80) - exponentBias;
51+
const offset = 1 + exponentBits + @as(comptime_int, @boolToInt(T == f80)) - exponentBias;
5252
return offset - @intCast(i32, @clz(u));
5353
}
5454

src/Sema.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18445,9 +18445,9 @@ fn zirBoolToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1844518445
const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1844618446
const operand = try sema.resolveInst(inst_data.operand);
1844718447
if (try sema.resolveMaybeUndefVal(operand)) |val| {
18448-
if (val.isUndef()) return sema.addConstUndef(Type.initTag(.u1));
18449-
const bool_ints = [2]Air.Inst.Ref{ .zero, .one };
18450-
return bool_ints[@boolToInt(val.toBool())];
18448+
if (val.isUndef()) return sema.addConstUndef(Type.u1);
18449+
if (val.toBool()) return sema.addConstant(Type.u1, Value.one);
18450+
return sema.addConstant(Type.u1, Value.zero);
1845118451
}
1845218452
return block.addUnOp(.bool_to_int, operand);
1845318453
}

src/translate_c.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,6 +2494,7 @@ fn transCCast(
24942494

24952495
if (isBoolRes(src_int_expr)) {
24962496
src_int_expr = try Tag.bool_to_int.create(c.arena, src_int_expr);
2497+
return Tag.as.create(c.arena, .{ .lhs = dst_node, .rhs = src_int_expr });
24972498
}
24982499

24992500
switch (cIntTypeCmp(dst_type, src_type)) {

test/behavior/bool.zig

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
33
const expect = std.testing.expect;
4+
const expectEqual = std.testing.expectEqual;
45

56
test "bool literals" {
67
try expect(true);
@@ -12,14 +13,22 @@ test "cast bool to int" {
1213

1314
const t = true;
1415
const f = false;
15-
try expect(@boolToInt(t) == @as(u32, 1));
16-
try expect(@boolToInt(f) == @as(u32, 0));
16+
try expectEqual(@as(u32, 1), @boolToInt(t));
17+
try expectEqual(@as(u32, 0), @boolToInt(f));
18+
try expectEqual(-1, @bitCast(i1, @boolToInt(t)));
19+
try expectEqual(0, @bitCast(i1, @boolToInt(f)));
20+
try expectEqual(u1, @TypeOf(@boolToInt(t)));
21+
try expectEqual(u1, @TypeOf(@boolToInt(f)));
1722
try nonConstCastBoolToInt(t, f);
1823
}
1924

2025
fn nonConstCastBoolToInt(t: bool, f: bool) !void {
21-
try expect(@boolToInt(t) == @as(u32, 1));
22-
try expect(@boolToInt(f) == @as(u32, 0));
26+
try expectEqual(@as(u32, 1), @boolToInt(t));
27+
try expectEqual(@as(u32, 0), @boolToInt(f));
28+
try expectEqual(@as(i1, -1), @bitCast(i1, @boolToInt(t)));
29+
try expectEqual(@as(i1, 0), @bitCast(i1, @boolToInt(f)));
30+
try expectEqual(u1, @TypeOf(@boolToInt(t)));
31+
try expectEqual(u1, @TypeOf(@boolToInt(f)));
2332
}
2433

2534
test "bool cmp" {

0 commit comments

Comments
 (0)