aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/struct.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2023-07-29 06:03:51 +0100
committermlugg <mlugg@mlugg.co.uk>2023-08-09 19:46:55 +0100
commit6917a8c25824d12f00327171b583d6cd9a830c29 (patch)
tree63b51fef7ea792e31c4cb14fdd732f632e62b03b /test/behavior/struct.zig
parent0461a64a93f0596e98b62d596bb547e5455577d2 (diff)
downloadzig-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/struct.zig')
-rw-r--r--test/behavior/struct.zig14
1 files changed, 14 insertions, 0 deletions
diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig
index 6017d4e63c..1c08e7b5fa 100644
--- a/test/behavior/struct.zig
+++ b/test/behavior/struct.zig
@@ -1724,3 +1724,17 @@ test "packed struct field in anonymous struct" {
fn countFields(v: anytype) usize {
return @typeInfo(@TypeOf(v)).Struct.fields.len;
}
+
+test "struct 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(s: struct { x: u32 }) u32 {
+ return s.x;
+ }
+ };
+
+ const x: u64 = 123;
+ const y = S.f(.{ .x = @intCast(x) });
+
+ try expect(y == x);
+}