| author | |
| committer | |
| log | 25ec2dbc1e2302d1138749262b588d3e438fcd55 |
| tree | 34187fbd88b2e9b046f50cea93f482a191bc3248 |
| parent | 2b7781d82ad8d2234b89257676670957e005f214 |
26 files changed, 149 insertions(+), 150 deletions(-)
lib/std/builtin.zig+8-1| ... | ... | @@ -209,7 +209,7 @@ pub const TypeInfo = union(enum) { |
| 209 | 209 | /// This data structure is used by the Zig language code generation and |
| 210 | 210 | /// therefore must be kept in sync with the compiler implementation. |
| 211 | 211 | pub const Int = struct { |
| 212 | is_signed: bool, | |
| 212 | signedness: Signedness, | |
| 213 | 213 | bits: comptime_int, |
| 214 | 214 | }; |
| 215 | 215 | |
| ... | ... | @@ -438,6 +438,13 @@ pub const Endian = enum { |
| 438 | 438 | Little, |
| 439 | 439 | }; |
| 440 | 440 | |
| 441 | /// This data structure is used by the Zig language code generation and | |
| 442 | /// therefore must be kept in sync with the compiler implementation. | |
| 443 | pub const Signedness = enum { | |
| 444 | signed, | |
| 445 | unsigned, | |
| 446 | }; | |
| 447 | ||
| 441 | 448 | /// This data structure is used by the Zig language code generation and |
| 442 | 449 | /// therefore must be kept in sync with the compiler implementation. |
| 443 | 450 | pub const OutputMode = enum { |
lib/std/fmt.zig+1-1| ... | ... | @@ -1028,7 +1028,7 @@ pub fn formatInt( |
| 1028 | 1028 | if (a == 0) break; |
| 1029 | 1029 | } |
| 1030 | 1030 | |
| 1031 | if (value_info.is_signed) { | |
| 1031 | if (value_info.signedness == .signed) { | |
| 1032 | 1032 | if (value < 0) { |
| 1033 | 1033 | // Negative integer |
| 1034 | 1034 | index -= 1; |
lib/std/io/serialization.zig+1-1| ... | ... | @@ -73,7 +73,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, |
| 73 | 73 | |
| 74 | 74 | if (int_size == 1) { |
| 75 | 75 | if (t_bit_count == 8) return @bitCast(T, buffer[0]); |
| 76 | const PossiblySignedByte = std.meta.Int(if (@typeInfo(T).Int.is_signed) .signed else .unsigned, 8); | |
| 76 | const PossiblySignedByte = std.meta.Int(@typeInfo(T).Int.signedness, 8); | |
| 77 | 77 | return @truncate(T, @bitCast(PossiblySignedByte, buffer[0])); |
| 78 | 78 | } |
| 79 | 79 |
lib/std/json/write_stream.zig+1-1| ... | ... | @@ -167,7 +167,7 @@ pub fn WriteStream(comptime OutStream: type, comptime max_depth: usize) type { |
| 167 | 167 | self.popState(); |
| 168 | 168 | return; |
| 169 | 169 | } |
| 170 | if (value < 4503599627370496 and (!info.is_signed or value > -4503599627370496)) { | |
| 170 | if (value < 4503599627370496 and (info.signedness == .unsigned or value > -4503599627370496)) { | |
| 171 | 171 | try self.stream.print("{}", .{value}); |
| 172 | 172 | self.popState(); |
| 173 | 173 | return; |
lib/std/leb128.zig+2-2| ... | ... | @@ -297,8 +297,8 @@ test "deserialize unsigned LEB128" { |
| 297 | 297 | |
| 298 | 298 | fn test_write_leb128(value: anytype) !void { |
| 299 | 299 | const T = @TypeOf(value); |
| 300 | const t_signed = @typeInfo(T).Int.is_signed; | |
| 301 | const signedness = if (t_signed) .signed else .unsigned; | |
| 300 | const signedness = @typeInfo(T).Int.signedness; | |
| 301 | const t_signed = signedness == .signed; | |
| 302 | 302 | |
| 303 | 303 | const writeStream = if (t_signed) writeILEB128 else writeULEB128; |
| 304 | 304 | const readStream = if (t_signed) readILEB128 else readULEB128; |
lib/std/math.zig+22-22| ... | ... | @@ -313,7 +313,7 @@ pub fn floatExponentBits(comptime T: type) comptime_int { |
| 313 | 313 | pub fn Min(comptime A: type, comptime B: type) type { |
| 314 | 314 | switch (@typeInfo(A)) { |
| 315 | 315 | .Int => |a_info| switch (@typeInfo(B)) { |
| 316 | .Int => |b_info| if (!a_info.is_signed and !b_info.is_signed) { | |
| 316 | .Int => |b_info| if (a_info.signedness == .unsigned and b_info.signedness == .unsigned) { | |
| 317 | 317 | if (a_info.bits < b_info.bits) { |
| 318 | 318 | return A; |
| 319 | 319 | } else { |
| ... | ... | @@ -450,7 +450,7 @@ pub fn shl(comptime T: type, a: T, shift_amt: anytype) T { |
| 450 | 450 | } |
| 451 | 451 | }; |
| 452 | 452 | |
| 453 | if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) { | |
| 453 | if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.signedness == .signed) { | |
| 454 | 454 | if (shift_amt < 0) { |
| 455 | 455 | return a >> casted_shift_amt; |
| 456 | 456 | } |
| ... | ... | @@ -490,7 +490,7 @@ pub fn shr(comptime T: type, a: T, shift_amt: anytype) T { |
| 490 | 490 | } |
| 491 | 491 | }; |
| 492 | 492 | |
| 493 | if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) { | |
| 493 | if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.signedness == .signed) { | |
| 494 | 494 | if (shift_amt < 0) { |
| 495 | 495 | return a << casted_shift_amt; |
| 496 | 496 | } |
| ... | ... | @@ -518,12 +518,12 @@ test "math.shr" { |
| 518 | 518 | pub fn rotr(comptime T: type, x: T, r: anytype) T { |
| 519 | 519 | if (@typeInfo(T) == .Vector) { |
| 520 | 520 | const C = @typeInfo(T).Vector.child; |
| 521 | if (@typeInfo(C).Int.is_signed) { | |
| 521 | if (@typeInfo(C).Int.signedness == .signed) { | |
| 522 | 522 | @compileError("cannot rotate signed integers"); |
| 523 | 523 | } |
| 524 | 524 | const ar = @intCast(Log2Int(C), @mod(r, @typeInfo(C).Int.bits)); |
| 525 | 525 | return (x >> @splat(@typeInfo(T).Vector.len, ar)) | (x << @splat(@typeInfo(T).Vector.len, 1 + ~ar)); |
| 526 | } else if (@typeInfo(T).Int.is_signed) { | |
| 526 | } else if (@typeInfo(T).Int.signedness == .signed) { | |
| 527 | 527 | @compileError("cannot rotate signed integer"); |
| 528 | 528 | } else { |
| 529 | 529 | const ar = @mod(r, @typeInfo(T).Int.bits); |
| ... | ... | @@ -546,12 +546,12 @@ test "math.rotr" { |
| 546 | 546 | pub fn rotl(comptime T: type, x: T, r: anytype) T { |
| 547 | 547 | if (@typeInfo(T) == .Vector) { |
| 548 | 548 | const C = @typeInfo(T).Vector.child; |
| 549 | if (@typeInfo(C).Int.is_signed) { | |
| 549 | if (@typeInfo(C).Int.signedness == .signed) { | |
| 550 | 550 | @compileError("cannot rotate signed integers"); |
| 551 | 551 | } |
| 552 | 552 | const ar = @intCast(Log2Int(C), @mod(r, @typeInfo(C).Int.bits)); |
| 553 | 553 | return (x << @splat(@typeInfo(T).Vector.len, ar)) | (x >> @splat(@typeInfo(T).Vector.len, 1 +% ~ar)); |
| 554 | } else if (@typeInfo(T).Int.is_signed) { | |
| 554 | } else if (@typeInfo(T).Int.signedness == .signed) { | |
| 555 | 555 | @compileError("cannot rotate signed integer"); |
| 556 | 556 | } else { |
| 557 | 557 | const ar = @mod(r, @typeInfo(T).Int.bits); |
| ... | ... | @@ -585,7 +585,7 @@ pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) t |
| 585 | 585 | if (from == 0 and to == 0) { |
| 586 | 586 | return u0; |
| 587 | 587 | } |
| 588 | const sign: std.meta.Signedness = if (from < 0) .signed else .unsigned; | |
| 588 | const sign: std.builtin.Signedness = if (from < 0) .signed else .unsigned; | |
| 589 | 589 | const largest_positive_integer = max(if (from < 0) (-from) - 1 else from, to); // two's complement |
| 590 | 590 | const base = log2(largest_positive_integer); |
| 591 | 591 | const upper = (1 << base) - 1; |
| ... | ... | @@ -658,7 +658,7 @@ fn testOverflow() void { |
| 658 | 658 | pub fn absInt(x: anytype) !@TypeOf(x) { |
| 659 | 659 | const T = @TypeOf(x); |
| 660 | 660 | comptime assert(@typeInfo(T) == .Int); // must pass an integer to absInt |
| 661 | comptime assert(@typeInfo(T).Int.is_signed); // must pass a signed integer to absInt | |
| 661 | comptime assert(@typeInfo(T).Int.signedness == .signed); // must pass a signed integer to absInt | |
| 662 | 662 | |
| 663 | 663 | if (x == minInt(@TypeOf(x))) { |
| 664 | 664 | return error.Overflow; |
| ... | ... | @@ -691,7 +691,7 @@ fn testAbsFloat() void { |
| 691 | 691 | pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T { |
| 692 | 692 | @setRuntimeSafety(false); |
| 693 | 693 | if (denominator == 0) return error.DivisionByZero; |
| 694 | if (@typeInfo(T) == .Int and @typeInfo(T).Int.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; | |
| 694 | if (@typeInfo(T) == .Int and @typeInfo(T).Int.signedness == .signed and numerator == minInt(T) and denominator == -1) return error.Overflow; | |
| 695 | 695 | return @divTrunc(numerator, denominator); |
| 696 | 696 | } |
| 697 | 697 | |
| ... | ... | @@ -712,7 +712,7 @@ fn testDivTrunc() void { |
| 712 | 712 | pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T { |
| 713 | 713 | @setRuntimeSafety(false); |
| 714 | 714 | if (denominator == 0) return error.DivisionByZero; |
| 715 | if (@typeInfo(T) == .Int and @typeInfo(T).Int.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; | |
| 715 | if (@typeInfo(T) == .Int and @typeInfo(T).Int.signedness == .signed and numerator == minInt(T) and denominator == -1) return error.Overflow; | |
| 716 | 716 | return @divFloor(numerator, denominator); |
| 717 | 717 | } |
| 718 | 718 | |
| ... | ... | @@ -786,7 +786,7 @@ fn testDivCeil() void { |
| 786 | 786 | pub fn divExact(comptime T: type, numerator: T, denominator: T) !T { |
| 787 | 787 | @setRuntimeSafety(false); |
| 788 | 788 | if (denominator == 0) return error.DivisionByZero; |
| 789 | if (@typeInfo(T) == .Int and @typeInfo(T).Int.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; | |
| 789 | if (@typeInfo(T) == .Int and @typeInfo(T).Int.signedness == .signed and numerator == minInt(T) and denominator == -1) return error.Overflow; | |
| 790 | 790 | const result = @divTrunc(numerator, denominator); |
| 791 | 791 | if (result * denominator != numerator) return error.UnexpectedRemainder; |
| 792 | 792 | return result; |
| ... | ... | @@ -892,7 +892,7 @@ test "math.absCast" { |
| 892 | 892 | /// Returns the negation of the integer parameter. |
| 893 | 893 | /// Result is a signed integer. |
| 894 | 894 | pub fn negateCast(x: anytype) !std.meta.Int(.signed, std.meta.bitCount(@TypeOf(x))) { |
| 895 | if (@typeInfo(@TypeOf(x)).Int.is_signed) return negate(x); | |
| 895 | if (@typeInfo(@TypeOf(x)).Int.signedness == .signed) return negate(x); | |
| 896 | 896 | |
| 897 | 897 | const int = std.meta.Int(.signed, std.meta.bitCount(@TypeOf(x))); |
| 898 | 898 | if (x > -minInt(int)) return error.Overflow; |
| ... | ... | @@ -981,11 +981,11 @@ fn testFloorPowerOfTwo() void { |
| 981 | 981 | /// Returns the next power of two (if the value is not already a power of two). |
| 982 | 982 | /// Only unsigned integers can be used. Zero is not an allowed input. |
| 983 | 983 | /// Result is a type with 1 more bit than the input type. |
| 984 | pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) std.meta.Int(if (@typeInfo(T).Int.is_signed) .signed else .unsigned, @typeInfo(T).Int.bits + 1) { | |
| 984 | pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) std.meta.Int(@typeInfo(T).Int.signedness, @typeInfo(T).Int.bits + 1) { | |
| 985 | 985 | comptime assert(@typeInfo(T) == .Int); |
| 986 | comptime assert(!@typeInfo(T).Int.is_signed); | |
| 986 | comptime assert(@typeInfo(T).Int.signedness == .unsigned); | |
| 987 | 987 | assert(value != 0); |
| 988 | comptime const PromotedType = std.meta.Int(if (@typeInfo(T).Int.is_signed) .signed else .unsigned, @typeInfo(T).Int.bits + 1); | |
| 988 | comptime const PromotedType = std.meta.Int(@typeInfo(T).Int.signedness, @typeInfo(T).Int.bits + 1); | |
| 989 | 989 | comptime const shiftType = std.math.Log2Int(PromotedType); |
| 990 | 990 | return @as(PromotedType, 1) << @intCast(shiftType, @typeInfo(T).Int.bits - @clz(T, value - 1)); |
| 991 | 991 | } |
| ... | ... | @@ -996,8 +996,8 @@ pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) std.meta.Int(if (@typeI |
| 996 | 996 | pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) { |
| 997 | 997 | comptime assert(@typeInfo(T) == .Int); |
| 998 | 998 | const info = @typeInfo(T).Int; |
| 999 | comptime assert(!info.is_signed); | |
| 1000 | comptime const PromotedType = std.meta.Int(if (info.is_signed) .signed else .unsigned, info.bits + 1); | |
| 999 | comptime assert(info.signedness == .unsigned); | |
| 1000 | comptime const PromotedType = std.meta.Int(info.signedness, info.bits + 1); | |
| 1001 | 1001 | comptime const overflowBit = @as(PromotedType, 1) << info.bits; |
| 1002 | 1002 | var x = ceilPowerOfTwoPromote(T, value); |
| 1003 | 1003 | if (overflowBit & x != 0) { |
| ... | ... | @@ -1090,13 +1090,13 @@ pub fn maxInt(comptime T: type) comptime_int { |
| 1090 | 1090 | const info = @typeInfo(T); |
| 1091 | 1091 | const bit_count = info.Int.bits; |
| 1092 | 1092 | if (bit_count == 0) return 0; |
| 1093 | return (1 << (bit_count - @boolToInt(info.Int.is_signed))) - 1; | |
| 1093 | return (1 << (bit_count - @boolToInt(info.Int.signedness == .signed))) - 1; | |
| 1094 | 1094 | } |
| 1095 | 1095 | |
| 1096 | 1096 | pub fn minInt(comptime T: type) comptime_int { |
| 1097 | 1097 | const info = @typeInfo(T); |
| 1098 | 1098 | const bit_count = info.Int.bits; |
| 1099 | if (!info.Int.is_signed) return 0; | |
| 1099 | if (info.Int.signedness == .unsigned) return 0; | |
| 1100 | 1100 | if (bit_count == 0) return 0; |
| 1101 | 1101 | return -(1 << (bit_count - 1)); |
| 1102 | 1102 | } |
| ... | ... | @@ -1143,8 +1143,8 @@ test "max value type" { |
| 1143 | 1143 | testing.expect(x == 2147483647); |
| 1144 | 1144 | } |
| 1145 | 1145 | |
| 1146 | pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(if (@typeInfo(T).Int.is_signed) .signed else .unsigned, @typeInfo(T).Int.bits * 2) { | |
| 1147 | const ResultInt = std.meta.Int(if (@typeInfo(T).Int.is_signed) .signed else .unsigned, @typeInfo(T).Int.bits * 2); | |
| 1146 | pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(@typeInfo(T).Int.signedness, @typeInfo(T).Int.bits * 2) { | |
| 1147 | const ResultInt = std.meta.Int(@typeInfo(T).Int.signedness, @typeInfo(T).Int.bits * 2); | |
| 1148 | 1148 | return @as(ResultInt, a) * @as(ResultInt, b); |
| 1149 | 1149 | } |
| 1150 | 1150 |
lib/std/math/big.zig+1-1| ... | ... | @@ -17,7 +17,7 @@ pub const Log2Limb = std.math.Log2Int(Limb); |
| 17 | 17 | comptime { |
| 18 | 18 | assert(std.math.floorPowerOfTwo(usize, limb_info.bits) == limb_info.bits); |
| 19 | 19 | assert(limb_info.bits <= 64); // u128 set is unsupported |
| 20 | assert(limb_info.is_signed == false); | |
| 20 | assert(limb_info.signedness == .unsigned); | |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | test "" { |
lib/std/math/big/int.zig+9-9| ... | ... | @@ -24,7 +24,7 @@ pub fn calcLimbLen(scalar: anytype) usize { |
| 24 | 24 | const T = @TypeOf(scalar); |
| 25 | 25 | switch (@typeInfo(T)) { |
| 26 | 26 | .Int => |info| { |
| 27 | const UT = if (info.is_signed) std.meta.Int(.unsigned, info.bits - 1) else T; | |
| 27 | const UT = if (info.signedness == .signed) std.meta.Int(.unsigned, info.bits - 1) else T; | |
| 28 | 28 | return @sizeOf(UT) / @sizeOf(Limb); |
| 29 | 29 | }, |
| 30 | 30 | .ComptimeInt => { |
| ... | ... | @@ -187,7 +187,7 @@ pub const Mutable = struct { |
| 187 | 187 | |
| 188 | 188 | switch (@typeInfo(T)) { |
| 189 | 189 | .Int => |info| { |
| 190 | const UT = if (info.is_signed) std.meta.Int(.unsigned, info.bits - 1) else T; | |
| 190 | const UT = if (info.signedness == .signed) std.meta.Int(.unsigned, info.bits - 1) else T; | |
| 191 | 191 | |
| 192 | 192 | const needed_limbs = @sizeOf(UT) / @sizeOf(Limb); |
| 193 | 193 | assert(needed_limbs <= self.limbs.len); // value too big |
| ... | ... | @@ -1054,22 +1054,22 @@ pub const Const = struct { |
| 1054 | 1054 | return bits; |
| 1055 | 1055 | } |
| 1056 | 1056 | |
| 1057 | pub fn fitsInTwosComp(self: Const, is_signed: bool, bit_count: usize) bool { | |
| 1057 | pub fn fitsInTwosComp(self: Const, signedness: std.builtin.Signedness, bit_count: usize) bool { | |
| 1058 | 1058 | if (self.eqZero()) { |
| 1059 | 1059 | return true; |
| 1060 | 1060 | } |
| 1061 | if (!is_signed and !self.positive) { | |
| 1061 | if (signedness == .unsigned and !self.positive) { | |
| 1062 | 1062 | return false; |
| 1063 | 1063 | } |
| 1064 | 1064 | |
| 1065 | const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and is_signed); | |
| 1065 | const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and signedness == .signed); | |
| 1066 | 1066 | return bit_count >= req_bits; |
| 1067 | 1067 | } |
| 1068 | 1068 | |
| 1069 | 1069 | /// Returns whether self can fit into an integer of the requested type. |
| 1070 | 1070 | pub fn fits(self: Const, comptime T: type) bool { |
| 1071 | 1071 | const info = @typeInfo(T).Int; |
| 1072 | return self.fitsInTwosComp(info.is_signed, info.bits); | |
| 1072 | return self.fitsInTwosComp(info.signedness, info.bits); | |
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | 1075 | /// Returns the approximate size of the integer in the given base. Negative values accommodate for |
| ... | ... | @@ -1110,7 +1110,7 @@ pub const Const = struct { |
| 1110 | 1110 | } |
| 1111 | 1111 | } |
| 1112 | 1112 | |
| 1113 | if (!info.is_signed) { | |
| 1113 | if (info.signedness == .unsigned) { | |
| 1114 | 1114 | return if (self.positive) @intCast(T, r) else error.NegativeIntoUnsigned; |
| 1115 | 1115 | } else { |
| 1116 | 1116 | if (self.positive) { |
| ... | ... | @@ -1558,8 +1558,8 @@ pub const Managed = struct { |
| 1558 | 1558 | return self.toConst().bitCountTwosComp(); |
| 1559 | 1559 | } |
| 1560 | 1560 | |
| 1561 | pub fn fitsInTwosComp(self: Managed, is_signed: bool, bit_count: usize) bool { | |
| 1562 | return self.toConst().fitsInTwosComp(is_signed, bit_count); | |
| 1561 | pub fn fitsInTwosComp(self: Managed, signedness: std.builtin.Signedness, bit_count: usize) bool { | |
| 1562 | return self.toConst().fitsInTwosComp(signedness, bit_count); | |
| 1563 | 1563 | } |
| 1564 | 1564 | |
| 1565 | 1565 | /// Returns whether self can fit into an integer of the requested type. |
lib/std/math/powi.zig+1-1| ... | ... | @@ -48,7 +48,7 @@ pub fn powi(comptime T: type, x: T, y: T) (error{ |
| 48 | 48 | // powi(x, y) = Overflow for for y >= @sizeOf(x) - 1 y > 0 |
| 49 | 49 | // powi(x, y) = Underflow for for y > @sizeOf(x) - 1 y < 0 |
| 50 | 50 | const bit_size = @sizeOf(T) * 8; |
| 51 | if (info.Int.is_signed) { | |
| 51 | if (info.Int.signedness == .signed) { | |
| 52 | 52 | if (x == -1) { |
| 53 | 53 | // powi(-1, y) = -1 for for y an odd integer |
| 54 | 54 | // powi(-1, y) = 1 for for y an even integer |
lib/std/meta.zig+2-7| ... | ... | @@ -718,15 +718,10 @@ pub fn declList(comptime Namespace: type, comptime Decl: type) []const *const De |
| 718 | 718 | |
| 719 | 719 | pub const IntType = @compileError("replaced by std.meta.Int"); |
| 720 | 720 | |
| 721 | pub const Signedness = enum { | |
| 722 | unsigned, | |
| 723 | signed, | |
| 724 | }; | |
| 725 | ||
| 726 | pub fn Int(comptime signedness: Signedness, comptime bit_count: u16) type { | |
| 721 | pub fn Int(comptime signedness: builtin.Signedness, comptime bit_count: u16) type { | |
| 727 | 722 | return @Type(TypeInfo{ |
| 728 | 723 | .Int = .{ |
| 729 | .is_signed = signedness == .signed, | |
| 724 | .signedness = signedness, | |
| 730 | 725 | .bits = bit_count, |
| 731 | 726 | }, |
| 732 | 727 | }); |
lib/std/meta/trait.zig+2-2| ... | ... | @@ -195,7 +195,7 @@ test "std.meta.trait.isPacked" { |
| 195 | 195 | |
| 196 | 196 | pub fn isUnsignedInt(comptime T: type) bool { |
| 197 | 197 | return switch (@typeInfo(T)) { |
| 198 | .Int => |i| !i.is_signed, | |
| 198 | .Int => |i| i.signedness == .unsigned, | |
| 199 | 199 | else => false, |
| 200 | 200 | }; |
| 201 | 201 | } |
| ... | ... | @@ -210,7 +210,7 @@ test "isUnsignedInt" { |
| 210 | 210 | pub fn isSignedInt(comptime T: type) bool { |
| 211 | 211 | return switch (@typeInfo(T)) { |
| 212 | 212 | .ComptimeInt => true, |
| 213 | .Int => |i| i.is_signed, | |
| 213 | .Int => |i| i.signedness == .signed, | |
| 214 | 214 | else => false, |
| 215 | 215 | }; |
| 216 | 216 | } |
lib/std/os.zig+1-6| ... | ... | @@ -4868,12 +4868,7 @@ pub fn sendfile( |
| 4868 | 4868 | var total_written: usize = 0; |
| 4869 | 4869 | |
| 4870 | 4870 | // Prevents EOVERFLOW. |
| 4871 | const size_t = @Type(std.builtin.TypeInfo{ | |
| 4872 | .Int = .{ | |
| 4873 | .is_signed = false, | |
| 4874 | .bits = @typeInfo(usize).Int.bits - 1, | |
| 4875 | }, | |
| 4876 | }); | |
| 4871 | const size_t = std.meta.Int(.unsigned, @typeInfo(usize).Int.bits - 1); | |
| 4877 | 4872 | const max_count = switch (std.Target.current.os.tag) { |
| 4878 | 4873 | .linux => 0x7ffff000, |
| 4879 | 4874 | .macos, .ios, .watchos, .tvos => math.maxInt(i32), |
lib/std/os/linux.zig+2-2| ... | ... | @@ -777,7 +777,7 @@ pub fn seteuid(euid: uid_t) usize { |
| 777 | 777 | // The setresuid(2) man page says that if -1 is passed the corresponding |
| 778 | 778 | // id will not be changed. Since uid_t is unsigned, this wraps around to the |
| 779 | 779 | // max value in C. |
| 780 | comptime assert(@typeInfo(uid_t) == .Int and !@typeInfo(uid_t).Int.is_signed); | |
| 780 | comptime assert(@typeInfo(uid_t) == .Int and @typeInfo(uid_t).Int.signedness == .unsigned); | |
| 781 | 781 | return setresuid(std.math.maxInt(uid_t), euid, std.math.maxInt(uid_t)); |
| 782 | 782 | } |
| 783 | 783 | |
| ... | ... | @@ -788,7 +788,7 @@ pub fn setegid(egid: gid_t) usize { |
| 788 | 788 | // The setresgid(2) man page says that if -1 is passed the corresponding |
| 789 | 789 | // id will not be changed. Since gid_t is unsigned, this wraps around to the |
| 790 | 790 | // max value in C. |
| 791 | comptime assert(@typeInfo(uid_t) == .Int and !@typeInfo(uid_t).Int.is_signed); | |
| 791 | comptime assert(@typeInfo(uid_t) == .Int and @typeInfo(uid_t).Int.signedness == .unsigned); | |
| 792 | 792 | return setresgid(std.math.maxInt(gid_t), egid, std.math.maxInt(gid_t)); |
| 793 | 793 | } |
| 794 | 794 |
lib/std/packed_int_array.zig+2-2| ... | ... | @@ -332,7 +332,7 @@ test "PackedIntArray" { |
| 332 | 332 | comptime var bits = 0; |
| 333 | 333 | inline while (bits <= max_bits) : (bits += 1) { |
| 334 | 334 | //alternate unsigned and signed |
| 335 | const sign: std.meta.Signedness = if (bits % 2 == 0) .signed else .unsigned; | |
| 335 | const sign: builtin.Signedness = if (bits % 2 == 0) .signed else .unsigned; | |
| 336 | 336 | const I = std.meta.Int(sign, bits); |
| 337 | 337 | |
| 338 | 338 | const PackedArray = PackedIntArray(I, int_count); |
| ... | ... | @@ -384,7 +384,7 @@ test "PackedIntSlice" { |
| 384 | 384 | comptime var bits = 0; |
| 385 | 385 | inline while (bits <= max_bits) : (bits += 1) { |
| 386 | 386 | //alternate unsigned and signed |
| 387 | const sign: std.meta.Signedness = if (bits % 2 == 0) .signed else .unsigned; | |
| 387 | const sign: builtin.Signedness = if (bits % 2 == 0) .signed else .unsigned; | |
| 388 | 388 | const I = std.meta.Int(sign, bits); |
| 389 | 389 | const P = PackedIntSlice(I); |
| 390 | 390 |
lib/std/rand.zig+9-9| ... | ... | @@ -69,7 +69,7 @@ pub const Random = struct { |
| 69 | 69 | /// Constant-time implementation off `uintLessThan`. |
| 70 | 70 | /// The results of this function may be biased. |
| 71 | 71 | pub fn uintLessThanBiased(r: *Random, comptime T: type, less_than: T) T { |
| 72 | comptime assert(@typeInfo(T).Int.is_signed == false); | |
| 72 | comptime assert(@typeInfo(T).Int.signedness == .unsigned); | |
| 73 | 73 | const bits = @typeInfo(T).Int.bits; |
| 74 | 74 | comptime assert(bits <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation! |
| 75 | 75 | assert(0 < less_than); |
| ... | ... | @@ -89,7 +89,7 @@ pub const Random = struct { |
| 89 | 89 | /// this function is guaranteed to return. |
| 90 | 90 | /// If you need deterministic runtime bounds, use `uintLessThanBiased`. |
| 91 | 91 | pub fn uintLessThan(r: *Random, comptime T: type, less_than: T) T { |
| 92 | comptime assert(@typeInfo(T).Int.is_signed == false); | |
| 92 | comptime assert(@typeInfo(T).Int.signedness == .unsigned); | |
| 93 | 93 | const bits = @typeInfo(T).Int.bits; |
| 94 | 94 | comptime assert(bits <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation! |
| 95 | 95 | assert(0 < less_than); |
| ... | ... | @@ -129,7 +129,7 @@ pub const Random = struct { |
| 129 | 129 | /// Constant-time implementation off `uintAtMost`. |
| 130 | 130 | /// The results of this function may be biased. |
| 131 | 131 | pub fn uintAtMostBiased(r: *Random, comptime T: type, at_most: T) T { |
| 132 | assert(@typeInfo(T).Int.is_signed == false); | |
| 132 | assert(@typeInfo(T).Int.signedness == .unsigned); | |
| 133 | 133 | if (at_most == maxInt(T)) { |
| 134 | 134 | // have the full range |
| 135 | 135 | return r.int(T); |
| ... | ... | @@ -141,7 +141,7 @@ pub const Random = struct { |
| 141 | 141 | /// See `uintLessThan`, which this function uses in most cases, |
| 142 | 142 | /// for commentary on the runtime of this function. |
| 143 | 143 | pub fn uintAtMost(r: *Random, comptime T: type, at_most: T) T { |
| 144 | assert(@typeInfo(T).Int.is_signed == false); | |
| 144 | assert(@typeInfo(T).Int.signedness == .unsigned); | |
| 145 | 145 | if (at_most == maxInt(T)) { |
| 146 | 146 | // have the full range |
| 147 | 147 | return r.int(T); |
| ... | ... | @@ -154,7 +154,7 @@ pub const Random = struct { |
| 154 | 154 | pub fn intRangeLessThanBiased(r: *Random, comptime T: type, at_least: T, less_than: T) T { |
| 155 | 155 | assert(at_least < less_than); |
| 156 | 156 | const info = @typeInfo(T).Int; |
| 157 | if (info.is_signed) { | |
| 157 | if (info.signedness == .signed) { | |
| 158 | 158 | // Two's complement makes this math pretty easy. |
| 159 | 159 | const UnsignedT = std.meta.Int(.unsigned, info.bits); |
| 160 | 160 | const lo = @bitCast(UnsignedT, at_least); |
| ... | ... | @@ -173,7 +173,7 @@ pub const Random = struct { |
| 173 | 173 | pub fn intRangeLessThan(r: *Random, comptime T: type, at_least: T, less_than: T) T { |
| 174 | 174 | assert(at_least < less_than); |
| 175 | 175 | const info = @typeInfo(T).Int; |
| 176 | if (info.is_signed) { | |
| 176 | if (info.signedness == .signed) { | |
| 177 | 177 | // Two's complement makes this math pretty easy. |
| 178 | 178 | const UnsignedT = std.meta.Int(.unsigned, info.bits); |
| 179 | 179 | const lo = @bitCast(UnsignedT, at_least); |
| ... | ... | @@ -191,7 +191,7 @@ pub const Random = struct { |
| 191 | 191 | pub fn intRangeAtMostBiased(r: *Random, comptime T: type, at_least: T, at_most: T) T { |
| 192 | 192 | assert(at_least <= at_most); |
| 193 | 193 | const info = @typeInfo(T).Int; |
| 194 | if (info.is_signed) { | |
| 194 | if (info.signedness == .signed) { | |
| 195 | 195 | // Two's complement makes this math pretty easy. |
| 196 | 196 | const UnsignedT = std.meta.Int(.unsigned, info.bits); |
| 197 | 197 | const lo = @bitCast(UnsignedT, at_least); |
| ... | ... | @@ -210,7 +210,7 @@ pub const Random = struct { |
| 210 | 210 | pub fn intRangeAtMost(r: *Random, comptime T: type, at_least: T, at_most: T) T { |
| 211 | 211 | assert(at_least <= at_most); |
| 212 | 212 | const info = @typeInfo(T).Int; |
| 213 | if (info.is_signed) { | |
| 213 | if (info.signedness == .signed) { | |
| 214 | 214 | // Two's complement makes this math pretty easy. |
| 215 | 215 | const UnsignedT = std.meta.Int(.unsigned, info.bits); |
| 216 | 216 | const lo = @bitCast(UnsignedT, at_least); |
| ... | ... | @@ -288,7 +288,7 @@ pub const Random = struct { |
| 288 | 288 | /// into an integer 0 <= result < less_than. |
| 289 | 289 | /// This function introduces a minor bias. |
| 290 | 290 | pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T { |
| 291 | comptime assert(@typeInfo(T).Int.is_signed == false); | |
| 291 | comptime assert(@typeInfo(T).Int.signedness == .unsigned); | |
| 292 | 292 | const bits = @typeInfo(T).Int.bits; |
| 293 | 293 | const T2 = std.meta.Int(.unsigned, bits * 2); |
| 294 | 294 |
lib/std/start.zig+2-2| ... | ... | @@ -325,7 +325,7 @@ pub fn callMain() u8 { |
| 325 | 325 | return 0; |
| 326 | 326 | }, |
| 327 | 327 | .Int => |info| { |
| 328 | if (info.bits != 8 or info.is_signed) { | |
| 328 | if (info.bits != 8 or info.signedness == .signed) { | |
| 329 | 329 | @compileError(bad_main_ret); |
| 330 | 330 | } |
| 331 | 331 | return root.main(); |
| ... | ... | @@ -341,7 +341,7 @@ pub fn callMain() u8 { |
| 341 | 341 | switch (@typeInfo(@TypeOf(result))) { |
| 342 | 342 | .Void => return 0, |
| 343 | 343 | .Int => |info| { |
| 344 | if (info.bits != 8 or info.is_signed) { | |
| 344 | if (info.bits != 8 or info.signedness == .signed) { | |
| 345 | 345 | @compileError(bad_main_ret); |
| 346 | 346 | } |
| 347 | 347 | return result; |
src/Module.zig+4-4| ... | ... | @@ -2633,7 +2633,7 @@ pub fn cmpNumeric( |
| 2633 | 2633 | dest_float_type = lhs.ty; |
| 2634 | 2634 | } else { |
| 2635 | 2635 | const int_info = lhs.ty.intInfo(self.getTarget()); |
| 2636 | lhs_bits = int_info.bits + @boolToInt(!int_info.signed and dest_int_is_signed); | |
| 2636 | lhs_bits = int_info.bits + @boolToInt(int_info.signedness == .unsigned and dest_int_is_signed); | |
| 2637 | 2637 | } |
| 2638 | 2638 | |
| 2639 | 2639 | var rhs_bits: usize = undefined; |
| ... | ... | @@ -2668,7 +2668,7 @@ pub fn cmpNumeric( |
| 2668 | 2668 | dest_float_type = rhs.ty; |
| 2669 | 2669 | } else { |
| 2670 | 2670 | const int_info = rhs.ty.intInfo(self.getTarget()); |
| 2671 | rhs_bits = int_info.bits + @boolToInt(!int_info.signed and dest_int_is_signed); | |
| 2671 | rhs_bits = int_info.bits + @boolToInt(int_info.signedness == .unsigned and dest_int_is_signed); | |
| 2672 | 2672 | } |
| 2673 | 2673 | |
| 2674 | 2674 | const dest_type = if (dest_float_type) |ft| ft else blk: { |
| ... | ... | @@ -2817,9 +2817,9 @@ pub fn coerce(self: *Module, scope: *Scope, dest_type: Type, inst: *Inst) !*Inst |
| 2817 | 2817 | |
| 2818 | 2818 | const src_info = inst.ty.intInfo(self.getTarget()); |
| 2819 | 2819 | const dst_info = dest_type.intInfo(self.getTarget()); |
| 2820 | if ((src_info.signed == dst_info.signed and dst_info.bits >= src_info.bits) or | |
| 2820 | if ((src_info.signedness == dst_info.signedness and dst_info.bits >= src_info.bits) or | |
| 2821 | 2821 | // small enough unsigned ints can get casted to large enough signed ints |
| 2822 | (src_info.signed and !dst_info.signed and dst_info.bits > src_info.bits)) | |
| 2822 | (src_info.signedness == .signed and dst_info.signedness == .unsigned and dst_info.bits > src_info.bits)) | |
| 2823 | 2823 | { |
| 2824 | 2824 | const b = try self.requireRuntimeBlock(scope, inst.src); |
| 2825 | 2825 | return self.addUnOp(b, inst.src, dest_type, .intcast, inst); |
src/codegen.zig+9-15| ... | ... | @@ -203,7 +203,7 @@ pub fn generateSymbol( |
| 203 | 203 | .Int => { |
| 204 | 204 | // TODO populate .debug_info for the integer |
| 205 | 205 | const info = typed_value.ty.intInfo(bin_file.options.target); |
| 206 | if (info.bits == 8 and !info.signed) { | |
| 206 | if (info.bits == 8 and info.signedness == .unsigned) { | |
| 207 | 207 | const x = typed_value.val.toUnsignedInt(); |
| 208 | 208 | try code.append(@intCast(u8, x)); |
| 209 | 209 | return Result{ .appended = {} }; |
| ... | ... | @@ -920,7 +920,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { |
| 920 | 920 | const operand = try self.resolveInst(inst.operand); |
| 921 | 921 | const info_a = inst.operand.ty.intInfo(self.target.*); |
| 922 | 922 | const info_b = inst.base.ty.intInfo(self.target.*); |
| 923 | if (info_a.signed != info_b.signed) | |
| 923 | if (info_a.signedness != info_b.signedness) | |
| 924 | 924 | return self.fail(inst.base.src, "TODO gen intcast sign safety in semantic analysis", .{}); |
| 925 | 925 | |
| 926 | 926 | if (info_a.bits == info_b.bits) |
| ... | ... | @@ -1780,7 +1780,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { |
| 1780 | 1780 | fn genCmp(self: *Self, inst: *ir.Inst.BinOp, op: math.CompareOperator) !MCValue { |
| 1781 | 1781 | // No side effects, so if it's unreferenced, do nothing. |
| 1782 | 1782 | if (inst.base.isUnused()) |
| 1783 | return MCValue.dead; | |
| 1783 | return MCValue{ .dead = {} }; | |
| 1784 | 1784 | switch (arch) { |
| 1785 | 1785 | .x86_64 => { |
| 1786 | 1786 | try self.code.ensureCapacity(self.code.items.len + 8); |
| ... | ... | @@ -1800,11 +1800,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { |
| 1800 | 1800 | |
| 1801 | 1801 | try self.genX8664BinMathCode(inst.base.src, inst.base.ty, dst_mcv, src_mcv, 7, 0x38); |
| 1802 | 1802 | const info = inst.lhs.ty.intInfo(self.target.*); |
| 1803 | if (info.signed) { | |
| 1804 | return MCValue{ .compare_flags_signed = op }; | |
| 1805 | } else { | |
| 1806 | return MCValue{ .compare_flags_unsigned = op }; | |
| 1807 | } | |
| 1803 | return switch (info.signedness) { | |
| 1804 | .signed => MCValue{ .compare_flags_signed = op }, | |
| 1805 | .unsigned => MCValue{ .compare_flags_unsigned = op }, | |
| 1806 | }; | |
| 1808 | 1807 | }, |
| 1809 | 1808 | else => return self.fail(inst.base.src, "TODO implement cmp for {}", .{self.target.cpu.arch}), |
| 1810 | 1809 | } |
| ... | ... | @@ -2904,12 +2903,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { |
| 2904 | 2903 | switch (mcv) { |
| 2905 | 2904 | .immediate => |imm| { |
| 2906 | 2905 | // This immediate is unsigned. |
| 2907 | const U = @Type(.{ | |
| 2908 | .Int = .{ | |
| 2909 | .bits = ti.bits - @boolToInt(ti.is_signed), | |
| 2910 | .is_signed = false, | |
| 2911 | }, | |
| 2912 | }); | |
| 2906 | const U = std.meta.Int(.unsigned, ti.bits - @boolToInt(ti.signedness == .signed)); | |
| 2913 | 2907 | if (imm >= math.maxInt(U)) { |
| 2914 | 2908 | return MCValue{ .register = try self.copyToTmpRegister(inst.src, mcv) }; |
| 2915 | 2909 | } |
| ... | ... | @@ -2949,7 +2943,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type { |
| 2949 | 2943 | }, |
| 2950 | 2944 | .Int => { |
| 2951 | 2945 | const info = typed_value.ty.intInfo(self.target.*); |
| 2952 | if (info.bits > ptr_bits or info.signed) { | |
| 2946 | if (info.bits > ptr_bits or info.signedness == .signed) { | |
| 2953 | 2947 | return self.fail(src, "TODO const int bigger than ptr and signed int", .{}); |
| 2954 | 2948 | } |
| 2955 | 2949 | return MCValue{ .immediate = typed_value.val.toUnsignedInt() }; |
src/link/Elf.zig+4-1| ... | ... | @@ -2443,7 +2443,10 @@ fn addDbgInfoType(self: *Elf, ty: Type, dbg_info_buffer: *std.ArrayList(u8)) !vo |
| 2443 | 2443 | try dbg_info_buffer.ensureCapacity(dbg_info_buffer.items.len + 12); |
| 2444 | 2444 | dbg_info_buffer.appendAssumeCapacity(abbrev_base_type); |
| 2445 | 2445 | // DW.AT_encoding, DW.FORM_data1 |
| 2446 | dbg_info_buffer.appendAssumeCapacity(if (info.signed) DW.ATE_signed else DW.ATE_unsigned); | |
| 2446 | dbg_info_buffer.appendAssumeCapacity(switch (info.signedness) { | |
| 2447 | .signed => DW.ATE_signed, | |
| 2448 | .unsigned => DW.ATE_unsigned, | |
| 2449 | }); | |
| 2447 | 2450 | // DW.AT_byte_size, DW.FORM_data1 |
| 2448 | 2451 | dbg_info_buffer.appendAssumeCapacity(@intCast(u8, ty.abiSize(self.base.options.target))); |
| 2449 | 2452 | // DW.AT_name, DW.FORM_string |
src/stage1/ir.cpp+8-6| ... | ... | @@ -25306,11 +25306,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy |
| 25306 | 25306 | ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 2); |
| 25307 | 25307 | result->data.x_struct.fields = fields; |
| 25308 | 25308 | |
| 25309 | // is_signed: bool | |
| 25310 | ensure_field_index(result->type, "is_signed", 0); | |
| 25309 | // is_signed: Signedness | |
| 25310 | ensure_field_index(result->type, "signedness", 0); | |
| 25311 | 25311 | fields[0]->special = ConstValSpecialStatic; |
| 25312 | fields[0]->type = ira->codegen->builtin_types.entry_bool; | |
| 25313 | fields[0]->data.x_bool = type_entry->data.integral.is_signed; | |
| 25312 | fields[0]->type = get_builtin_type(ira->codegen, "Signedness"); | |
| 25313 | bigint_init_unsigned(&fields[0]->data.x_enum_tag, !type_entry->data.integral.is_signed); | |
| 25314 | 25314 | // bits: u8 |
| 25315 | 25315 | ensure_field_index(result->type, "bits", 1); |
| 25316 | 25316 | fields[1]->special = ConstValSpecialStatic; |
| ... | ... | @@ -26073,9 +26073,11 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI |
| 26073 | 26073 | BigInt *bi = get_const_field_lit_int(ira, source_instr->source_node, payload, "bits", 1); |
| 26074 | 26074 | if (bi == nullptr) |
| 26075 | 26075 | return ira->codegen->invalid_inst_gen->value->type; |
| 26076 | bool is_signed; | |
| 26077 | if ((err = get_const_field_bool(ira, source_instr->source_node, payload, "is_signed", 0, &is_signed))) | |
| 26076 | ZigValue *value = get_const_field(ira, source_instr->source_node, payload, "signedness", 0); | |
| 26077 | if (value == nullptr) | |
| 26078 | 26078 | return ira->codegen->invalid_inst_gen->value->type; |
| 26079 | assert(value->type == get_builtin_type(ira->codegen, "Signedness")); | |
| 26080 | bool is_signed = !bigint_as_u32(&value->data.x_enum_tag); | |
| 26079 | 26081 | return get_int_type(ira->codegen, is_signed, bigint_as_u32(bi)); |
| 26080 | 26082 | } |
| 26081 | 26083 | case ZigTypeIdFloat: |
src/type.zig+27-27| ... | ... | @@ -186,7 +186,7 @@ pub const Type = extern union { |
| 186 | 186 | // The target will not be branched upon, because we handled target-dependent cases above. |
| 187 | 187 | const info_a = a.intInfo(@as(Target, undefined)); |
| 188 | 188 | const info_b = b.intInfo(@as(Target, undefined)); |
| 189 | return info_a.signed == info_b.signed and info_a.bits == info_b.bits; | |
| 189 | return info_a.signedness == info_b.signedness and info_a.bits == info_b.bits; | |
| 190 | 190 | }, |
| 191 | 191 | .Array => { |
| 192 | 192 | if (a.arrayLen() != b.arrayLen()) |
| ... | ... | @@ -266,7 +266,7 @@ pub const Type = extern union { |
| 266 | 266 | // Remaining cases are arbitrary sized integers. |
| 267 | 267 | // The target will not be branched upon, because we handled target-dependent cases above. |
| 268 | 268 | const info = self.intInfo(@as(Target, undefined)); |
| 269 | std.hash.autoHash(&hasher, info.signed); | |
| 269 | std.hash.autoHash(&hasher, info.signedness); | |
| 270 | 270 | std.hash.autoHash(&hasher, info.bits); |
| 271 | 271 | } |
| 272 | 272 | }, |
| ... | ... | @@ -1908,7 +1908,7 @@ pub const Type = extern union { |
| 1908 | 1908 | } |
| 1909 | 1909 | |
| 1910 | 1910 | /// Asserts the type is an integer. |
| 1911 | pub fn intInfo(self: Type, target: Target) struct { signed: bool, bits: u16 } { | |
| 1911 | pub fn intInfo(self: Type, target: Target) struct { signedness: std.builtin.Signedness, bits: u16 } { | |
| 1912 | 1912 | return switch (self.tag()) { |
| 1913 | 1913 | .f16, |
| 1914 | 1914 | .f32, |
| ... | ... | @@ -1958,26 +1958,26 @@ pub const Type = extern union { |
| 1958 | 1958 | .empty_struct, |
| 1959 | 1959 | => unreachable, |
| 1960 | 1960 | |
| 1961 | .int_unsigned => .{ .signed = false, .bits = self.cast(Payload.IntUnsigned).?.bits }, | |
| 1962 | .int_signed => .{ .signed = true, .bits = self.cast(Payload.IntSigned).?.bits }, | |
| 1963 | .u8 => .{ .signed = false, .bits = 8 }, | |
| 1964 | .i8 => .{ .signed = true, .bits = 8 }, | |
| 1965 | .u16 => .{ .signed = false, .bits = 16 }, | |
| 1966 | .i16 => .{ .signed = true, .bits = 16 }, | |
| 1967 | .u32 => .{ .signed = false, .bits = 32 }, | |
| 1968 | .i32 => .{ .signed = true, .bits = 32 }, | |
| 1969 | .u64 => .{ .signed = false, .bits = 64 }, | |
| 1970 | .i64 => .{ .signed = true, .bits = 64 }, | |
| 1971 | .usize => .{ .signed = false, .bits = target.cpu.arch.ptrBitWidth() }, | |
| 1972 | .isize => .{ .signed = true, .bits = target.cpu.arch.ptrBitWidth() }, | |
| 1973 | .c_short => .{ .signed = true, .bits = CType.short.sizeInBits(target) }, | |
| 1974 | .c_ushort => .{ .signed = false, .bits = CType.ushort.sizeInBits(target) }, | |
| 1975 | .c_int => .{ .signed = true, .bits = CType.int.sizeInBits(target) }, | |
| 1976 | .c_uint => .{ .signed = false, .bits = CType.uint.sizeInBits(target) }, | |
| 1977 | .c_long => .{ .signed = true, .bits = CType.long.sizeInBits(target) }, | |
| 1978 | .c_ulong => .{ .signed = false, .bits = CType.ulong.sizeInBits(target) }, | |
| 1979 | .c_longlong => .{ .signed = true, .bits = CType.longlong.sizeInBits(target) }, | |
| 1980 | .c_ulonglong => .{ .signed = false, .bits = CType.ulonglong.sizeInBits(target) }, | |
| 1961 | .int_unsigned => .{ .signedness = .unsigned, .bits = self.cast(Payload.IntUnsigned).?.bits }, | |
| 1962 | .int_signed => .{ .signedness = .signed, .bits = self.cast(Payload.IntSigned).?.bits }, | |
| 1963 | .u8 => .{ .signedness = .unsigned, .bits = 8 }, | |
| 1964 | .i8 => .{ .signedness = .signed, .bits = 8 }, | |
| 1965 | .u16 => .{ .signedness = .unsigned, .bits = 16 }, | |
| 1966 | .i16 => .{ .signedness = .signed, .bits = 16 }, | |
| 1967 | .u32 => .{ .signedness = .unsigned, .bits = 32 }, | |
| 1968 | .i32 => .{ .signedness = .signed, .bits = 32 }, | |
| 1969 | .u64 => .{ .signedness = .unsigned, .bits = 64 }, | |
| 1970 | .i64 => .{ .signedness = .signed, .bits = 64 }, | |
| 1971 | .usize => .{ .signedness = .unsigned, .bits = target.cpu.arch.ptrBitWidth() }, | |
| 1972 | .isize => .{ .signedness = .signed, .bits = target.cpu.arch.ptrBitWidth() }, | |
| 1973 | .c_short => .{ .signedness = .signed, .bits = CType.short.sizeInBits(target) }, | |
| 1974 | .c_ushort => .{ .signedness = .unsigned, .bits = CType.ushort.sizeInBits(target) }, | |
| 1975 | .c_int => .{ .signedness = .signed, .bits = CType.int.sizeInBits(target) }, | |
| 1976 | .c_uint => .{ .signedness = .unsigned, .bits = CType.uint.sizeInBits(target) }, | |
| 1977 | .c_long => .{ .signedness = .signed, .bits = CType.long.sizeInBits(target) }, | |
| 1978 | .c_ulong => .{ .signedness = .unsigned, .bits = CType.ulong.sizeInBits(target) }, | |
| 1979 | .c_longlong => .{ .signedness = .signed, .bits = CType.longlong.sizeInBits(target) }, | |
| 1980 | .c_ulonglong => .{ .signedness = .unsigned, .bits = CType.ulonglong.sizeInBits(target) }, | |
| 1981 | 1981 | }; |
| 1982 | 1982 | } |
| 1983 | 1983 | |
| ... | ... | @@ -2869,7 +2869,7 @@ pub const Type = extern union { |
| 2869 | 2869 | assert(self.zigTypeTag() == .Int); |
| 2870 | 2870 | const info = self.intInfo(target); |
| 2871 | 2871 | |
| 2872 | if (!info.signed) { | |
| 2872 | if (info.signedness == .unsigned) { | |
| 2873 | 2873 | return Value.initTag(.zero); |
| 2874 | 2874 | } |
| 2875 | 2875 | |
| ... | ... | @@ -2902,13 +2902,13 @@ pub const Type = extern union { |
| 2902 | 2902 | assert(self.zigTypeTag() == .Int); |
| 2903 | 2903 | const info = self.intInfo(target); |
| 2904 | 2904 | |
| 2905 | if (info.signed and (info.bits - 1) <= std.math.maxInt(u6)) { | |
| 2905 | if (info.signedness == .signed and (info.bits - 1) <= std.math.maxInt(u6)) { | |
| 2906 | 2906 | const payload = try arena.allocator.create(Value.Payload.Int_i64); |
| 2907 | 2907 | payload.* = .{ |
| 2908 | 2908 | .int = (@as(i64, 1) << @truncate(u6, info.bits - 1)) - 1, |
| 2909 | 2909 | }; |
| 2910 | 2910 | return Value.initPayload(&payload.base); |
| 2911 | } else if (!info.signed and info.bits <= std.math.maxInt(u6)) { | |
| 2911 | } else if (info.signedness == .signed and info.bits <= std.math.maxInt(u6)) { | |
| 2912 | 2912 | const payload = try arena.allocator.create(Value.Payload.Int_u64); |
| 2913 | 2913 | payload.* = .{ |
| 2914 | 2914 | .int = (@as(u64, 1) << @truncate(u6, info.bits)) - 1, |
| ... | ... | @@ -2917,7 +2917,7 @@ pub const Type = extern union { |
| 2917 | 2917 | } |
| 2918 | 2918 | |
| 2919 | 2919 | var res = try std.math.big.int.Managed.initSet(&arena.allocator, 1); |
| 2920 | try res.shiftLeft(res, info.bits - @boolToInt(info.signed)); | |
| 2920 | try res.shiftLeft(res, info.bits - @boolToInt(info.signedness == .signed)); | |
| 2921 | 2921 | const one = std.math.big.int.Const{ |
| 2922 | 2922 | .limbs = &[_]std.math.big.Limb{1}, |
| 2923 | 2923 | .positive = true, |
src/value.zig+8-9| ... | ... | @@ -929,11 +929,10 @@ pub const Value = extern union { |
| 929 | 929 | .bool_true, |
| 930 | 930 | => { |
| 931 | 931 | const info = ty.intInfo(target); |
| 932 | if (info.signed) { | |
| 933 | return info.bits >= 2; | |
| 934 | } else { | |
| 935 | return info.bits >= 1; | |
| 936 | } | |
| 932 | return switch (info.signedness) { | |
| 933 | .signed => info.bits >= 2, | |
| 934 | .unsigned => info.bits >= 1, | |
| 935 | }; | |
| 937 | 936 | }, |
| 938 | 937 | |
| 939 | 938 | .int_u64 => switch (ty.zigTypeTag()) { |
| ... | ... | @@ -941,7 +940,7 @@ pub const Value = extern union { |
| 941 | 940 | const x = self.cast(Payload.Int_u64).?.int; |
| 942 | 941 | if (x == 0) return true; |
| 943 | 942 | const info = ty.intInfo(target); |
| 944 | const needed_bits = std.math.log2(x) + 1 + @boolToInt(info.signed); | |
| 943 | const needed_bits = std.math.log2(x) + 1 + @boolToInt(info.signedness == .signed); | |
| 945 | 944 | return info.bits >= needed_bits; |
| 946 | 945 | }, |
| 947 | 946 | .ComptimeInt => return true, |
| ... | ... | @@ -952,7 +951,7 @@ pub const Value = extern union { |
| 952 | 951 | const x = self.cast(Payload.Int_i64).?.int; |
| 953 | 952 | if (x == 0) return true; |
| 954 | 953 | const info = ty.intInfo(target); |
| 955 | if (!info.signed and x < 0) | |
| 954 | if (info.signedness == .unsigned and x < 0) | |
| 956 | 955 | return false; |
| 957 | 956 | @panic("TODO implement i64 intFitsInType"); |
| 958 | 957 | }, |
| ... | ... | @@ -962,7 +961,7 @@ pub const Value = extern union { |
| 962 | 961 | .int_big_positive => switch (ty.zigTypeTag()) { |
| 963 | 962 | .Int => { |
| 964 | 963 | const info = ty.intInfo(target); |
| 965 | return self.cast(Payload.IntBigPositive).?.asBigInt().fitsInTwosComp(info.signed, info.bits); | |
| 964 | return self.cast(Payload.IntBigPositive).?.asBigInt().fitsInTwosComp(info.signedness, info.bits); | |
| 966 | 965 | }, |
| 967 | 966 | .ComptimeInt => return true, |
| 968 | 967 | else => unreachable, |
| ... | ... | @@ -970,7 +969,7 @@ pub const Value = extern union { |
| 970 | 969 | .int_big_negative => switch (ty.zigTypeTag()) { |
| 971 | 970 | .Int => { |
| 972 | 971 | const info = ty.intInfo(target); |
| 973 | return self.cast(Payload.IntBigNegative).?.asBigInt().fitsInTwosComp(info.signed, info.bits); | |
| 972 | return self.cast(Payload.IntBigNegative).?.asBigInt().fitsInTwosComp(info.signedness, info.bits); | |
| 974 | 973 | }, |
| 975 | 974 | .ComptimeInt => return true, |
| 976 | 975 | else => unreachable, |
src/zir.zig+4-1| ... | ... | @@ -2687,7 +2687,10 @@ const EmitZIR = struct { |
| 2687 | 2687 | }, |
| 2688 | 2688 | .Int => { |
| 2689 | 2689 | const info = ty.intInfo(self.old_module.getTarget()); |
| 2690 | const signed = try self.emitPrimitive(src, if (info.signed) .@"true" else .@"false"); | |
| 2690 | const signed = try self.emitPrimitive(src, switch (info.signedness) { | |
| 2691 | .signed => .@"true", | |
| 2692 | .unsigned => .@"false", | |
| 2693 | }); | |
| 2691 | 2694 | const bits_payload = try self.arena.allocator.create(Value.Payload.Int_u64); |
| 2692 | 2695 | bits_payload.* = .{ .int = info.bits }; |
| 2693 | 2696 | const bits = try self.emitComptimeIntVal(src, Value.initPayload(&bits_payload.base)); |
test/compile_errors.zig+12-11| ... | ... | @@ -72,6 +72,18 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 72 | 72 | "tmp.zig:8:12: note: called from here", |
| 73 | 73 | }); |
| 74 | 74 | |
| 75 | cases.add("@Type with TypeInfo.Int", | |
| 76 | \\const builtin = @import("builtin"); | |
| 77 | \\export fn entry() void { | |
| 78 | \\ _ = @Type(builtin.TypeInfo.Int { | |
| 79 | \\ .signedness = .signed, | |
| 80 | \\ .bits = 8, | |
| 81 | \\ }); | |
| 82 | \\} | |
| 83 | , &[_][]const u8{ | |
| 84 | "tmp.zig:3:36: error: expected type 'std.builtin.TypeInfo', found 'std.builtin.Int'", | |
| 85 | }); | |
| 86 | ||
| 75 | 87 | cases.add("indexing a undefined slice at comptime", |
| 76 | 88 | \\comptime { |
| 77 | 89 | \\ var slice: []u8 = undefined; |
| ... | ... | @@ -1827,17 +1839,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 1827 | 1839 | "tmp.zig:4:15: error: unable to evaluate constant expression", |
| 1828 | 1840 | }); |
| 1829 | 1841 | |
| 1830 | cases.add("@Type with TypeInfo.Int", | |
| 1831 | \\const builtin = @import("builtin"); | |
| 1832 | \\export fn entry() void { | |
| 1833 | \\ _ = @Type(builtin.TypeInfo.Int { | |
| 1834 | \\ .is_signed = true, | |
| 1835 | \\ .bits = 8, | |
| 1836 | \\ }); | |
| 1837 | \\} | |
| 1838 | , &[_][]const u8{ | |
| 1839 | "tmp.zig:3:36: error: expected type 'std.builtin.TypeInfo', found 'std.builtin.Int'", | |
| 1840 | }); | |
| 1841 | 1842 | cases.add("wrong type for argument tuple to @asyncCall", |
| 1842 | 1843 | \\export fn entry1() void { |
| 1843 | 1844 | \\ var frame: @Frame(foo) = undefined; |
test/stage1/behavior/type.zig+6-6| ... | ... | @@ -31,12 +31,12 @@ test "Type.NoReturn" { |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | test "Type.Int" { |
| 34 | testing.expect(u1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = false, .bits = 1 } })); | |
| 35 | testing.expect(i1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = true, .bits = 1 } })); | |
| 36 | testing.expect(u8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = false, .bits = 8 } })); | |
| 37 | testing.expect(i8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = true, .bits = 8 } })); | |
| 38 | testing.expect(u64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = false, .bits = 64 } })); | |
| 39 | testing.expect(i64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = true, .bits = 64 } })); | |
| 34 | testing.expect(u1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 1 } })); | |
| 35 | testing.expect(i1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 1 } })); | |
| 36 | testing.expect(u8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 8 } })); | |
| 37 | testing.expect(i8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 8 } })); | |
| 38 | testing.expect(u64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .unsigned, .bits = 64 } })); | |
| 39 | testing.expect(i64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .signedness = .signed, .bits = 64 } })); | |
| 40 | 40 | testTypes(&[_]type{ u8, u32, i64 }); |
| 41 | 41 | } |
| 42 | 42 |
test/stage1/behavior/type_info.zig+1-1| ... | ... | @@ -28,7 +28,7 @@ test "type info: integer, floating point type info" { |
| 28 | 28 | fn testIntFloat() void { |
| 29 | 29 | const u8_info = @typeInfo(u8); |
| 30 | 30 | expect(u8_info == .Int); |
| 31 | expect(!u8_info.Int.is_signed); | |
| 31 | expect(u8_info.Int.signedness == .unsigned); | |
| 32 | 32 | expect(u8_info.Int.bits == 8); |
| 33 | 33 | |
| 34 | 34 | const f64_info = @typeInfo(f64); |