zig/test/standalone/extern/main.zig
mlugg c93e0d8618 Sema: @extern fixes
* There was an edge case where the arena could be destroyed twice on
  error: once from the arena itself and once from the decl destruction.

* The type of the created decl was incorrect (it should have been the
  pointer child type), but it's not required anyway, so it's now just
  initialized to anyopaque (which more accurately reflects what's
  actually at that memory, since e.g. [*]T may correspond to nothing).

* A runtime bitcast of the pointer was performed, meaning @extern didn't
  work at comptime. This is unnecessary: the decl_ref can just be
  initialized with the correct pointer type.
2023-03-12 18:55:23 +02:00

22 lines
619 B
Zig

const assert = @import("std").debug.assert;
const updateHidden = @extern(*const fn (u32) callconv(.C) void, .{ .name = "updateHidden" });
const getHidden = @extern(*const fn () callconv(.C) u32, .{ .name = "getHidden" });
const T = extern struct { x: u32 };
test {
var mut_val_ptr = @extern(*f64, .{ .name = "mut_val" });
var const_val_ptr = @extern(*const T, .{ .name = "const_val" });
assert(getHidden() == 0);
updateHidden(123);
assert(getHidden() == 123);
assert(mut_val_ptr.* == 1.23);
mut_val_ptr.* = 10.0;
assert(mut_val_ptr.* == 10.0);
assert(const_val_ptr.x == 42);
}