From 4068fafcc621a05f4149f5e838c938f78c9500ad Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sat, 24 Dec 2022 19:44:52 -0500 Subject: [PATCH] Sema: fix missing struct layout for llvm backend Closes #14063 --- src/Sema.zig | 12 ++++++++++++ test/behavior/struct.zig | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index fab94a06ff..bc13e9bd0f 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -29155,6 +29155,18 @@ pub fn resolveTypeLayout(sema: *Sema, ty: Type) CompileError!void { const payload_ty = ty.errorUnionPayload(); return sema.resolveTypeLayout(payload_ty); }, + .Fn => { + const info = ty.fnInfo(); + if (info.is_generic) { + // Resolving of generic function types is defeerred to when + // the function is instantiated. + return; + } + for (info.param_types) |param_ty| { + try sema.resolveTypeLayout(param_ty); + } + try sema.resolveTypeLayout(info.return_type); + }, else => {}, } } diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index cce9654254..2a15d68c67 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1430,3 +1430,16 @@ test "struct field has a pointer to an aligned version of itself" { try expect(&e == e.next); } + +test "struct only referenced from optional parameter/return" { + const S = struct { + fn f(_: ?struct { x: u8 }) void {} + fn g() ?struct { x: u8 } { + return null; + } + }; + + const fp: *const anyopaque = &S.f; + const gp: *const anyopaque = &S.g; + try expect(fp != gp); +}