authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-26 18:06:19-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-09 15:11:19-07:00
log5766c8ebc8c1ebf32383bb338328154035f0f4d7
tree3acd69f43ca6771fc25531be434ab9fe78ddac5d
parent4068fafcc621a05f4149f5e838c938f78c9500ad

Merge pull request #14070 from jacobly0/issue/14032

Fix #14032

2 files changed, 33 insertions(+), 8 deletions(-)

src/Sema.zig+8-2
......@@ -29119,6 +29119,12 @@ fn resolveLazyValue(sema: *Sema, val: Value) CompileError!void {
2911929119 const field_ptr = val.castTag(.comptime_field_ptr).?.data;
2912029120 return sema.resolveLazyValue(field_ptr.field_val);
2912129121 },
29122 .eu_payload,
29123 .opt_payload,
29124 => {
29125 const sub_val = val.cast(Value.Payload.SubValue).?.data;
29126 return sema.resolveLazyValue(sub_val);
29127 },
2912229128 .@"union" => {
2912329129 const union_val = val.castTag(.@"union").?.data;
2912429130 return sema.resolveLazyValue(union_val.val);
......@@ -29158,7 +29164,7 @@ pub fn resolveTypeLayout(sema: *Sema, ty: Type) CompileError!void {
2915829164 .Fn => {
2915929165 const info = ty.fnInfo();
2916029166 if (info.is_generic) {
29161 // Resolving of generic function types is defeerred to when
29167 // Resolving of generic function types is deferred to when
2916229168 // the function is instantiated.
2916329169 return;
2916429170 }
......@@ -29612,7 +29618,7 @@ pub fn resolveTypeFully(sema: *Sema, ty: Type) CompileError!void {
2961229618 .Fn => {
2961329619 const info = ty.fnInfo();
2961429620 if (info.is_generic) {
29615 // Resolving of generic function types is defeerred to when
29621 // Resolving of generic function types is deferred to when
2961629622 // the function is instantiated.
2961729623 return;
2961829624 }
test/behavior/struct.zig+25-6
......@@ -1431,15 +1431,34 @@ test "struct field has a pointer to an aligned version of itself" {
14311431 try expect(&e == e.next);
14321432}
14331433
1434test "struct only referenced from optional parameter/return" {
1434test "struct has only one reference" {
14351435 const S = struct {
1436 fn f(_: ?struct { x: u8 }) void {}
1437 fn g() ?struct { x: u8 } {
1436 fn optionalStructParam(_: ?struct { x: u8 }) void {}
1437 fn errorUnionStructParam(_: error{}!struct { x: u8 }) void {}
1438 fn optionalStructReturn() ?struct { x: u8 } {
14381439 return null;
14391440 }
1441 fn errorUnionStructReturn() error{Foo}!struct { x: u8 } {
1442 return error.Foo;
1443 }
1444 fn optionalComptimeIntParam(comptime x: ?comptime_int) comptime_int {
1445 return x.?;
1446 }
1447 fn errorUnionComptimeIntParam(comptime x: error{}!comptime_int) comptime_int {
1448 return x catch unreachable;
1449 }
14401450 };
14411451
1442 const fp: *const anyopaque = &S.f;
1443 const gp: *const anyopaque = &S.g;
1444 try expect(fp != gp);
1452 const optional_struct_param: *const anyopaque = &S.optionalStructParam;
1453 const error_union_struct_param: *const anyopaque = &S.errorUnionStructParam;
1454 try expect(optional_struct_param != error_union_struct_param);
1455
1456 const optional_struct_return: *const anyopaque = &S.optionalStructReturn;
1457 const error_union_struct_return: *const anyopaque = &S.errorUnionStructReturn;
1458 try expect(optional_struct_return != error_union_struct_return);
1459
1460 try expectEqual(@alignOf(struct {}), S.optionalComptimeIntParam(@alignOf(struct {})));
1461 try expectEqual(@alignOf(struct { x: u8 }), S.errorUnionComptimeIntParam(@alignOf(struct { x: u8 })));
1462 try expectEqual(@sizeOf(struct { x: u16 }), S.optionalComptimeIntParam(@sizeOf(struct { x: u16 })));
1463 try expectEqual(@sizeOf(struct { x: u32 }), S.errorUnionComptimeIntParam(@sizeOf(struct { x: u32 })));
14451464}