LLVM: fix lowering of extern anyopaque

closes #18461
This commit is contained in:
Andrew Kelley 2024-01-10 17:56:38 -07:00
parent aba8d4f62c
commit 30688c341b
3 changed files with 21 additions and 1 deletions

View File

@ -3256,7 +3256,12 @@ pub const Object = struct {
128 => .fp128,
else => unreachable,
},
.anyopaque_type => unreachable,
.anyopaque_type => {
// This is unreachable except when used as the type for an extern global.
// For example: `@extern(*anyopaque, .{ .name = "foo"})` should produce
// @foo = external global i8
return .i8;
},
.bool_type => .i1,
.void_type => .void,
.type_type => unreachable,

View File

@ -32,6 +32,7 @@ test {
_ = @import("behavior/eval.zig");
_ = @import("behavior/export_builtin.zig");
_ = @import("behavior/export_self_referential_type_info.zig");
_ = @import("behavior/extern.zig");
_ = @import("behavior/field_parent_ptr.zig");
_ = @import("behavior/floatop.zig");
_ = @import("behavior/fn.zig");

14
test/behavior/extern.zig Normal file
View File

@ -0,0 +1,14 @@
const builtin = @import("builtin");
const std = @import("std");
const expect = std.testing.expect;
test "anyopaque extern symbol" {
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
const a = @extern(*anyopaque, .{ .name = "a_mystery_symbol" });
const b: *i32 = @alignCast(@ptrCast(a));
try expect(b.* == 1234);
}
export var a_mystery_symbol: i32 = 1234;