| author | |
| committer | |
| log | 07b936c95fb460c1b71b0b246ba1576167442721 |
| tree | 4031cddc48f5dfcf022b3f11676c134f93ec499c |
| parent | 7e82398cfe94f99ea82270894f125d8547d5f1ad |
| signature | Commit is signed but in an unrecognized format. |
2 files changed, 97 insertions(+), 0 deletions(-)
test/cases/compile_errors/runtime_operation_in_comptime_scope.zig created+36| ... | ... | @@ -0,0 +1,36 @@ |
| 1 | export fn entry1() void { | |
| 2 | foo(); | |
| 3 | } | |
| 4 | ||
| 5 | comptime { | |
| 6 | qux(); | |
| 7 | } | |
| 8 | ||
| 9 | inline fn foo() void { | |
| 10 | _ = bar(); | |
| 11 | } | |
| 12 | ||
| 13 | fn bar() type { | |
| 14 | qux(); | |
| 15 | return u8; | |
| 16 | } | |
| 17 | ||
| 18 | fn qux() void { | |
| 19 | rt = 123; | |
| 20 | } | |
| 21 | ||
| 22 | var 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 @@ |
| 1 | var rt_val: [5]u32 = .{ 1, 2, 3, 4, 5 }; | |
| 2 | ||
| 3 | comptime { | |
| 4 | _ = rt_val; // fine | |
| 5 | } | |
| 6 | ||
| 7 | comptime { | |
| 8 | const a = rt_val; // error | |
| 9 | _ = a; | |
| 10 | } | |
| 11 | ||
| 12 | comptime { | |
| 13 | const l = rt_val.len; // fine | |
| 14 | @compileLog(l); | |
| 15 | } | |
| 16 | ||
| 17 | export fn foo() void { | |
| 18 | _ = comptime rt_val; // error | |
| 19 | } | |
| 20 | ||
| 21 | export fn bar() void { | |
| 22 | const l = comptime rt_val.len; // fine | |
| 23 | @compileLog(l); | |
| 24 | } | |
| 25 | ||
| 26 | export 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 | ||
| 36 | export 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) |