Repository navigation
Feat: add known error sets #3319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iwami4438
wants to merge
9
commits into
zigtools:master
Choose a base branch
from
iwami4438:feature/add-known-error-sets
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−27
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7479354
WIP: I believe this is the relevant code, so I’m adding it.
iwami4438 35bbf02
feat: ability to resolve partially known error sets
iwami4438 029db88
fix typo
iwami4438 f2bc63f
fix: an oversight regarding the handling of `error_set`.
iwami4438 3a71d2d
Merge branch 'master' into feature/add-known-error-sets
iwami4438 55025ea
fix: added a caret, as one was missing.
iwami4438 8bfe62b
fix: the code based on feedback.
iwami4438 389b539
Merge branch 'master' into feature/add-known-error-sets
iwami4438 2cdb34e
fix: Updated the implementation based on the proposal:
iwami4438 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,9 @@ pub const Key = union(enum) { | |
| pub const ErrorSet = struct { | ||
| owner_decl: Decl.OptionalIndex, | ||
| names: StringSlice, | ||
| flags: Flags, | ||
|
|
||
| pub const Flags = bitpack struct(u32) { has_unknown: bool, _: u31 = 0 }; | ||
| }; | ||
|
|
||
| pub const Function = struct { | ||
|
|
@@ -434,6 +437,7 @@ pub const Key = union(enum) { | |
| const b_info = b.error_set_type; | ||
|
|
||
| if (a_info.owner_decl != b_info.owner_decl) return false; | ||
| if (a_info.flags.has_unknown != b_info.flags.has_unknown) return false; | ||
|
|
||
| if (a_info.names.len != b_info.names.len) return false; | ||
|
|
||
|
|
@@ -1564,6 +1568,7 @@ fn addExtra(ip: *InternPool, comptime T: type, extra: T) Allocator.Error!u32 { | |
| Key.Pointer.Flags, | ||
| Key.Pointer.PackedOffset, | ||
| Key.Function.Flags, | ||
| Key.ErrorSet.Flags, | ||
| => ip.extra.appendAssumeCapacity(@bitCast(item)), | ||
|
|
||
| u64, | ||
|
|
@@ -1605,6 +1610,7 @@ fn extraData(ip: *const InternPool, comptime T: type, index: u32) T { | |
| Key.Pointer.Flags, | ||
| Key.Pointer.PackedOffset, | ||
| Key.Function.Flags, | ||
| Key.ErrorSet.Flags, | ||
| => @bitCast(item), | ||
|
|
||
| u64, | ||
|
|
@@ -3221,7 +3227,7 @@ fn isUnknownDeepInternal( | |
| }, | ||
| .optional_type => |optional_info| try ip.isUnknownDeepInternal(optional_info.payload_type, set), | ||
| .error_union_type => |error_union_info| try ip.isUnknownDeepInternal(error_union_info.payload_type, set), | ||
| .error_set_type => false, | ||
| .error_set_type => return false, | ||
| .enum_type => |enum_index| { | ||
| const enum_info = ip.getEnum(enum_index); | ||
| for (enum_info.values.keys()) |val| { | ||
|
|
@@ -3544,38 +3550,82 @@ pub fn scalarType(ip: *InternPool, ty: Index) Index { | |
| } | ||
|
|
||
| pub fn errorSetMerge(ip: *InternPool, a_ty: Index, b_ty: Index) Allocator.Error!Index { | ||
| assert(ip.zigTypeTag(a_ty) == .error_set); | ||
| assert(ip.zigTypeTag(b_ty) == .error_set); | ||
| const has_unknown = ip.isUnknown(a_ty) or ip.isUnknown(b_ty); | ||
|
|
||
| // Anything merged with anyerror is anyerror. | ||
| if (a_ty == .anyerror_type or b_ty == .anyerror_type) { | ||
| return .anyerror_type; | ||
| } | ||
|
|
||
| if (a_ty == b_ty) return a_ty; | ||
| if (has_unknown) { | ||
| if (ip.isUnknown(a_ty) and ip.isUnknown(b_ty)) { | ||
| return try ip.get(.{ | ||
| .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = .empty, | ||
| .flags = .{ | ||
| .has_unknown = true, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| if (ip.isUnknown(a_ty)) { | ||
| assert(ip.zigTypeTag(b_ty) == .error_set); | ||
| const names = ip.indexToKey(b_ty).error_set_type.names; | ||
| return try ip.get(.{ | ||
| .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = names, | ||
| .flags = .{ | ||
| .has_unknown = true, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| if (ip.isUnknown(b_ty)) { | ||
| assert(ip.zigTypeTag(a_ty) == .error_set); | ||
| const names = ip.indexToKey(a_ty).error_set_type.names; | ||
| return try ip.get(.{ | ||
| .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = names, | ||
| .flags = .{ | ||
| .has_unknown = true, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| unreachable; | ||
| } else { | ||
| assert(ip.zigTypeTag(a_ty) == .error_set); | ||
| assert(ip.zigTypeTag(b_ty) == .error_set); | ||
|
|
||
| const a_names = try ip.indexToKey(a_ty).error_set_type.names.dupe(ip.gpa, ip); | ||
| defer ip.gpa.free(a_names); | ||
| if (a_ty == b_ty) return a_ty; | ||
|
|
||
| const b_names = try ip.indexToKey(b_ty).error_set_type.names.dupe(ip.gpa, ip); | ||
| defer ip.gpa.free(b_names); | ||
| const a_names = try ip.indexToKey(a_ty).error_set_type.names.dupe(ip.gpa, ip); | ||
| defer ip.gpa.free(a_names); | ||
|
|
||
| var set: std.array_hash_map.Auto(String, void) = .empty; | ||
| defer set.deinit(ip.gpa); | ||
| const b_names = try ip.indexToKey(b_ty).error_set_type.names.dupe(ip.gpa, ip); | ||
| defer ip.gpa.free(b_names); | ||
|
|
||
| try set.ensureTotalCapacity(ip.gpa, a_names.len + b_names.len); | ||
| var set: std.array_hash_map.Auto(String, void) = .empty; | ||
| defer set.deinit(ip.gpa); | ||
|
|
||
| for (a_names) |name| set.putAssumeCapacityNoClobber(name, {}); | ||
| for (b_names) |name| set.putAssumeCapacity(name, {}); | ||
| try set.ensureTotalCapacity(ip.gpa, a_names.len + b_names.len); | ||
|
|
||
| ip.string_pool.sortStrings(ip.io, set.keys()); | ||
| for (a_names) |name| set.putAssumeCapacityNoClobber(name, {}); | ||
| for (b_names) |name| set.putAssumeCapacity(name, {}); | ||
|
|
||
| return try ip.get(.{ | ||
| .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = try ip.getStringSlice(set.keys()), | ||
| }, | ||
| }); | ||
| ip.string_pool.sortStrings(ip.io, set.keys()); | ||
|
|
||
| const a_or_b_has_unknown = ip.indexToKey(a_ty).error_set_type.flags.has_unknown or ip.indexToKey(b_ty).error_set_type.flags.has_unknown; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is best to avoid unnecessarily calling |
||
| return try ip.get(.{ | ||
| .error_set_type = .{ .owner_decl = .none, .names = try ip.getStringSlice(set.keys()), .flags = .{ .has_unknown = a_or_b_has_unknown } }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// Asserts the type is a vector or tuple. | ||
|
|
@@ -4041,6 +4091,12 @@ fn printInternal(ip: *InternPool, ty: Index, writer: *std.Io.Writer, options: Fo | |
| const name = error_set_info.names.at(@intCast(i), ip); | ||
| try writer.print("{f}", .{ip.fmtId(name)}); | ||
| } | ||
| if (error_set_info.flags.has_unknown) { | ||
| if (error_set_info.names.len > 0) { | ||
| try writer.writeByte(','); | ||
| } | ||
| try writer.writeAll(" ... "); | ||
| } | ||
| try writer.writeByte('}'); | ||
| }, | ||
| .enum_type => return panicOrElse(?Index, "TODO", null), | ||
|
|
@@ -4583,16 +4639,19 @@ test "error set type" { | |
| const empty_error_set = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = StringSlice.empty, | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
|
|
||
| const foo_bar_baz_set = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = try ip.getStringSlice(&.{ foo_name, bar_name, baz_name }), | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
|
|
||
| const foo_bar_set = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = try ip.getStringSlice(&.{ foo_name, bar_name }), | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
|
|
||
| try expect(empty_error_set != foo_bar_baz_set); | ||
|
|
@@ -4613,6 +4672,7 @@ test "error union type" { | |
| const empty_error_set = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = StringSlice.empty, | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
| const bool_type = try ip.get(.{ .simple_type = .bool }); | ||
|
|
||
|
|
@@ -4979,18 +5039,22 @@ test "coerceInMemoryAllowed error set" { | |
| const foo_bar_baz_set = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = try ip.getStringSlice(&.{ baz_name, bar_name, foo_name }), | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
| const foo_bar_set = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = try ip.getStringSlice(&.{ foo_name, bar_name }), | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
| const foo_set = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = try ip.getStringSlice(&.{foo_name}), | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
| const empty_set = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = StringSlice.empty, | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
|
|
||
| try expect(try ip.coerceInMemoryAllowed(arena, .anyerror_type, foo_bar_baz_set, true, builtin.target) == .ok); | ||
|
|
@@ -5277,16 +5341,19 @@ test "resolvePeerTypes error sets" { | |
| const @"error{foo}" = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = try ip.getStringSlice(&.{foo_name}), | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
|
|
||
| const @"error{bar}" = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = try ip.getStringSlice(&.{bar_name}), | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
|
|
||
| const @"error{bar,foo}" = try ip.get(.{ .error_set_type = .{ | ||
| .owner_decl = .none, | ||
| .names = try ip.getStringSlice(&.{ bar_name, foo_name }), | ||
| .flags = .{ .has_unknown = false }, | ||
| } }); | ||
|
|
||
| const @"error{bar}!i32" = try ip.get(.{ .error_union_type = .{ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is unnecessary and can be safely removed.