Skip to content

Commit 2811a06

Browse files
committed
Supports lowering tuples without a result type
1 parent e487184 commit 2811a06

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

src/Sema/LowerZon.zig

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ pub fn run(
6464

6565
fn lowerExprAnonResTy(self: *LowerZon, node: Zoir.Node.Index) CompileError!InternPool.Index {
6666
const gpa = self.sema.gpa;
67-
const ip = &self.sema.pt.zcu.intern_pool;
67+
const pt = self.sema.pt;
68+
const ip = &pt.zcu.intern_pool;
6869
switch (node.get(self.file.zoir.?)) {
6970
.true => return .bool_true,
7071
.false => return .bool_false,
@@ -73,55 +74,75 @@ fn lowerExprAnonResTy(self: *LowerZon, node: Zoir.Node.Index) CompileError!Inter
7374
.neg_inf => return self.fail(node, "negative infinity requires a known result type", .{}),
7475
.nan => return self.fail(node, "nan requires a known result type", .{}),
7576
.int_literal => |int| switch (int) {
76-
.small => |val| return self.sema.pt.intern(.{ .int = .{
77+
.small => |val| return pt.intern(.{ .int = .{
7778
.ty = .comptime_int_type,
7879
.storage = .{ .i64 = val },
7980
} }),
80-
.big => |val| return self.sema.pt.intern(.{ .int = .{
81+
.big => |val| return pt.intern(.{ .int = .{
8182
.ty = .comptime_int_type,
8283
.storage = .{ .big_int = val },
8384
} }),
8485
},
8586
.float_literal => |val| {
86-
const result = try self.sema.pt.floatValue(.fromInterned(.comptime_float_type), val);
87+
const result = try pt.floatValue(.fromInterned(.comptime_float_type), val);
8788
return result.toIntern();
8889
},
89-
.char_literal => |val| return self.sema.pt.intern(.{ .int = .{
90+
.char_literal => |val| return pt.intern(.{ .int = .{
9091
.ty = .comptime_int_type,
9192
.storage = .{ .i64 = val },
9293
} }),
93-
.enum_literal => |val| return self.sema.pt.intern(.{
94+
.enum_literal => |val| return pt.intern(.{
9495
.enum_literal = try ip.getOrPutString(
95-
self.sema.gpa,
96-
self.sema.pt.tid,
96+
gpa,
97+
pt.tid,
9798
val.get(self.file.zoir.?),
9899
.no_embedded_nulls,
99100
),
100101
}),
101102
.string_literal => |val| {
102-
const ip_str = try ip.getOrPutString(gpa, self.sema.pt.tid, val, .maybe_embedded_nulls);
103+
const ip_str = try ip.getOrPutString(gpa, pt.tid, val, .maybe_embedded_nulls);
103104
const result = try self.sema.addStrLit(ip_str, val.len);
104105
return result.toInterned().?;
105106
},
106107
.empty_literal => {
107108
const ty = try ip.getTupleType(
108109
gpa,
109-
self.sema.pt.tid,
110+
pt.tid,
110111
.{
111112
.types = &.{},
112113
.values = &.{},
113114
},
114115
);
115-
return self.sema.pt.intern(.{ .aggregate = .{
116+
return pt.intern(.{ .aggregate = .{
116117
.ty = ty,
117118
.storage = .{ .elems = &.{} },
118119
} });
119120
},
120-
.array_literal, .struct_literal => @panic("unimplemented"),
121+
.array_literal => |nodes| {
122+
const types = try self.sema.arena.alloc(InternPool.Index, nodes.len);
123+
const values = try self.sema.arena.alloc(InternPool.Index, nodes.len);
124+
for (0..nodes.len) |i| {
125+
values[i] = try self.lowerExprAnonResTy(nodes.at(@intCast(i)));
126+
types[i] = Value.fromInterned(values[i]).typeOf(pt.zcu).toIntern();
127+
}
128+
const ty = try ip.getTupleType(
129+
gpa,
130+
pt.tid,
131+
.{
132+
.types = types,
133+
.values = values,
134+
},
135+
);
136+
return pt.intern(.{ .aggregate = .{
137+
.ty = ty,
138+
.storage = .{ .elems = values },
139+
} });
140+
},
141+
.struct_literal => @panic("unimplemented"),
121142
}
122143
}
123144

124-
/// Validate that `ty` is a valid ZON type, or is `.none`. If it is not, emit a compile error.
145+
/// Validate that `ty` is a valid ZON type, or emit a compile error.
125146
///
126147
/// Rules out nested optionals, error sets, etc.
127148
fn checkType(self: *LowerZon, ty: Type) !void {

0 commit comments

Comments
 (0)