From 07b936c95fb460c1b71b0b246ba1576167442721 Mon Sep 17 00:00:00 2001 From: mlugg Date: Sun, 29 Dec 2024 22:58:11 +0000 Subject: [PATCH] cases: add cases for runtime code in comptime scopes --- .../runtime_operation_in_comptime_scope.zig | 36 +++++++++++ .../runtime_value_in_comptime_scope.zig | 61 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 test/cases/compile_errors/runtime_operation_in_comptime_scope.zig create mode 100644 test/cases/compile_errors/runtime_value_in_comptime_scope.zig diff --git a/test/cases/compile_errors/runtime_operation_in_comptime_scope.zig b/test/cases/compile_errors/runtime_operation_in_comptime_scope.zig new file mode 100644 index 0000000000..8c1d31adf2 --- /dev/null +++ b/test/cases/compile_errors/runtime_operation_in_comptime_scope.zig @@ -0,0 +1,36 @@ +export fn entry1() void { + foo(); +} + +comptime { + qux(); +} + +inline fn foo() void { + _ = bar(); +} + +fn bar() type { + qux(); + return u8; +} + +fn qux() void { + rt = 123; +} + +var rt: u32 = undefined; + +// error +// +// :19:8: error: unable to evaluate comptime expression +// :19:5: note: operation is runtime due to this operand +// :14:8: note: called at comptime from here +// :10:12: note: called at comptime from here +// :13:10: note: function with comptime-only return type 'type' is evaluated at comptime +// :13:10: note: types are not available at runtime +// :2:8: note: called from here +// :19:8: error: unable to evaluate comptime expression +// :19:5: note: operation is runtime due to this operand +// :6:8: note: called at comptime from here +// :5:1: note: 'comptime' keyword forces comptime evaluation diff --git a/test/cases/compile_errors/runtime_value_in_comptime_scope.zig b/test/cases/compile_errors/runtime_value_in_comptime_scope.zig new file mode 100644 index 0000000000..37c6b46dba --- /dev/null +++ b/test/cases/compile_errors/runtime_value_in_comptime_scope.zig @@ -0,0 +1,61 @@ +var rt_val: [5]u32 = .{ 1, 2, 3, 4, 5 }; + +comptime { + _ = rt_val; // fine +} + +comptime { + const a = rt_val; // error + _ = a; +} + +comptime { + const l = rt_val.len; // fine + @compileLog(l); +} + +export fn foo() void { + _ = comptime rt_val; // error +} + +export fn bar() void { + const l = comptime rt_val.len; // fine + @compileLog(l); +} + +export fn baz() void { + const S = struct { + fn inner() void { + _ = comptime rt_val; + } + }; + comptime S.inner(); // fine; inner comptime is a nop + S.inner(); // error +} + +export fn qux() void { + const S = struct { + fn inner() void { + const a = rt_val; + _ = a; + } + }; + S.inner(); // fine; everything is runtime + comptime S.inner(); // error +} + +// error +// +// :8:15: error: unable to resolve comptime value +// :7:1: note: 'comptime' keyword forces comptime evaluation +// :18:9: error: unable to resolve comptime value +// :18:9: note: 'comptime' keyword forces comptime evaluation +// :29:17: error: unable to resolve comptime value +// :29:17: note: 'comptime' keyword forces comptime evaluation +// :39:23: error: unable to resolve comptime value +// :44:21: note: called at comptime from here +// :44:5: note: 'comptime' keyword forces comptime evaluation +// +// Compile Log Output: +// @as(usize, 5) +// @as(usize, 5)