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 {...@@ -29119,6 +29119,12 @@ fn resolveLazyValue(sema: *Sema, val: Value) CompileError!void {
29119 const field_ptr = val.castTag(.comptime_field_ptr).?.data;29119 const field_ptr = val.castTag(.comptime_field_ptr).?.data;
29120 return sema.resolveLazyValue(field_ptr.field_val);29120 return sema.resolveLazyValue(field_ptr.field_val);
29121 },29121 },
29122 .eu_payload,
29123 .opt_payload,
29124 => {
29125 const sub_val = val.cast(Value.Payload.SubValue).?.data;
29126 return sema.resolveLazyValue(sub_val);
29127 },
29122 .@"union" => {29128 .@"union" => {
29123 const union_val = val.castTag(.@"union").?.data;29129 const union_val = val.castTag(.@"union").?.data;
29124 return sema.resolveLazyValue(union_val.val);29130 return sema.resolveLazyValue(union_val.val);
...@@ -29158,7 +29164,7 @@ pub fn resolveTypeLayout(sema: *Sema, ty: Type) CompileError!void {...@@ -29158,7 +29164,7 @@ pub fn resolveTypeLayout(sema: *Sema, ty: Type) CompileError!void {
29158 .Fn => {29164 .Fn => {
29159 const info = ty.fnInfo();29165 const info = ty.fnInfo();
29160 if (info.is_generic) {29166 if (info.is_generic) {
29161 // Resolving of generic function types is defeerred to when29167 // Resolving of generic function types is deferred to when
29162 // the function is instantiated.29168 // the function is instantiated.
29163 return;29169 return;
29164 }29170 }
...@@ -29612,7 +29618,7 @@ pub fn resolveTypeFully(sema: *Sema, ty: Type) CompileError!void {...@@ -29612,7 +29618,7 @@ pub fn resolveTypeFully(sema: *Sema, ty: Type) CompileError!void {
29612 .Fn => {29618 .Fn => {
29613 const info = ty.fnInfo();29619 const info = ty.fnInfo();
29614 if (info.is_generic) {29620 if (info.is_generic) {
29615 // Resolving of generic function types is defeerred to when29621 // Resolving of generic function types is deferred to when
29616 // the function is instantiated.29622 // the function is instantiated.
29617 return;29623 return;
29618 }29624 }
test/behavior/struct.zig+25-6
...@@ -1431,15 +1431,34 @@ test "struct field has a pointer to an aligned version of itself" {...@@ -1431,15 +1431,34 @@ test "struct field has a pointer to an aligned version of itself" {
1431 try expect(&e == e.next);1431 try expect(&e == e.next);
1432}1432}
14331433
1434test "struct only referenced from optional parameter/return" {1434test "struct has only one reference" {
1435 const S = struct {1435 const S = struct {
1436 fn f(_: ?struct { x: u8 }) void {}1436 fn optionalStructParam(_: ?struct { x: u8 }) void {}
1437 fn g() ?struct { x: u8 } {1437 fn errorUnionStructParam(_: error{}!struct { x: u8 }) void {}
1438 fn optionalStructReturn() ?struct { x: u8 } {
1438 return null;1439 return null;
1439 }1440 }
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 }
1440 };1450 };
14411451
1442 const fp: *const anyopaque = &S.f;1452 const optional_struct_param: *const anyopaque = &S.optionalStructParam;
1443 const gp: *const anyopaque = &S.g;1453 const error_union_struct_param: *const anyopaque = &S.errorUnionStructParam;
1444 try expect(fp != gp);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 })));
1445}1464}