authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-29 22:58:11+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-31 09:55:04+00:00
log07b936c95fb460c1b71b0b246ba1576167442721
tree4031cddc48f5dfcf022b3f11676c134f93ec499c
parent7e82398cfe94f99ea82270894f125d8547d5f1ad
signaturelock-open Commit is signed but in an unrecognized format.

cases: add cases for runtime code in comptime scopes


2 files changed, 97 insertions(+), 0 deletions(-)

test/cases/compile_errors/runtime_operation_in_comptime_scope.zig created+36
......@@ -0,0 +1,36 @@
1export fn entry1() void {
2 foo();
3}
4
5comptime {
6 qux();
7}
8
9inline fn foo() void {
10 _ = bar();
11}
12
13fn bar() type {
14 qux();
15 return u8;
16}
17
18fn qux() void {
19 rt = 123;
20}
21
22var rt: u32 = undefined;
23
24// error
25//
26// :19:8: error: unable to evaluate comptime expression
27// :19:5: note: operation is runtime due to this operand
28// :14:8: note: called at comptime from here
29// :10:12: note: called at comptime from here
30// :13:10: note: function with comptime-only return type 'type' is evaluated at comptime
31// :13:10: note: types are not available at runtime
32// :2:8: note: called from here
33// :19:8: error: unable to evaluate comptime expression
34// :19:5: note: operation is runtime due to this operand
35// :6:8: note: called at comptime from here
36// :5:1: note: 'comptime' keyword forces comptime evaluation
test/cases/compile_errors/runtime_value_in_comptime_scope.zig created+61
......@@ -0,0 +1,61 @@
1var rt_val: [5]u32 = .{ 1, 2, 3, 4, 5 };
2
3comptime {
4 _ = rt_val; // fine
5}
6
7comptime {
8 const a = rt_val; // error
9 _ = a;
10}
11
12comptime {
13 const l = rt_val.len; // fine
14 @compileLog(l);
15}
16
17export fn foo() void {
18 _ = comptime rt_val; // error
19}
20
21export fn bar() void {
22 const l = comptime rt_val.len; // fine
23 @compileLog(l);
24}
25
26export fn baz() void {
27 const S = struct {
28 fn inner() void {
29 _ = comptime rt_val;
30 }
31 };
32 comptime S.inner(); // fine; inner comptime is a nop
33 S.inner(); // error
34}
35
36export fn qux() void {
37 const S = struct {
38 fn inner() void {
39 const a = rt_val;
40 _ = a;
41 }
42 };
43 S.inner(); // fine; everything is runtime
44 comptime S.inner(); // error
45}
46
47// error
48//
49// :8:15: error: unable to resolve comptime value
50// :7:1: note: 'comptime' keyword forces comptime evaluation
51// :18:9: error: unable to resolve comptime value
52// :18:9: note: 'comptime' keyword forces comptime evaluation
53// :29:17: error: unable to resolve comptime value
54// :29:17: note: 'comptime' keyword forces comptime evaluation
55// :39:23: error: unable to resolve comptime value
56// :44:21: note: called at comptime from here
57// :44:5: note: 'comptime' keyword forces comptime evaluation
58//
59// Compile Log Output:
60// @as(usize, 5)
61// @as(usize, 5)