authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-26 18:06:19-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-26 18:06:19-05:00
logb0cd24f90ef159cc60c9607e9cc37af8b5bd147a
tree34447045027e830bde4aca5fa43ecfd5342997f4
parent728cc73819a6511e4402498a8146854b9278285f
parent64865679cf173c024a01d158686fc1cc9965a012
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14070 from jacobly0/issue/14032

Fix #14032

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

src/Sema.zig+8-2
......@@ -29708,6 +29708,12 @@ fn resolveLazyValue(sema: *Sema, val: Value) CompileError!void {
2970829708 const field_ptr = val.castTag(.comptime_field_ptr).?.data;
2970929709 return sema.resolveLazyValue(field_ptr.field_val);
2971029710 },
29711 .eu_payload,
29712 .opt_payload,
29713 => {
29714 const sub_val = val.cast(Value.Payload.SubValue).?.data;
29715 return sema.resolveLazyValue(sub_val);
29716 },
2971129717 .@"union" => {
2971229718 const union_val = val.castTag(.@"union").?.data;
2971329719 return sema.resolveLazyValue(union_val.val);
......@@ -29747,7 +29753,7 @@ pub fn resolveTypeLayout(sema: *Sema, ty: Type) CompileError!void {
2974729753 .Fn => {
2974829754 const info = ty.fnInfo();
2974929755 if (info.is_generic) {
29750 // Resolving of generic function types is defeerred to when
29756 // Resolving of generic function types is deferred to when
2975129757 // the function is instantiated.
2975229758 return;
2975329759 }
......@@ -30201,7 +30207,7 @@ pub fn resolveTypeFully(sema: *Sema, ty: Type) CompileError!void {
3020130207 .Fn => {
3020230208 const info = ty.fnInfo();
3020330209 if (info.is_generic) {
30204 // Resolving of generic function types is defeerred to when
30210 // Resolving of generic function types is deferred to when
3020530211 // the function is instantiated.
3020630212 return;
3020730213 }
test/behavior/struct.zig+25-6
......@@ -1420,15 +1420,34 @@ test "struct field has a pointer to an aligned version of itself" {
14201420 try expect(&e == e.next);
14211421}
14221422
1423test "struct only referenced from optional parameter/return" {
1423test "struct has only one reference" {
14241424 const S = struct {
1425 fn f(_: ?struct { x: u8 }) void {}
1426 fn g() ?struct { x: u8 } {
1425 fn optionalStructParam(_: ?struct { x: u8 }) void {}
1426 fn errorUnionStructParam(_: error{}!struct { x: u8 }) void {}
1427 fn optionalStructReturn() ?struct { x: u8 } {
14271428 return null;
14281429 }
1430 fn errorUnionStructReturn() error{Foo}!struct { x: u8 } {
1431 return error.Foo;
1432 }
1433 fn optionalComptimeIntParam(comptime x: ?comptime_int) comptime_int {
1434 return x.?;
1435 }
1436 fn errorUnionComptimeIntParam(comptime x: error{}!comptime_int) comptime_int {
1437 return x catch unreachable;
1438 }
14291439 };
14301440
1431 const fp: *const anyopaque = &S.f;
1432 const gp: *const anyopaque = &S.g;
1433 try expect(fp != gp);
1441 const optional_struct_param: *const anyopaque = &S.optionalStructParam;
1442 const error_union_struct_param: *const anyopaque = &S.errorUnionStructParam;
1443 try expect(optional_struct_param != error_union_struct_param);
1444
1445 const optional_struct_return: *const anyopaque = &S.optionalStructReturn;
1446 const error_union_struct_return: *const anyopaque = &S.errorUnionStructReturn;
1447 try expect(optional_struct_return != error_union_struct_return);
1448
1449 try expectEqual(@alignOf(struct {}), S.optionalComptimeIntParam(@alignOf(struct {})));
1450 try expectEqual(@alignOf(struct { x: u8 }), S.errorUnionComptimeIntParam(@alignOf(struct { x: u8 })));
1451 try expectEqual(@sizeOf(struct { x: u16 }), S.optionalComptimeIntParam(@sizeOf(struct { x: u16 })));
1452 try expectEqual(@sizeOf(struct { x: u32 }), S.errorUnionComptimeIntParam(@sizeOf(struct { x: u32 })));
14341453}