diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2023-07-29 06:03:51 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2023-08-09 19:46:55 +0100 |
| commit | 6917a8c25824d12f00327171b583d6cd9a830c29 (patch) | |
| tree | 63b51fef7ea792e31c4cb14fdd732f632e62b03b /test/behavior/array.zig | |
| parent | 0461a64a93f0596e98b62d596bb547e5455577d2 (diff) | |
| download | zig-6917a8c25824d12f00327171b583d6cd9a830c29.tar.gz zig-6917a8c25824d12f00327171b583d6cd9a830c29.zip | |
AstGen: handle `ty` result location for struct and array init correctly
Well, this was a journey!
The original issue I was trying to fix is covered by the new behavior
test in array.zig: in essence, `ty` and `coerced_ty` result locations
were not correctly propagated.
While fixing this, I noticed a similar bug in struct inits: the type was
propagated to *fields* fine, but the actual struct init was
unnecessarily anonymous, which could lead to unnecessary copies. Note
that the behavior test added in struct.zig was already passing - the bug
here didn't change any easy-to-test behavior - but I figured I'd add it
anyway.
This is a little harder than it seems, because the result type may not
itself be an array/struct type: it could be an optional / error union
wrapper. A new ZIR instruction is introduced to unwrap these.
This is also made a little tricky by the fact that it's possible for
result types to be unknown at the time of semantic analysis (due to
`anytype` parameters), leading to generic poison. In these cases, we
must essentially downgrade to an anonymous initialization.
Fixing these issues exposed *another* bug, related to type resolution in
Sema. That issue is now tracked by #16603. As a temporary workaround for
this bug, a few result locations for builtin function operands have been
disabled in AstGen. This is technically a breaking change, but it's very
minor: I doubt it'll cause any breakage in the wild.
Diffstat (limited to 'test/behavior/array.zig')
| -rw-r--r-- | test/behavior/array.zig | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/test/behavior/array.zig b/test/behavior/array.zig index 5d475c9b25..a4cac26569 100644 --- a/test/behavior/array.zig +++ b/test/behavior/array.zig @@ -761,3 +761,17 @@ test "slicing array of zero-sized values" { for (arr[0..]) |zero| try expect(zero == 0); } + +test "array init with no result pointer sets field result types" { + const S = struct { + // A function parameter has a result type, but no result pointer. + fn f(arr: [1]u32) u32 { + return arr[0]; + } + }; + + const x: u64 = 123; + const y = S.f(.{@intCast(x)}); + + try expect(y == x); +} |
