Skip to content

Commit 997eaf6

Browse files
mluggandrewrk
authored andcommitted
Sema: do not force resolution of struct field inits when calling function pointer field
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 #17692 (comment).
1 parent f258a39 commit 997eaf6

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Sema.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26652,8 +26652,9 @@ fn finishFieldCallBind(
2665226652

2665326653
const container_ty = ptr_ty.childType(mod);
2665426654
if (container_ty.zigTypeTag(mod) == .Struct) {
26655-
try sema.resolveStructFieldInits(container_ty);
26656-
if (try container_ty.structFieldValueComptime(mod, field_index)) |default_val| {
26655+
if (container_ty.structFieldIsComptime(field_index, mod)) {
26656+
try sema.resolveStructFieldInits(container_ty);
26657+
const default_val = (try container_ty.structFieldValueComptime(mod, field_index)).?;
2665726658
return .{ .direct = Air.internedToRef(default_val.toIntern()) };
2665826659
}
2665926660
}

test/behavior/struct.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,3 +1842,18 @@ test "circular dependency through pointer field of a struct" {
18421842
try expect(outer.middle.outer == null);
18431843
try expect(outer.middle.inner == null);
18441844
}
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+
}

0 commit comments

Comments
 (0)