aboutsummaryrefslogtreecommitdiff
path: root/lib/std/meta.zig
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/meta.zig')
-rw-r--r--lib/std/meta.zig32
1 files changed, 27 insertions, 5 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 46d31a87c3..fd3e03bdbd 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -1096,7 +1096,7 @@ test "sizeof" {
pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal };
-fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: comptime_int, comptime radix: CIntLiteralRadix) type {
+fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type {
const signed_decimal = [_]type{ c_int, c_long, c_longlong };
const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong };
const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong };
@@ -1111,17 +1111,39 @@ fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime target: compt
var pos = mem.indexOfScalar(type, list, SuffixType).?;
while (pos < list.len) : (pos += 1) {
- if (target >= math.minInt(list[pos]) and target <= math.maxInt(list[pos])) {
+ if (number >= math.minInt(list[pos]) and number <= math.maxInt(list[pos])) {
return list[pos];
}
}
- @compileError("Integer literal does not fit in compatible types");
+ @compileError("Integer literal is too large");
}
/// Promote the type of an integer literal until it fits as C would.
/// This is for translate-c and is not intended for general use.
-pub fn promoteIntLiteral(comptime SuffixType: type, comptime target: comptime_int, comptime radix: CIntLiteralRadix) PromoteIntLiteralReturnType(SuffixType, target, radix) {
- return target;
+pub fn promoteIntLiteral(
+ comptime SuffixType: type,
+ comptime number: comptime_int,
+ comptime radix: CIntLiteralRadix,
+) PromoteIntLiteralReturnType(SuffixType, number, radix) {
+ return number;
+}
+
+test "promoteIntLiteral" {
+ const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hexadecimal);
+ testing.expectEqual(c_uint, @TypeOf(signed_hex));
+
+ if (math.maxInt(c_longlong) == math.maxInt(c_int)) return;
+
+ const signed_decimal = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .decimal);
+ const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hexadecimal);
+
+ if (math.maxInt(c_long) > math.maxInt(c_int)) {
+ testing.expectEqual(c_long, @TypeOf(signed_decimal));
+ testing.expectEqual(c_ulong, @TypeOf(unsigned));
+ } else {
+ testing.expectEqual(c_longlong, @TypeOf(signed_decimal));
+ testing.expectEqual(c_ulonglong, @TypeOf(unsigned));
+ }
}
/// For a given function type, returns a tuple type which fields will