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); +}