From 4c750016eb9b1c0831cbb0398a4d6ee9dbdc932e Mon Sep 17 00:00:00 2001 From: Loris Cro Date: Wed, 3 Aug 2022 17:21:56 +0200 Subject: [PATCH] autodoc: inferred error unions in function return values --- lib/docs/main.js | 14 +++++++++++++- src/Autodoc.zig | 20 ++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/docs/main.js b/lib/docs/main.js index 5435efd647..5ee39ec893 100644 --- a/lib/docs/main.js +++ b/lib/docs/main.js @@ -826,7 +826,13 @@ var zigAnalysis; function exprName(expr, opts) { switch (Object.keys(expr)[0]) { - default: throw "oh no"; + default: throw "this expression is not implemented yet"; + case "bool": { + if (expr.bool) { + return "true"; + } + return "false"; + } case "&": { return "&" + exprName(zigAnalysis.exprs[expr["&"]]); } @@ -1699,6 +1705,12 @@ var zigAnalysis; let rhs = exprName(errUnionObj.rhs, opts); return lhs + "!" + rhs; } + case typeKinds.InferredErrorUnion: + { + let errUnionObj = (typeObj); + let payload = exprName(errUnionObj.payload, opts); + return "!" + payload; + } case typeKinds.Fn: { let fnObj = (typeObj); diff --git a/src/Autodoc.zig b/src/Autodoc.zig index 35f9dc7dea..a98ad7aee0 100644 --- a/src/Autodoc.zig +++ b/src/Autodoc.zig @@ -468,7 +468,7 @@ const DocData = struct { child: Expr, }, ErrorUnion: struct { lhs: Expr, rhs: Expr }, - // ErrorUnion: struct { name: []const u8 }, + InferredErrorUnion: struct { payload: Expr }, ErrorSet: struct { name: []const u8, fields: ?[]const Field = null, @@ -582,7 +582,7 @@ const DocData = struct { typeOf: usize, // index in `exprs` typeInfo: usize, // index in `exprs` typeOf_peer: []usize, - errorUnion: usize, // index in `exprs` + errorUnion: usize, // index in `types` as: As, sizeOf: usize, // index in `exprs` bitSizeOf: usize, // index in `exprs` @@ -1929,7 +1929,7 @@ fn walkInstruction( .comptimeExpr = self.comptime_exprs.items.len, } }; try self.comptime_exprs.append(self.arena, .{ - .code = "if(banana) 1 else 0", + .code = "if (...) { ... }", }); return res; }, @@ -2119,6 +2119,7 @@ fn walkInstruction( inst_index, self_ast_node_index, type_slot_index, + tags[inst_index] == .func_inferred, ); return result; @@ -3416,6 +3417,7 @@ fn analyzeFunction( inst_index: usize, self_ast_node_index: usize, type_slot_index: usize, + ret_is_inferred_error_set: bool, ) AutodocErrors!DocData.WalkResult { const tags = file.zir.instructions.items(.tag); const data = file.zir.instructions.items(.data); @@ -3522,13 +3524,23 @@ fn analyzeFunction( else => null, }; + const ret_type: DocData.Expr = blk: { + if (ret_is_inferred_error_set) { + const ret_type_slot_index = self.types.items.len; + try self.types.append(self.arena, .{ + .InferredErrorUnion = .{ .payload = ret_type_ref }, + }); + break :blk .{ .type = ret_type_slot_index }; + } else break :blk ret_type_ref; + }; + self.ast_nodes.items[self_ast_node_index].fields = param_ast_indexes.items; self.types.items[type_slot_index] = .{ .Fn = .{ .name = "todo_name func", .src = self_ast_node_index, .params = param_type_refs.items, - .ret = ret_type_ref, + .ret = ret_type, .generic_ret = generic_ret, }, };