From 2f07d76eee37442f53c294f53b38b11dfb1cd4da Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 2 Apr 2021 21:17:23 -0700 Subject: [PATCH] stage2: implement Type.onePossibleValue for structs --- src/type.zig | 10 ++++++++-- test/stage2/cbe.zig | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/type.zig b/src/type.zig index 2086567b8a..f5ce296e4d 100644 --- a/src/type.zig +++ b/src/type.zig @@ -3118,8 +3118,14 @@ pub const Type = extern union { => return null, .@"struct" => { - log.warn("TODO implement Type.onePossibleValue for structs", .{}); - return null; + const s = ty.castTag(.@"struct").?.data; + for (s.fields.entries.items) |entry| { + const field_ty = entry.value.ty; + if (field_ty.onePossibleValue() == null) { + return null; + } + } + return Value.initTag(.empty_struct_value); }, .empty_struct, .empty_struct_literal => return Value.initTag(.empty_struct_value), diff --git a/test/stage2/cbe.zig b/test/stage2/cbe.zig index 6efb5a5e7f..bfab645323 100644 --- a/test/stage2/cbe.zig +++ b/test/stage2/cbe.zig @@ -524,6 +524,16 @@ pub fn addCases(ctx: *TestContext) !void { ":6:10: error: no field named 'z' in struct 'Point'", ":1:15: note: 'Point' declared here", }); + case.addCompareOutput( + \\const Point = struct { x: i32, y: i32 }; + \\export fn main() c_int { + \\ var p: Point = .{ + \\ .x = 12, + \\ .y = 24, + \\ }; + \\ return p.y - p.x - p.x; + \\} + , ""); } ctx.c("empty start function", linux_x64,