| author | |
| committer | |
| log | fd208d9d5913a0929e444deb97b91092c427bb14 |
| tree | 2d3fa39c5f3b8eec18ead580a8dc1c7a0c091c51 |
| parent | 4360f45d7fa242f95aae86a137d56c737798dccf |
4 files changed, 41 insertions(+), 4 deletions(-)
src/astgen.zig+7-1| ... | ... | @@ -482,6 +482,13 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In |
| 482 | 482 | const result = try addZIRInst(mod, scope, src, zir.Inst.EnumLiteral, .{ .name = name }, .{}); |
| 483 | 483 | return rvalue(mod, scope, rl, result); |
| 484 | 484 | }, |
| 485 | .error_value => { | |
| 486 | const ident_token = node_datas[node].rhs; | |
| 487 | const name = try mod.identifierTokenString(scope, ident_token); | |
| 488 | const src = token_starts[ident_token]; | |
| 489 | const result = try addZirInstTag(mod, scope, src, .error_value, .{ .name = name }); | |
| 490 | return rvalue(mod, scope, rl, result); | |
| 491 | }, | |
| 485 | 492 | .error_union => { |
| 486 | 493 | const error_set = try typeExpr(mod, scope, node_datas[node].lhs); |
| 487 | 494 | const payload = try typeExpr(mod, scope, node_datas[node].rhs); |
| ... | ... | @@ -644,7 +651,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In |
| 644 | 651 | => return mod.failNode(scope, node, "TODO implement astgen.expr for function prototypes", .{}), |
| 645 | 652 | |
| 646 | 653 | .@"nosuspend" => return mod.failNode(scope, node, "TODO implement astgen.expr for .nosuspend", .{}), |
| 647 | .error_value => return mod.failNode(scope, node, "TODO implement astgen.expr for .error_value", .{}), | |
| 648 | 654 | } |
| 649 | 655 | } |
| 650 | 656 |
src/value.zig+3-3| ... | ... | @@ -2138,13 +2138,13 @@ pub const Value = extern union { |
| 2138 | 2138 | data: f128, |
| 2139 | 2139 | }; |
| 2140 | 2140 | |
| 2141 | // TODO move to type.zig | |
| 2141 | /// TODO move to type.zig | |
| 2142 | 2142 | pub const ErrorSet = struct { |
| 2143 | 2143 | pub const base_tag = Tag.error_set; |
| 2144 | 2144 | |
| 2145 | 2145 | base: Payload = .{ .tag = base_tag }, |
| 2146 | 2146 | data: struct { |
| 2147 | // TODO revisit this when we have the concept of the error tag type | |
| 2147 | /// TODO revisit this when we have the concept of the error tag type | |
| 2148 | 2148 | fields: std.StringHashMapUnmanaged(u16), |
| 2149 | 2149 | decl: *Module.Decl, |
| 2150 | 2150 | }, |
| ... | ... | @@ -2153,9 +2153,9 @@ pub const Value = extern union { |
| 2153 | 2153 | pub const Error = struct { |
| 2154 | 2154 | base: Payload = .{ .tag = .@"error" }, |
| 2155 | 2155 | data: struct { |
| 2156 | // TODO revisit this when we have the concept of the error tag type | |
| 2157 | 2156 | /// `name` is owned by `Module` and will be valid for the entire |
| 2158 | 2157 | /// duration of the compilation. |
| 2158 | /// TODO revisit this when we have the concept of the error tag type | |
| 2159 | 2159 | name: []const u8, |
| 2160 | 2160 | value: u16, |
| 2161 | 2161 | }, |
src/zir.zig+14| ... | ... | @@ -154,6 +154,8 @@ pub const Inst = struct { |
| 154 | 154 | error_union_type, |
| 155 | 155 | /// Create an error set. |
| 156 | 156 | error_set, |
| 157 | /// `error.Foo` syntax. | |
| 158 | error_value, | |
| 157 | 159 | /// Export the provided Decl as the provided name in the compilation's output object file. |
| 158 | 160 | @"export", |
| 159 | 161 | /// Given a pointer to a struct or object that contains virtual fields, returns a pointer |
| ... | ... | @@ -487,6 +489,7 @@ pub const Inst = struct { |
| 487 | 489 | .ptr_type => PtrType, |
| 488 | 490 | .enum_literal => EnumLiteral, |
| 489 | 491 | .error_set => ErrorSet, |
| 492 | .error_value => ErrorValue, | |
| 490 | 493 | .slice => Slice, |
| 491 | 494 | .typeof_peer => TypeOfPeer, |
| 492 | 495 | .container_field_named => ContainerFieldNamed, |
| ... | ... | @@ -612,6 +615,7 @@ pub const Inst = struct { |
| 612 | 615 | .error_union_type, |
| 613 | 616 | .bit_not, |
| 614 | 617 | .error_set, |
| 618 | .error_value, | |
| 615 | 619 | .slice, |
| 616 | 620 | .slice_start, |
| 617 | 621 | .import, |
| ... | ... | @@ -1099,6 +1103,16 @@ pub const Inst = struct { |
| 1099 | 1103 | kw_args: struct {}, |
| 1100 | 1104 | }; |
| 1101 | 1105 | |
| 1106 | pub const ErrorValue = struct { | |
| 1107 | pub const base_tag = Tag.error_value; | |
| 1108 | base: Inst, | |
| 1109 | ||
| 1110 | positionals: struct { | |
| 1111 | name: []const u8, | |
| 1112 | }, | |
| 1113 | kw_args: struct {}, | |
| 1114 | }; | |
| 1115 | ||
| 1102 | 1116 | pub const Slice = struct { |
| 1103 | 1117 | pub const base_tag = Tag.slice; |
| 1104 | 1118 | base: Inst, |
src/zir_sema.zig+17| ... | ... | @@ -149,6 +149,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError! |
| 149 | 149 | .error_union_type => return zirErrorUnionType(mod, scope, old_inst.castTag(.error_union_type).?), |
| 150 | 150 | .anyframe_type => return zirAnyframeType(mod, scope, old_inst.castTag(.anyframe_type).?), |
| 151 | 151 | .error_set => return zirErrorSet(mod, scope, old_inst.castTag(.error_set).?), |
| 152 | .error_value => return zirErrorValue(mod, scope, old_inst.castTag(.error_value).?), | |
| 152 | 153 | .slice => return zirSlice(mod, scope, old_inst.castTag(.slice).?), |
| 153 | 154 | .slice_start => return zirSliceStart(mod, scope, old_inst.castTag(.slice_start).?), |
| 154 | 155 | .import => return zirImport(mod, scope, old_inst.castTag(.import).?), |
| ... | ... | @@ -1166,6 +1167,22 @@ fn zirErrorSet(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorSet) InnerError |
| 1166 | 1167 | return mod.analyzeDeclVal(scope, inst.base.src, new_decl); |
| 1167 | 1168 | } |
| 1168 | 1169 | |
| 1170 | fn zirErrorValue(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorValue) InnerError!*Inst { | |
| 1171 | const tracy = trace(@src()); | |
| 1172 | defer tracy.end(); | |
| 1173 | ||
| 1174 | // Create an anonymous error set type with only this error value, and return the value. | |
| 1175 | const entry = try mod.getErrorValue(inst.positionals.name); | |
| 1176 | const result_type = try Type.Tag.error_set_single.create(scope.arena(), entry.key); | |
| 1177 | return mod.constInst(scope, inst.base.src, .{ | |
| 1178 | .ty = result_type, | |
| 1179 | .val = try Value.Tag.@"error".create(scope.arena(), .{ | |
| 1180 | .name = entry.key, | |
| 1181 | .value = entry.value, | |
| 1182 | }), | |
| 1183 | }); | |
| 1184 | } | |
| 1185 | ||
| 1169 | 1186 | fn zirMergeErrorSets(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst { |
| 1170 | 1187 | const tracy = trace(@src()); |
| 1171 | 1188 | defer tracy.end(); |