authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-10-12 10:38:07+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-10-12 04:38:07-04:00
log1f196b9e2f9213fa395427bd5a8282ab154b1fe6
tree32d35ba10b5cb2aeb5efc99d14eded8b8942ab34
parent62258555b6b69538086cfd3d16b748ffdb5c5dca
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

stage2: implement exporting using field access (#13136)

This implements `@export(a.b, .{..});` in semantic analysis, allowing users to directly export a variable from a namespace. * add test case for exporting using field access

2 files changed, 20 insertions(+), 4 deletions(-)

src/Sema.zig+7-4
...@@ -5180,10 +5180,13 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -5180,10 +5180,13 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
5180 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };5180 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
5181 const options_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };5181 const options_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
5182 const decl_name = sema.code.nullTerminatedString(extra.decl_name);5182 const decl_name = sema.code.nullTerminatedString(extra.decl_name);
5183 if (extra.namespace != .none) {5183 const decl_index = if (extra.namespace != .none) index_blk: {
5184 return sema.fail(block, src, "TODO: implement exporting with field access", .{});5184 const container_ty = try sema.resolveType(block, operand_src, extra.namespace);
5185 }5185 const container_namespace = container_ty.getNamespace().?;
5186 const decl_index = try sema.lookupIdentifier(block, operand_src, decl_name);5186
5187 const maybe_index = try sema.lookupInNamespace(block, operand_src, container_namespace, decl_name, false);
5188 break :index_blk maybe_index.?; // AstGen would produce error in case of unidentified name
5189 } else try sema.lookupIdentifier(block, operand_src, decl_name);
5187 const options = sema.resolveExportOptions(block, .unneeded, extra.options) catch |err| switch (err) {5190 const options = sema.resolveExportOptions(block, .unneeded, extra.options) catch |err| switch (err) {
5188 error.NeededSourceLocation => {5191 error.NeededSourceLocation => {
5189 _ = try sema.resolveExportOptions(block, options_src, extra.options);5192 _ = try sema.resolveExportOptions(block, options_src, extra.options);
test/behavior/export.zig+13
...@@ -55,3 +55,16 @@ test "exporting with internal linkage" {...@@ -55,3 +55,16 @@ test "exporting with internal linkage" {
55 };55 };
56 S.foo();56 S.foo();
57}57}
58
59test "exporting using field access" {
60 const S = struct {
61 const Inner = struct {
62 const x: u32 = 5;
63 };
64 comptime {
65 @export(Inner.x, .{ .name = "foo", .linkage = .Internal });
66 }
67 };
68
69 _ = S.Inner.x;
70}