Skip to content

Commit

Permalink
Sema: mark export on owner nav when exporting function alias
Browse files Browse the repository at this point in the history
Resolves: #20847
  • Loading branch information
mlugg committed Sep 18, 2024
1 parent feaee2b commit 2111f4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6457,15 +6457,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
if (ptr_info.byte_offset != 0) {
return sema.fail(block, ptr_src, "TODO: export pointer in middle of value", .{});
}
try sema.ensureNavResolved(src, nav);
// Make sure to export the owner Nav if applicable.
const exported_nav = switch (ip.indexToKey(ip.getNav(nav).status.resolved.val)) {
.variable => |v| v.owner_nav,
.@"extern" => |e| e.owner_nav,
.func => |f| f.owner_nav,
else => nav,
};
try sema.analyzeExport(block, src, options, exported_nav);
try sema.analyzeExport(block, src, options, nav);
},
}
}
Expand All @@ -6475,7 +6467,7 @@ pub fn analyzeExport(
block: *Block,
src: LazySrcLoc,
options: Zcu.Export.Options,
exported_nav_index: InternPool.Nav.Index,
orig_nav_index: InternPool.Nav.Index,
) !void {
const gpa = sema.gpa;
const pt = sema.pt;
Expand All @@ -6485,7 +6477,15 @@ pub fn analyzeExport(
if (options.linkage == .internal)
return;

try sema.ensureNavResolved(src, exported_nav_index);
try sema.ensureNavResolved(src, orig_nav_index);

const exported_nav_index = switch (ip.indexToKey(ip.getNav(orig_nav_index).status.resolved.val)) {
.variable => |v| v.owner_nav,
.@"extern" => |e| e.owner_nav,
.func => |f| f.owner_nav,
else => orig_nav_index,
};

const exported_nav = ip.getNav(exported_nav_index);
const export_ty = Type.fromInterned(exported_nav.typeOf(ip));

Expand Down
13 changes: 13 additions & 0 deletions test/behavior/export_keyword.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion) void {
b;
}
}

test "export function alias" {
_ = struct {
fn foo_internal() callconv(.C) u32 {
return 123;
}
export const foo_exported = foo_internal;
};
const Import = struct {
extern fn foo_exported() u32;
};
try expect(Import.foo_exported() == 123);
}

0 comments on commit 2111f4c

Please sign in to comment.