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>
This commit is contained in:
antlilja
2022-08-06 22:32:00 +02:00
committed by Veikka Tuominen
parent da95da438e
commit ae8d26a6a0
10 changed files with 95 additions and 26 deletions

View File

@@ -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

View File

@@ -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'