From 7f0ba01a0e7ddcf3f2736d202c4b6d64e181d204 Mon Sep 17 00:00:00 2001 From: seanthegleaming Date: Mon, 30 Sep 2024 11:24:40 -0400 Subject: [PATCH 1/6] Add comptimeKnown to std.meta --- lib/std/meta.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index e7ea5b5f0efd..cbb53ae83964 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1322,3 +1322,46 @@ test hasUniqueRepresentation { try testing.expect(hasUniqueRepresentation(@Vector(std.simd.suggestVectorLength(u8) orelse 1, u8))); try testing.expect(@sizeOf(@Vector(3, u8)) == 3 or !hasUniqueRepresentation(@Vector(3, u8))); } + +/// Returns true if the given value of is known at comptime +pub inline fn comptimeKnown(value: anytype) bool { + const TupleType = @TypeOf(.{value}); + const tuple_fields = @typeInfo(TupleType).@"struct".fields; + const value_field = tuple_fields[0]; + return value_field.is_comptime; +} + +test comptimeKnown { + // comptime_int literal + try testing.expect(comptimeKnown(1337)); + + // runtime integer literal + try testing.expect(comptimeKnown(@as(u16, 999))); + + // comptime_int constant + const integer_constant = 42; + try testing.expect(comptimeKnown(integer_constant)); + + // integer constant, initialized by comptime-known value + const constant_value: u32 = 0xDEADBEEF; + try testing.expect(comptimeKnown(constant_value)); + + // integer variable + var variable: u8 = 0; + while (variable < 10) : (variable += 1) { + try testing.expect(!comptimeKnown(variable)); + } + try testing.expect(!comptimeKnown(variable)); + + // integer constant, initialized by runtime value + const runtime_constant: u8 = variable; + try testing.expect(!comptimeKnown(runtime_constant)); + + // runtime integer comptime-variable + // should be comptime known + comptime var compile_variable: u8 = 0; + inline while (compile_variable < 10) : (compile_variable += 1) { + try testing.expect(comptimeKnown(compile_variable)); + } + try testing.expect(comptimeKnown(compile_variable)); +} From 4c4961d5fa3abfb10d92fda0130abe49a20d6226 Mon Sep 17 00:00:00 2001 From: seanthegleaming Date: Mon, 30 Sep 2024 12:13:19 -0400 Subject: [PATCH 2/6] Fixed a small inconsistency with a comment in the test cases --- lib/std/meta.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index cbb53ae83964..b4f78c5e56f2 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1357,7 +1357,7 @@ test comptimeKnown { const runtime_constant: u8 = variable; try testing.expect(!comptimeKnown(runtime_constant)); - // runtime integer comptime-variable + // integer comptime-variable // should be comptime known comptime var compile_variable: u8 = 0; inline while (compile_variable < 10) : (compile_variable += 1) { From 20edbd3ce3df61ef1ce482386318f0770125ea92 Mon Sep 17 00:00:00 2001 From: seanthegleaming Date: Mon, 30 Sep 2024 12:14:56 -0400 Subject: [PATCH 3/6] Fixed a small inconsistency with the comments in the test cases --- lib/std/meta.zig | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index b4f78c5e56f2..b17a58469528 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1358,7 +1358,6 @@ test comptimeKnown { try testing.expect(!comptimeKnown(runtime_constant)); // integer comptime-variable - // should be comptime known comptime var compile_variable: u8 = 0; inline while (compile_variable < 10) : (compile_variable += 1) { try testing.expect(comptimeKnown(compile_variable)); From 71421e7846b0f1c87c25ccb2b7f41da494f4fadd Mon Sep 17 00:00:00 2001 From: seanthegleaming Date: Mon, 30 Sep 2024 12:21:13 -0400 Subject: [PATCH 4/6] Fixed a typo in the doc comment for comptimeKnown --- lib/std/meta.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index b17a58469528..deed349a55ca 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1323,7 +1323,7 @@ test hasUniqueRepresentation { try testing.expect(@sizeOf(@Vector(3, u8)) == 3 or !hasUniqueRepresentation(@Vector(3, u8))); } -/// Returns true if the given value of is known at comptime +/// T if the given value is known at comptime pub inline fn comptimeKnown(value: anytype) bool { const TupleType = @TypeOf(.{value}); const tuple_fields = @typeInfo(TupleType).@"struct".fields; From 9e156dc905afbcf45e2ec2c18f171abe409cf9ba Mon Sep 17 00:00:00 2001 From: seanthegleaming Date: Mon, 30 Sep 2024 12:22:13 -0400 Subject: [PATCH 5/6] Fix typo in the previous push, finalizing doc comment for comptimeKnown --- lib/std/meta.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index deed349a55ca..bea176b10e9b 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1323,7 +1323,7 @@ test hasUniqueRepresentation { try testing.expect(@sizeOf(@Vector(3, u8)) == 3 or !hasUniqueRepresentation(@Vector(3, u8))); } -/// T if the given value is known at comptime +/// True if the given value is known at comptime pub inline fn comptimeKnown(value: anytype) bool { const TupleType = @TypeOf(.{value}); const tuple_fields = @typeInfo(TupleType).@"struct".fields; From fb0537cef2759c880597420e944992d2e1daca7a Mon Sep 17 00:00:00 2001 From: seanthegleaming Date: Mon, 30 Sep 2024 12:27:54 -0400 Subject: [PATCH 6/6] Fixed final inconsistency in comments of test cases --- lib/std/meta.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index bea176b10e9b..c1d68f0d499c 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1335,7 +1335,7 @@ test comptimeKnown { // comptime_int literal try testing.expect(comptimeKnown(1337)); - // runtime integer literal + // integer literal try testing.expect(comptimeKnown(@as(u16, 999))); // comptime_int constant