authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-11 18:32:56-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-12 22:57:27-05:00
logfb7be4e074d1f23f708aa64cd49e8b0d9862e39a
treed7a2a61e853ba7197b4932d08f044412cefafb8d
parentf78f9388fe79f084d5ea028e6270a410eacfc316

behavior: referencing an extern means depending on it


3 files changed, 46 insertions(+), 0 deletions(-)

test/behavior/generics.zig+5
......@@ -381,6 +381,11 @@ test "extern function used as generic parameter" {
381381 };
382382 }
383383 };
384 const E = struct {
385 export fn usedAsGenericParameterFoo() void {}
386 export fn usedAsGenericParameterBar() void {}
387 };
388 _ = E;
384389 try expect(S.usedAsGenericParameterBaz(S.usedAsGenericParameterFoo) !=
385390 S.usedAsGenericParameterBaz(S.usedAsGenericParameterBar));
386391}
test/behavior/sizeof_and_typeof.zig+32
......@@ -359,6 +359,30 @@ extern fn c_fputs([*c]const u8, noalias [*c]FILE) c_int;
359359extern fn c_ftell([*c]FILE) c_long;
360360extern fn c_fopen([*c]const u8, [*c]const u8) [*c]FILE;
361361
362const exp = struct {
363 export fn c_printf(a: [*c]const u8) c_int {
364 _ = a;
365 unreachable;
366 }
367 export fn c_fputs(a: [*c]const u8, noalias b: [*c]FILE) c_int {
368 _ = a;
369 _ = b;
370 unreachable;
371 }
372 export fn c_ftell(a: [*c]FILE) c_long {
373 _ = a;
374 unreachable;
375 }
376 export fn c_fopen(a: [*c]const u8, b: [*c]const u8) [*c]FILE {
377 _ = a;
378 _ = b;
379 unreachable;
380 }
381};
382comptime {
383 _ = exp;
384}
385
362386test "Extern function calls in @TypeOf" {
363387 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
364388
......@@ -367,6 +391,14 @@ test "Extern function calls in @TypeOf" {
367391
368392 extern fn s_do_thing([*c]const @This(), b: c_int) c_short;
369393 };
394 const E = struct {
395 export fn s_do_thing(a: [*c]const @This(), b: c_int) c_short {
396 _ = a;
397 _ = b;
398 unreachable;
399 }
400 };
401 _ = E;
370402
371403 const Test = struct {
372404 fn test_fn_1(a: anytype, b: anytype) @TypeOf(c_printf("%d %s\n", a, b)) {
test/behavior/type_info.zig+9
......@@ -357,6 +357,15 @@ test "type info: function type info" {
357357}
358358
359359fn testFunction() !void {
360 const S = struct {
361 export fn typeInfoFoo() callconv(.c) usize {
362 unreachable;
363 }
364 export fn typeInfoFooAligned() callconv(.c) usize {
365 unreachable;
366 }
367 };
368 _ = S;
360369 const foo_fn_type = @TypeOf(typeInfoFoo);
361370 const foo_fn_info = @typeInfo(foo_fn_type);
362371 try expect(foo_fn_info.@"fn".calling_convention.eql(.c));