2020-04-27 20:22:15 +08:00
|
|
|
const expectEqual = @import("std").testing.expectEqual;
|
|
|
|
|
2020-04-27 23:07:18 +08:00
|
|
|
const FILE = extern struct {
|
|
|
|
dummy_field: u8,
|
|
|
|
};
|
2020-05-19 00:28:26 +08:00
|
|
|
|
2020-04-27 20:22:15 +08:00
|
|
|
extern fn printf([*c]const u8, ...) c_int;
|
|
|
|
extern fn fputs([*c]const u8, noalias [*c]FILE) c_int;
|
|
|
|
extern fn ftell([*c]FILE) c_long;
|
2020-05-19 00:28:26 +08:00
|
|
|
extern fn fopen([*c]const u8, [*c]const u8) [*c]FILE;
|
2020-04-27 20:22:15 +08:00
|
|
|
|
2020-04-27 23:07:18 +08:00
|
|
|
const S = extern struct {
|
|
|
|
state: c_short,
|
|
|
|
|
|
|
|
extern fn s_do_thing([*c]S, b: c_int) c_short;
|
|
|
|
};
|
|
|
|
|
|
|
|
test "Extern function calls in @TypeOf" {
|
2020-04-27 20:22:15 +08:00
|
|
|
const Test = struct {
|
2020-07-11 19:09:04 +08:00
|
|
|
fn test_fn_1(a: anytype, b: anytype) @TypeOf(printf("%d %s\n", a, b)) {
|
2020-04-27 20:22:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:09:04 +08:00
|
|
|
fn test_fn_2(a: anytype) @TypeOf((S{ .state = 0 }).s_do_thing(a)) {
|
2020-04-27 23:07:18 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:23:22 +08:00
|
|
|
fn doTheTest() !void {
|
|
|
|
try expectEqual(c_int, @TypeOf(test_fn_1(0, 42)));
|
|
|
|
try expectEqual(c_short, @TypeOf(test_fn_2(0)));
|
2020-04-27 20:22:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-05 02:23:22 +08:00
|
|
|
try Test.doTheTest();
|
|
|
|
comptime try Test.doTheTest();
|
2020-04-27 20:22:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
test "Peer resolution of extern function calls in @TypeOf" {
|
|
|
|
const Test = struct {
|
|
|
|
fn test_fn() @TypeOf(ftell(null), fputs(null, null)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:23:22 +08:00
|
|
|
fn doTheTest() !void {
|
|
|
|
try expectEqual(c_long, @TypeOf(test_fn()));
|
2020-04-27 20:22:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-05 02:23:22 +08:00
|
|
|
try Test.doTheTest();
|
|
|
|
comptime try Test.doTheTest();
|
2020-04-27 20:22:15 +08:00
|
|
|
}
|
2020-05-19 00:28:26 +08:00
|
|
|
|
|
|
|
test "Extern function calls, dereferences and field access in @TypeOf" {
|
|
|
|
const Test = struct {
|
|
|
|
fn test_fn_1(a: c_long) @TypeOf(fopen("test", "r").*) {
|
2021-06-20 09:10:22 +08:00
|
|
|
_ = a;
|
2020-05-19 00:28:26 +08:00
|
|
|
return .{ .dummy_field = 0 };
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:09:04 +08:00
|
|
|
fn test_fn_2(a: anytype) @TypeOf(fopen("test", "r").*.dummy_field) {
|
2021-06-20 09:10:22 +08:00
|
|
|
_ = a;
|
2020-05-19 00:28:26 +08:00
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:23:22 +08:00
|
|
|
fn doTheTest() !void {
|
|
|
|
try expectEqual(FILE, @TypeOf(test_fn_1(0)));
|
|
|
|
try expectEqual(u8, @TypeOf(test_fn_2(0)));
|
2020-05-19 00:28:26 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-05 02:23:22 +08:00
|
|
|
try Test.doTheTest();
|
|
|
|
comptime try Test.doTheTest();
|
2020-07-11 19:09:04 +08:00
|
|
|
}
|