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
This commit is contained in:
Luuk de Gram 2022-10-12 10:38:07 +02:00 committed by GitHub
parent 62258555b6
commit 1f196b9e2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -5180,10 +5180,13 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
const options_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
const decl_name = sema.code.nullTerminatedString(extra.decl_name);
if (extra.namespace != .none) {
return sema.fail(block, src, "TODO: implement exporting with field access", .{});
}
const decl_index = try sema.lookupIdentifier(block, operand_src, decl_name);
const decl_index = if (extra.namespace != .none) index_blk: {
const container_ty = try sema.resolveType(block, operand_src, extra.namespace);
const container_namespace = container_ty.getNamespace().?;
const maybe_index = try sema.lookupInNamespace(block, operand_src, container_namespace, decl_name, false);
break :index_blk maybe_index.?; // AstGen would produce error in case of unidentified name
} else try sema.lookupIdentifier(block, operand_src, decl_name);
const options = sema.resolveExportOptions(block, .unneeded, extra.options) catch |err| switch (err) {
error.NeededSourceLocation => {
_ = try sema.resolveExportOptions(block, options_src, extra.options);

View File

@ -55,3 +55,16 @@ test "exporting with internal linkage" {
};
S.foo();
}
test "exporting using field access" {
const S = struct {
const Inner = struct {
const x: u32 = 5;
};
comptime {
@export(Inner.x, .{ .name = "foo", .linkage = .Internal });
}
};
_ = S.Inner.x;
}