aboutsummaryrefslogtreecommitdiff
path: root/test/cases
diff options
context:
space:
mode:
authorantlilja <liljaanton2001@gmail.com>2022-08-06 22:32:00 +0200
committerVeikka Tuominen <git@vexu.eu>2022-08-27 11:17:48 +0300
commitae8d26a6a00a528bdf555689c2a93cb35a3287f2 (patch)
treed72b651523e62b81d0b1733cf2f1a4eced7265db /test/cases
parentda95da438ed5c934a9f42811651f5ffa524e48a8 (diff)
downloadzig-ae8d26a6a00a528bdf555689c2a93cb35a3287f2.tar.gz
zig-ae8d26a6a00a528bdf555689c2a93cb35a3287f2.zip
Sema: add error for non-comptime param in comptime func
Adds error for taking a non comptime parameter in a function returning a comptime-only type but not when that type is dependent on a parameter. Co-authored-by: Veikka Tuominen <git@vexu.eu>
Diffstat (limited to 'test/cases')
-rw-r--r--test/cases/compile_errors/explain_why_fn_is_called_at_comptime.zig8
-rw-r--r--test/cases/compile_errors/non_comptime_param_in_comptime_function.zig36
2 files changed, 40 insertions, 4 deletions
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 7ec539dcd1..04f64c2303 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
@@ -4,12 +4,12 @@ const S = struct {
};
fn bar() void {}
-fn foo(a: u8) S {
- return .{ .fnPtr = bar, .a = a };
+fn foo(comptime a: *u8) S {
+ return .{ .fnPtr = bar, .a = a.* };
}
pub export fn entry() void {
var a: u8 = 1;
- _ = foo(a);
+ _ = foo(&a);
}
// error
@@ -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:15: note: function is being called at comptime because it returns a comptime only type 'tmp.S'
+// :7:25: note: function is being called at comptime because it 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/non_comptime_param_in_comptime_function.zig b/test/cases/compile_errors/non_comptime_param_in_comptime_function.zig
new file mode 100644
index 0000000000..758166dd7f
--- /dev/null
+++ b/test/cases/compile_errors/non_comptime_param_in_comptime_function.zig
@@ -0,0 +1,36 @@
+fn F(val: anytype) type {
+ _ = val;
+ return struct {};
+}
+export fn entry() void {
+ _ = F(void{});
+}
+const S = struct {
+ foo: fn () void,
+};
+fn bar(_: u32) S {
+ return undefined;
+}
+export fn entry1() void {
+ _ = bar();
+}
+// prioritize other return type errors
+fn foo(a: u32) callconv(.C) comptime_int {
+ return a;
+}
+export fn entry2() void {
+ _ = foo(1);
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :1:20: error: function with comptime only return type 'type' requires all parameters to be comptime
+// :1:20: note: types are not available at runtime
+// :1:6: note: param 'val' is required to be comptime
+// :11:16: error: function with comptime only return type 'tmp.S' requires all parameters to be comptime
+// :9:10: note: struct requires comptime because of this field
+// :9:10: note: use '*const fn() void' for a function pointer type
+// :11:8: note: param is required to be comptime
+// :18:29: error: return type 'comptime_int' not allowed in function with calling convention 'C'