Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5605,7 +5605,15 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
};
const decl_index = switch (operand.val.tag()) {
.function => operand.val.castTag(.function).?.data.owner_decl,
else => return sema.fail(block, operand_src, "TODO implement exporting arbitrary Value objects", .{}), // TODO put this Value into an anonymous Decl and then export it.
else => blk: {
var anon_decl = try block.startAnonDecl();
defer anon_decl.deinit();
break :blk try anon_decl.finish(
try operand.ty.copy(anon_decl.arena()),
try operand.val.copy(anon_decl.arena()),
0,
);
},
};
try sema.analyzeExport(block, src, options, decl_index);
}
Expand Down Expand Up @@ -5641,6 +5649,14 @@ pub fn analyzeExport(
return sema.failWithOwnedErrorMsg(msg);
}

// TODO: some backends might support re-exporting extern decls
if (exported_decl.isExtern()) {
return sema.fail(block, src, "export target cannot be extern", .{});
}

// This decl is alive no matter what, since it's being exported
mod.markDeclAlive(exported_decl);

const gpa = mod.gpa;

try mod.decl_exports.ensureUnusedCapacity(gpa, 1);
Expand Down
19 changes: 19 additions & 0 deletions test/behavior/export.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,22 @@ test "exporting using field access" {

_ = S.Inner.x;
}

test "exporting comptime-known value" {
const x: u32 = 10;
@export(x, .{ .name = "exporting_comptime_known_value_foo" });
const S = struct {
extern const exporting_comptime_known_value_foo: u32;
};
try expect(S.exporting_comptime_known_value_foo == 10);
}

test "exporting comptime var" {
comptime var x: u32 = 5;
@export(x, .{ .name = "exporting_comptime_var_foo" });
x = 7; // modifying this now shouldn't change anything
const S = struct {
extern const exporting_comptime_var_foo: u32;
};
try expect(S.exporting_comptime_var_foo == 5);
}