authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-18 17:57:54+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-18 21:42:47+01:00
log2111f4c38b4c91a2406da3a5cf578162c1cafc4d
tree090c00ff870f9a83d69876c66cff5bcb8524f031
parentfeaee2ba170766cc905a6aa9c799f3105cdc8145

Sema: mark export on owner nav when exporting function alias

Resolves: #20847

2 files changed, 24 insertions(+), 11 deletions(-)

src/Sema.zig+11-11
...@@ -6457,15 +6457,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -6457,15 +6457,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
6457 if (ptr_info.byte_offset != 0) {6457 if (ptr_info.byte_offset != 0) {
6458 return sema.fail(block, ptr_src, "TODO: export pointer in middle of value", .{});6458 return sema.fail(block, ptr_src, "TODO: export pointer in middle of value", .{});
6459 }6459 }
6460 try sema.ensureNavResolved(src, nav);6460 try sema.analyzeExport(block, src, options, nav);
6461 // Make sure to export the owner Nav if applicable.
6462 const exported_nav = switch (ip.indexToKey(ip.getNav(nav).status.resolved.val)) {
6463 .variable => |v| v.owner_nav,
6464 .@"extern" => |e| e.owner_nav,
6465 .func => |f| f.owner_nav,
6466 else => nav,
6467 };
6468 try sema.analyzeExport(block, src, options, exported_nav);
6469 },6461 },
6470 }6462 }
6471}6463}
...@@ -6475,7 +6467,7 @@ pub fn analyzeExport(...@@ -6475,7 +6467,7 @@ pub fn analyzeExport(
6475 block: *Block,6467 block: *Block,
6476 src: LazySrcLoc,6468 src: LazySrcLoc,
6477 options: Zcu.Export.Options,6469 options: Zcu.Export.Options,
6478 exported_nav_index: InternPool.Nav.Index,6470 orig_nav_index: InternPool.Nav.Index,
6479) !void {6471) !void {
6480 const gpa = sema.gpa;6472 const gpa = sema.gpa;
6481 const pt = sema.pt;6473 const pt = sema.pt;
...@@ -6485,7 +6477,15 @@ pub fn analyzeExport(...@@ -6485,7 +6477,15 @@ pub fn analyzeExport(
6485 if (options.linkage == .internal)6477 if (options.linkage == .internal)
6486 return;6478 return;
64876479
6488 try sema.ensureNavResolved(src, exported_nav_index);6480 try sema.ensureNavResolved(src, orig_nav_index);
6481
6482 const exported_nav_index = switch (ip.indexToKey(ip.getNav(orig_nav_index).status.resolved.val)) {
6483 .variable => |v| v.owner_nav,
6484 .@"extern" => |e| e.owner_nav,
6485 .func => |f| f.owner_nav,
6486 else => orig_nav_index,
6487 };
6488
6489 const exported_nav = ip.getNav(exported_nav_index);6489 const exported_nav = ip.getNav(exported_nav_index);
6490 const export_ty = Type.fromInterned(exported_nav.typeOf(ip));6490 const export_ty = Type.fromInterned(exported_nav.typeOf(ip));
64916491
test/behavior/export_keyword.zig+13
...@@ -39,3 +39,16 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion) void {...@@ -39,3 +39,16 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion) void {
39 b;39 b;
40 }40 }
41}41}
42
43test "export function alias" {
44 _ = struct {
45 fn foo_internal() callconv(.C) u32 {
46 return 123;
47 }
48 export const foo_exported = foo_internal;
49 };
50 const Import = struct {
51 extern fn foo_exported() u32;
52 };
53 try expect(Import.foo_exported() == 123);
54}