aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-05-14 20:37:22 -0700
committerAndrew Kelley <andrew@ziglang.org>2023-06-10 20:47:53 -0700
commit6a9a918fbe4adc23dd7d7573c6f1e499f4be074e (patch)
treef94c3d80a82b04a905d725689740af1d8c90dc97 /src/Sema.zig
parentd18881de1be811c1dff52590223b92c916c4b773 (diff)
downloadzig-6a9a918fbe4adc23dd7d7573c6f1e499f4be074e.tar.gz
zig-6a9a918fbe4adc23dd7d7573c6f1e499f4be074e.zip
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`.
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig27
1 files changed, 9 insertions, 18 deletions
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| {