diff --git a/doc/langref.html.in b/doc/langref.html.in index 94aa7d6fc743..14dda686a94a 100644 --- a/doc/langref.html.in +++ b/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#}.
-- If the value is known at compile-time, the return type is {#syntax#}comptime_int{#endsyntax#} - instead of {#syntax#}u1{#endsyntax#}. -
{#header_close#} {#header_open|@bitSizeOf#} diff --git a/lib/std/math.zig b/lib/std/math.zig index 02b737610c52..4221118ba5cd 100644 --- a/lib/std/math.zig +++ b/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)) { diff --git a/lib/std/math/ilogb.zig b/lib/std/math/ilogb.zig index 88ae16840699..c091619f3a2e 100644 --- a/lib/std/math/ilogb.zig +++ b/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)); } diff --git a/src/Sema.zig b/src/Sema.zig index 138c19acf460..9178392d2708 100644 --- a/src/Sema.zig +++ b/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); } diff --git a/src/translate_c.zig b/src/translate_c.zig index bc7a1138da1c..becb2779b27c 100644 --- a/src/translate_c.zig +++ b/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)) { diff --git a/test/behavior/bool.zig b/test/behavior/bool.zig index d216d72a0564..8cfd87bf2165 100644 --- a/test/behavior/bool.zig +++ b/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" {