Skip to content

Commit f929c38

Browse files
authored
Merge pull request #2644 from FnControlOption/intern
Intern {pointer,array,tuple,optional,error union} types when possible
2 parents 27532e2 + b82a130 commit f929c38

11 files changed

Lines changed: 612 additions & 261 deletions

File tree

src/analyser/InternPool.zig

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,27 @@ pub fn resolvePeerTypes(ip: *InternPool, types: []const Index, target: std.Targe
22582258
chosen = try ip.errorSetMerge(chosen, candidate);
22592259
continue;
22602260
},
2261+
.error_union_type => |chosen_info| {
2262+
if (chosen_info.error_set_type != .none) {
2263+
chosen = try ip.get(.{ .error_union_type = .{
2264+
.error_set_type = try ip.errorSetMerge(chosen_info.error_set_type, candidate),
2265+
.payload_type = chosen_info.payload_type,
2266+
} });
2267+
continue;
2268+
}
2269+
},
2270+
else => {},
2271+
},
2272+
.error_union_type => |candidate_info| switch (chosen_key) {
2273+
.error_set_type => {
2274+
if (candidate_info.error_set_type != .none) {
2275+
chosen = try ip.get(.{ .error_union_type = .{
2276+
.error_set_type = try ip.errorSetMerge(chosen, candidate_info.error_set_type),
2277+
.payload_type = candidate_info.payload_type,
2278+
} });
2279+
continue;
2280+
}
2281+
},
22612282
else => {},
22622283
},
22632284
else => {},
@@ -3534,6 +3555,8 @@ pub fn errorSetMerge(ip: *InternPool, a_ty: Index, b_ty: Index) Allocator.Error!
35343555
for (a_names) |name| set.putAssumeCapacityNoClobber(name, {});
35353556
for (b_names) |name| set.putAssumeCapacity(name, {});
35363557

3558+
ip.string_pool.sortStrings(ip.io, set.keys());
3559+
35373560
return try ip.get(.{
35383561
.error_set_type = .{
35393562
.owner_decl = .none,
@@ -4057,7 +4080,7 @@ fn printInternal(ip: *InternPool, ty: Index, writer: *std.Io.Writer, options: Fo
40574080
.union_type => return panicOrElse(?Index, "TODO", null),
40584081
.tuple_type => |tuple_info| {
40594082
assert(tuple_info.types.len == tuple_info.values.len);
4060-
try writer.writeAll("tuple{");
4083+
try writer.writeAll("struct { ");
40614084

40624085
for (0..tuple_info.types.len) |i| {
40634086
const field_ty = tuple_info.types.at(@intCast(i), ip);
@@ -4073,7 +4096,7 @@ fn printInternal(ip: *InternPool, ty: Index, writer: *std.Io.Writer, options: Fo
40734096
try ip.print(field_val, writer, options);
40744097
}
40754098
}
4076-
try writer.writeByte('}');
4099+
try writer.writeAll(" }");
40774100
},
40784101
.vector_type => |vector_info| {
40794102
try writer.print("@Vector({d},{f})", .{
@@ -5248,18 +5271,23 @@ test "resolvePeerTypes error sets" {
52485271
.names = try ip.getStringSlice(&.{bar_name}),
52495272
} });
52505273

5251-
const @"error{foo,bar}" = try ip.get(.{ .error_set_type = .{
5252-
.owner_decl = .none,
5253-
.names = try ip.getStringSlice(&.{ foo_name, bar_name }),
5254-
} });
5255-
52565274
const @"error{bar,foo}" = try ip.get(.{ .error_set_type = .{
52575275
.owner_decl = .none,
52585276
.names = try ip.getStringSlice(&.{ bar_name, foo_name }),
52595277
} });
52605278

5261-
try ip.testResolvePeerTypesInOrder(@"error{foo}", @"error{bar}", @"error{foo,bar}");
5262-
try ip.testResolvePeerTypesInOrder(@"error{bar}", @"error{foo}", @"error{bar,foo}");
5279+
const @"error{bar}!i32" = try ip.get(.{ .error_union_type = .{
5280+
.error_set_type = @"error{bar}",
5281+
.payload_type = .i32_type,
5282+
} });
5283+
5284+
const @"error{bar,foo}!i32" = try ip.get(.{ .error_union_type = .{
5285+
.error_set_type = @"error{bar,foo}",
5286+
.payload_type = .i32_type,
5287+
} });
5288+
5289+
try ip.testResolvePeerTypes(@"error{foo}", @"error{bar}", @"error{bar,foo}");
5290+
try ip.testResolvePeerTypes(@"error{foo}", @"error{bar}!i32", @"error{bar,foo}!i32");
52635291
}
52645292

52655293
fn testResolvePeerTypes(ip: *InternPool, a: Index, b: Index, expected: Index) !void {

src/analyser/completions.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ pub fn dotCompletions(
170170
try completions.ensureUnusedCapacity(arena, tuple_info.types.len);
171171
for (tuple_types, 0..) |tuple_ty, i| {
172172
completions.appendAssumeCapacity(.{
173-
.label = try std.fmt.allocPrint(arena, "{d}", .{i}),
173+
.label = try std.fmt.allocPrint(arena, "@\"{d}\"", .{i}),
174174
.kind = .Field,
175-
.detail = try std.fmt.allocPrint(arena, "{d}: {f}", .{ i, tuple_ty.fmt(ip) }),
175+
.detail = try std.fmt.allocPrint(arena, "{f}", .{tuple_ty.fmt(ip)}),
176176
});
177177
}
178178
},

src/analyser/string_pool.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ pub fn StringPool(comptime config: Config) type {
167167
return std.mem.sliceTo(string_bytes + start, 0);
168168
}
169169

170+
pub fn sortStrings(pool: *Pool, io: std.Io, indexes: []String) void {
171+
pool.mutex.lockUncancelable(io);
172+
defer pool.mutex.unlock(io);
173+
std.mem.sort(String, indexes, pool, stringLessThanUnsafe);
174+
}
175+
176+
fn stringLessThanUnsafe(pool: *Pool, a: String, b: String) bool {
177+
const a_slice = pool.stringToSliceUnsafe(a);
178+
const b_slice = pool.stringToSliceUnsafe(b);
179+
return std.mem.lessThan(u8, a_slice, b_slice);
180+
}
181+
170182
mutex: MutexType,
171183
bytes: std.ArrayList(u8),
172184
map: std.HashMapUnmanaged(u32, void, std.hash_map.StringIndexContext, std.hash_map.default_max_load_percentage),

0 commit comments

Comments
 (0)