From 8dd844d96fdc1975365e4cc8f12f0d55d5e0cef4 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Mon, 26 Dec 2022 00:01:53 -0500 Subject: [PATCH 1/2] Sema: fix typo --- src/Sema.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 2b4ef94a8f..418f74b6f1 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -29747,7 +29747,7 @@ pub fn resolveTypeLayout(sema: *Sema, ty: Type) CompileError!void { .Fn => { const info = ty.fnInfo(); if (info.is_generic) { - // Resolving of generic function types is defeerred to when + // Resolving of generic function types is deferred to when // the function is instantiated. return; } @@ -30201,7 +30201,7 @@ pub fn resolveTypeFully(sema: *Sema, ty: Type) CompileError!void { .Fn => { const info = ty.fnInfo(); if (info.is_generic) { - // Resolving of generic function types is defeerred to when + // Resolving of generic function types is deferred to when // the function is instantiated. return; } From 64865679cf173c024a01d158686fc1cc9965a012 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Mon, 26 Dec 2022 04:16:32 -0500 Subject: [PATCH 2/2] Sema: add missing `resolveLazyValue` cases Closes #14032 --- src/Sema.zig | 6 ++++++ test/behavior/struct.zig | 31 +++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 418f74b6f1..de9018be6e 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -29708,6 +29708,12 @@ fn resolveLazyValue(sema: *Sema, val: Value) CompileError!void { const field_ptr = val.castTag(.comptime_field_ptr).?.data; return sema.resolveLazyValue(field_ptr.field_val); }, + .eu_payload, + .opt_payload, + => { + const sub_val = val.cast(Value.Payload.SubValue).?.data; + return sema.resolveLazyValue(sub_val); + }, .@"union" => { const union_val = val.castTag(.@"union").?.data; return sema.resolveLazyValue(union_val.val); diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index a23294a102..2a9ea945e0 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1420,15 +1420,34 @@ 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" { +test "struct has only one reference" { const S = struct { - fn f(_: ?struct { x: u8 }) void {} - fn g() ?struct { x: u8 } { + fn optionalStructParam(_: ?struct { x: u8 }) void {} + fn errorUnionStructParam(_: error{}!struct { x: u8 }) void {} + fn optionalStructReturn() ?struct { x: u8 } { return null; } + fn errorUnionStructReturn() error{Foo}!struct { x: u8 } { + return error.Foo; + } + fn optionalComptimeIntParam(comptime x: ?comptime_int) comptime_int { + return x.?; + } + fn errorUnionComptimeIntParam(comptime x: error{}!comptime_int) comptime_int { + return x catch unreachable; + } }; - const fp: *const anyopaque = &S.f; - const gp: *const anyopaque = &S.g; - try expect(fp != gp); + const optional_struct_param: *const anyopaque = &S.optionalStructParam; + const error_union_struct_param: *const anyopaque = &S.errorUnionStructParam; + try expect(optional_struct_param != error_union_struct_param); + + const optional_struct_return: *const anyopaque = &S.optionalStructReturn; + const error_union_struct_return: *const anyopaque = &S.errorUnionStructReturn; + try expect(optional_struct_return != error_union_struct_return); + + try expectEqual(@alignOf(struct {}), S.optionalComptimeIntParam(@alignOf(struct {}))); + try expectEqual(@alignOf(struct { x: u8 }), S.errorUnionComptimeIntParam(@alignOf(struct { x: u8 }))); + try expectEqual(@sizeOf(struct { x: u16 }), S.optionalComptimeIntParam(@sizeOf(struct { x: u16 }))); + try expectEqual(@sizeOf(struct { x: u32 }), S.errorUnionComptimeIntParam(@sizeOf(struct { x: u32 }))); }