diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-10-27 19:31:45 +0300 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-10-28 13:31:16 +0300 |
| commit | c3b85e4e2f3da02d78ccb814e3025ee4b78d541b (patch) | |
| tree | 7bf5f7c960d57daa2bfae0011e888b6ed1bb23aa /test/cases/compile_errors | |
| parent | c389f8800b8707fc163efab330d2d1838f553620 (diff) | |
| download | zig-c3b85e4e2f3da02d78ccb814e3025ee4b78d541b.tar.gz zig-c3b85e4e2f3da02d78ccb814e3025ee4b78d541b.zip | |
Sema: further enhance explanation of why expr is evaluated at comptime
Diffstat (limited to 'test/cases/compile_errors')
4 files changed, 65 insertions, 2 deletions
diff --git a/test/cases/compile_errors/condition_comptime_reason_explained.zig b/test/cases/compile_errors/condition_comptime_reason_explained.zig new file mode 100644 index 0000000000..332ae8afc8 --- /dev/null +++ b/test/cases/compile_errors/condition_comptime_reason_explained.zig @@ -0,0 +1,48 @@ +const S = struct { + fnPtr: fn () void, +}; +fn bar() void {} +fn baz() void {} +var runtime: bool = true; +fn ifExpr() S { + if (runtime) { + return .{ + .fnPtr = bar, + }; + } else { + return .{ + .fnPtr = baz, + }; + } +} +pub export fn entry1() void { + _ = ifExpr(); +} +fn switchExpr() S { + switch (runtime) { + true => return .{ + .fnPtr = bar, + }, + false => return .{ + .fnPtr = baz, + }, + } +} +pub export fn entry2() void { + _ = switchExpr(); +} + +// error +// backend=stage2 +// target=native +// +// :8:9: error: unable to resolve comptime value +// :8:9: note: condition in comptime branch must be comptime-known +// :7:13: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S' +// :2:12: note: struct requires comptime because of this field +// :2:12: note: use '*const fn() void' for a function pointer type +// :19:15: note: called from here +// :22:13: error: unable to resolve comptime value +// :22:13: note: condition in comptime switch must be comptime-known +// :21:17: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S' +// :32:19: note: called from here diff --git a/test/cases/compile_errors/explain_why_fn_is_called_at_comptime.zig b/test/cases/compile_errors/explain_why_fn_is_called_at_comptime.zig index d19ab31617..83d5d7e33f 100644 --- a/test/cases/compile_errors/explain_why_fn_is_called_at_comptime.zig +++ b/test/cases/compile_errors/explain_why_fn_is_called_at_comptime.zig @@ -18,6 +18,6 @@ pub export fn entry() void { // // :12:13: error: unable to resolve comptime value // :12:13: note: argument to function being called at comptime must be comptime-known -// :7:25: note: function is being called at comptime because it returns a comptime-only type 'tmp.S' +// :7:25: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S' // :2:12: note: struct requires comptime because of this field // :2:12: note: use '*const fn() void' for a function pointer type diff --git a/test/cases/compile_errors/explain_why_generic_fn_is_called_at_comptime.zig b/test/cases/compile_errors/explain_why_generic_fn_is_called_at_comptime.zig index 36aeb40479..701241a403 100644 --- a/test/cases/compile_errors/explain_why_generic_fn_is_called_at_comptime.zig +++ b/test/cases/compile_errors/explain_why_generic_fn_is_called_at_comptime.zig @@ -19,4 +19,4 @@ pub export fn entry() void { // // :14:13: error: unable to resolve comptime value // :14:13: note: argument to function being called at comptime must be comptime-known -// :9:38: note: generic function is instantiated with a comptime-only return type +// :9:38: note: expression is evaluated at comptime because the generic function was instantiated with a comptime-only return type diff --git a/test/cases/compile_errors/unable_to_evaluate_expr_inside_cimport.zig b/test/cases/compile_errors/unable_to_evaluate_expr_inside_cimport.zig new file mode 100644 index 0000000000..9460a58993 --- /dev/null +++ b/test/cases/compile_errors/unable_to_evaluate_expr_inside_cimport.zig @@ -0,0 +1,15 @@ +const c = @cImport({ + _ = 1 + foo; +}); +extern var foo: i32; +export fn entry() void { + _ = c; +} + +// error +// backend=llvm +// target=native +// +// :2:11: error: unable to evaluate comptime expression +// :2:13: note: operation is runtime due to this operand +// :1:11: note: expression is evaluated at comptime because it is inside a @cImport |
