Commit 07b936c95f

mlugg <mlugg@mlugg.co.uk>
2024-12-29 23:58:11
cases: add cases for runtime code in comptime scopes
1 parent 7e82398
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
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)