From 4fad84112a72c42227afdea432da22861fa088dd Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk <65823030+wrongnull@users.noreply.github.com> Date: Sun, 24 Sep 2023 21:23:55 +0300 Subject: [PATCH 1/4] std.meta: make globalOption comptime-only and update its docs Since https://github.com/ziglang/zig/issues/425 is closed, changes in this pr are now valid --- lib/std/meta.zig | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index c8ef19c017b3..18f4da58a973 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1091,11 +1091,14 @@ test "ArgsTuple forwarding" { } } -/// TODO: https://github.com/ziglang/zig/issues/425 +/// Returns the optional value of the declaration `name` in the root source file, casts it to type `T`. +/// This function should only be called at compile time. pub fn globalOption(comptime name: []const u8, comptime T: type) ?T { - if (!@hasDecl(root, name)) - return null; - return @as(T, @field(root, name)); + coptime { + if (!@hasDecl(root, name)) + return null; + return @as(T, @field(root, name)); + } } /// Returns whether `error_union` contains an error. From 209634fa685463053c84fc9912ea18d6fda88852 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk <65823030+wrongnull@users.noreply.github.com> Date: Sun, 24 Sep 2023 21:27:03 +0300 Subject: [PATCH 2/4] fix typo --- 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 18f4da58a973..5ba16feebaf4 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1094,7 +1094,7 @@ test "ArgsTuple forwarding" { /// Returns the optional value of the declaration `name` in the root source file, casts it to type `T`. /// This function should only be called at compile time. pub fn globalOption(comptime name: []const u8, comptime T: type) ?T { - coptime { + comptime { if (!@hasDecl(root, name)) return null; return @as(T, @field(root, name)); From 850f82cf29759b900e3678dc3cc62fc2f36dfc89 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk <65823030+wrongnull@users.noreply.github.com> Date: Sun, 24 Sep 2023 21:43:50 +0300 Subject: [PATCH 3/4] rewrite to block structure --- lib/std/meta.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 5ba16feebaf4..ddc08f5f49fe 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1094,10 +1094,10 @@ test "ArgsTuple forwarding" { /// Returns the optional value of the declaration `name` in the root source file, casts it to type `T`. /// This function should only be called at compile time. pub fn globalOption(comptime name: []const u8, comptime T: type) ?T { - comptime { + return comptime blk: { if (!@hasDecl(root, name)) - return null; - return @as(T, @field(root, name)); + break :blk null; + break :blk @as(T, @field(root, name)); } } From 82ff96eb740a3ea92250a8000fea399131030be8 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk <65823030+wrongnull@users.noreply.github.com> Date: Sun, 24 Sep 2023 21:52:17 +0300 Subject: [PATCH 4/4] missing semicolon --- 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 ddc08f5f49fe..848c088d6a5e 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1098,7 +1098,7 @@ pub fn globalOption(comptime name: []const u8, comptime T: type) ?T { if (!@hasDecl(root, name)) break :blk null; break :blk @as(T, @field(root, name)); - } + }; } /// Returns whether `error_union` contains an error.