From 6a9a918fbe4adc23dd7d7573c6f1e499f4be074e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 14 May 2023 20:37:22 -0700 Subject: stage2: encode one-possible-value tuple specially Anonymous structs and anonymous tuples can be stored via a only_possible_value tag because their type encodings, by definition, will have every value specified, which can be used to populate the fields slice in `Key.Aggregate`. Also fix `isTupleOrAnonStruct`. --- src/Sema.zig | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index 31e07bdcdc..74b3cdd114 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -18237,6 +18237,7 @@ fn zirStructInitAnon( return sema.failWithOwnedErrorMsg(msg); } if (try sema.resolveMaybeUndefVal(init)) |init_val| { + assert(init_val.ip_index != .none); values[i] = init_val.ip_index; } else { values[i] = .none; @@ -33181,8 +33182,8 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { // TODO: this is incorrect for structs with comptime fields, I think // we should use a temporary allocator to construct an aggregate that // is populated with the comptime values and then intern that value here. - // This TODO is repeated for anon_struct_type below, as well as - // in the redundant implementation of one-possible-value in type.zig. + // This TODO is repeated in the redundant implementation of + // one-possible-value in type.zig. const empty = try mod.intern(.{ .aggregate = .{ .ty = ty.ip_index, .fields = &.{}, @@ -33191,25 +33192,15 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { }, .anon_struct_type => |tuple| { - for (tuple.types, tuple.values) |field_ty, val| { - const is_comptime = val != .none; - if (is_comptime) continue; - if ((try sema.typeHasOnePossibleValue(field_ty.toType())) != null) continue; - return null; + for (tuple.values) |val| { + if (val == .none) return null; } - // In this case the struct has no runtime-known fields and + // In this case the struct has all comptime-known fields and // therefore has one possible value. - - // TODO: this is incorrect for structs with comptime fields, I think - // we should use a temporary allocator to construct an aggregate that - // is populated with the comptime values and then intern that value here. - // This TODO is repeated for struct_type above, as well as - // in the redundant implementation of one-possible-value in type.zig. - const empty = try mod.intern(.{ .aggregate = .{ + return (try mod.intern(.{ .aggregate = .{ .ty = ty.ip_index, - .fields = &.{}, - } }); - return empty.toValue(); + .fields = tuple.values, + } })).toValue(); }, .union_type => |union_type| { -- cgit v1.2.3