From 28caaea0938e938de96cc66a49f670005a2df9d4 Mon Sep 17 00:00:00 2001 From: mlugg Date: Sat, 16 Sep 2023 22:35:42 +0100 Subject: [PATCH] AstGen: allow closure over known-runtime values within @TypeOf AstGen emits an error when a closure over a known-runtime value crosses a namespace boundary. This usually makes sense: however, this usage is actually valid if the capture is within a `@TypeOf` operand. Sema already has a special case to allow such closure within `@TypeOf` when AstGen could not determine a value to be runtime-known. This commit simply introduces analagous logic to AstGen to allow `var`s to cross namespace boundaries within `@TypeOf`. --- src/AstGen.zig | 7 ++++++- test/behavior/eval.zig | 10 ++++++++++ test/run_translated_c.zig | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index 61476b505a..a602be8538 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -7468,7 +7468,7 @@ fn localVarRef( } // Can't close over a runtime variable - if (num_namespaces_out != 0 and !local_ptr.maybe_comptime) { + if (num_namespaces_out != 0 and !local_ptr.maybe_comptime and !gz.is_typeof) { const ident_name = try astgen.identifierTokenString(ident_token); return astgen.failNodeNotes(ident, "mutable '{s}' not accessible from here", .{ident_name}, &.{ try astgen.errNoteTok(local_ptr.token_src, "declared mutable here", .{}), @@ -8041,6 +8041,7 @@ fn typeOf( var typeof_scope = gz.makeSubBlock(scope); typeof_scope.is_comptime = false; + typeof_scope.is_typeof = true; typeof_scope.c_import = false; defer typeof_scope.unstack(); @@ -10882,6 +10883,9 @@ const GenZir = struct { /// whenever we know Sema will analyze the current block with `is_comptime`, /// for instance when we're within a `struct_decl` or a `block_comptime`. is_comptime: bool, + /// Whether we're in an expression within a `@TypeOf` operand. In this case, closure of runtime + /// variables is permitted where it is usually not. + is_typeof: bool = false, /// This is set to true for inline loops; false otherwise. is_inline: bool = false, c_import: bool = false, @@ -10953,6 +10957,7 @@ const GenZir = struct { fn makeSubBlock(gz: *GenZir, scope: *Scope) GenZir { return .{ .is_comptime = gz.is_comptime, + .is_typeof = gz.is_typeof, .c_import = gz.c_import, .decl_node_index = gz.decl_node_index, .decl_line = gz.decl_line, diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index 35cfab9211..aae5c33635 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -991,6 +991,16 @@ test "closure capture type of runtime-known parameter" { try S.b(c); } +test "closure capture type of runtime-known var" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + + var x: u32 = 1234; + const S = struct { val: @TypeOf(x + 100) }; + const s: S = .{ .val = x }; + try expect(s.val == 1234); +} + test "comptime break passing through runtime condition converted to runtime break" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; diff --git a/test/run_translated_c.zig b/test/run_translated_c.zig index 0e3000a113..13b72c1738 100644 --- a/test/run_translated_c.zig +++ b/test/run_translated_c.zig @@ -1895,4 +1895,14 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { \\ return 0; \\} , ""); + + cases.add("Closure over local in typeof", + \\#include + \\int main(void) { + \\ int x = 123; + \\ union { typeof(x) val; } u = { x }; + \\ if (u.val != 123) abort(); + \\ return 0; + \\} + , ""); }