| author | |
| committer | |
| log | 997eaf6d8724fe2076195de4d6d54bf2bf880eea |
| tree | d423bc12df1141b2f8029f7a88e4f0f56413a316 |
| parent | f258a391daa31b3ba2c37d879db96fadc0c058f3 |
b3462b7 caused a regression in a third-party project, since it forced
resolution of field initializers for any field call 'foo.bar()', despite
this only being necessary when 'bar' is a comptime field.
See https://github.com/ziglang/zig/pull/17692#issuecomment-1802096734.2 files changed, 18 insertions(+), 2 deletions(-)
src/Sema.zig+3-2| ... | @@ -26652,8 +26652,9 @@ fn finishFieldCallBind( | ... | @@ -26652,8 +26652,9 @@ fn finishFieldCallBind( |
| 26652 | 26652 | ||
| 26653 | const container_ty = ptr_ty.childType(mod); | 26653 | const container_ty = ptr_ty.childType(mod); |
| 26654 | if (container_ty.zigTypeTag(mod) == .Struct) { | 26654 | if (container_ty.zigTypeTag(mod) == .Struct) { |
| 26655 | try sema.resolveStructFieldInits(container_ty); | 26655 | if (container_ty.structFieldIsComptime(field_index, mod)) { |
| 26656 | if (try container_ty.structFieldValueComptime(mod, field_index)) |default_val| { | 26656 | try sema.resolveStructFieldInits(container_ty); |
| 26657 | const default_val = (try container_ty.structFieldValueComptime(mod, field_index)).?; | ||
| 26657 | return .{ .direct = Air.internedToRef(default_val.toIntern()) }; | 26658 | return .{ .direct = Air.internedToRef(default_val.toIntern()) }; |
| 26658 | } | 26659 | } |
| 26659 | } | 26660 | } |
test/behavior/struct.zig+15| ... | @@ -1842,3 +1842,18 @@ test "circular dependency through pointer field of a struct" { | ... | @@ -1842,3 +1842,18 @@ test "circular dependency through pointer field of a struct" { |
| 1842 | try expect(outer.middle.outer == null); | 1842 | try expect(outer.middle.outer == null); |
| 1843 | try expect(outer.middle.inner == null); | 1843 | try expect(outer.middle.inner == null); |
| 1844 | } | 1844 | } |
| 1845 | |||
| 1846 | test "field calls do not force struct field init resolution" { | ||
| 1847 | const S = struct { | ||
| 1848 | x: u32 = blk: { | ||
| 1849 | _ = @TypeOf(make().dummyFn()); // runtime field call - S not fully resolved - dummyFn call should not force field init resolution | ||
| 1850 | break :blk 123; | ||
| 1851 | }, | ||
| 1852 | dummyFn: *const fn () void = undefined, | ||
| 1853 | fn make() @This() { | ||
| 1854 | return .{}; | ||
| 1855 | } | ||
| 1856 | }; | ||
| 1857 | var s: S = .{}; | ||
| 1858 | try expect(s.x == 123); | ||
| 1859 | } |