authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-08-03 17:21:56+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-08-03 17:21:56+02:00
log4c750016eb9b1c0831cbb0398a4d6ee9dbdc932e
tree055dd9000ed66fa91d98f021c9d4c0a3de6fe33f
parentecccf1f91f70e6d844eecfd848ddfa9951d9e6b1

autodoc: inferred error unions in function return values


2 files changed, 29 insertions(+), 5 deletions(-)

lib/docs/main.js+13-1
...@@ -826,7 +826,13 @@ var zigAnalysis;...@@ -826,7 +826,13 @@ var zigAnalysis;
826 826
827 function exprName(expr, opts) {827 function exprName(expr, opts) {
828 switch (Object.keys(expr)[0]) {828 switch (Object.keys(expr)[0]) {
829 default: throw "oh no";829 default: throw "this expression is not implemented yet";
830 case "bool": {
831 if (expr.bool) {
832 return "true";
833 }
834 return "false";
835 }
830 case "&": {836 case "&": {
831 return "&" + exprName(zigAnalysis.exprs[expr["&"]]);837 return "&" + exprName(zigAnalysis.exprs[expr["&"]]);
832 }838 }
...@@ -1699,6 +1705,12 @@ var zigAnalysis;...@@ -1699,6 +1705,12 @@ var zigAnalysis;
1699 let rhs = exprName(errUnionObj.rhs, opts);1705 let rhs = exprName(errUnionObj.rhs, opts);
1700 return lhs + "!" + rhs;1706 return lhs + "!" + rhs;
1701 }1707 }
1708 case typeKinds.InferredErrorUnion:
1709 {
1710 let errUnionObj = (typeObj);
1711 let payload = exprName(errUnionObj.payload, opts);
1712 return "!" + payload;
1713 }
1702 case typeKinds.Fn:1714 case typeKinds.Fn:
1703 {1715 {
1704 let fnObj = (typeObj);1716 let fnObj = (typeObj);
src/Autodoc.zig+16-4
...@@ -468,7 +468,7 @@ const DocData = struct {...@@ -468,7 +468,7 @@ const DocData = struct {
468 child: Expr,468 child: Expr,
469 },469 },
470 ErrorUnion: struct { lhs: Expr, rhs: Expr },470 ErrorUnion: struct { lhs: Expr, rhs: Expr },
471 // ErrorUnion: struct { name: []const u8 },471 InferredErrorUnion: struct { payload: Expr },
472 ErrorSet: struct {472 ErrorSet: struct {
473 name: []const u8,473 name: []const u8,
474 fields: ?[]const Field = null,474 fields: ?[]const Field = null,
...@@ -582,7 +582,7 @@ const DocData = struct {...@@ -582,7 +582,7 @@ const DocData = struct {
582 typeOf: usize, // index in `exprs`582 typeOf: usize, // index in `exprs`
583 typeInfo: usize, // index in `exprs`583 typeInfo: usize, // index in `exprs`
584 typeOf_peer: []usize,584 typeOf_peer: []usize,
585 errorUnion: usize, // index in `exprs`585 errorUnion: usize, // index in `types`
586 as: As,586 as: As,
587 sizeOf: usize, // index in `exprs`587 sizeOf: usize, // index in `exprs`
588 bitSizeOf: usize, // index in `exprs`588 bitSizeOf: usize, // index in `exprs`
...@@ -1929,7 +1929,7 @@ fn walkInstruction(...@@ -1929,7 +1929,7 @@ fn walkInstruction(
1929 .comptimeExpr = self.comptime_exprs.items.len,1929 .comptimeExpr = self.comptime_exprs.items.len,
1930 } };1930 } };
1931 try self.comptime_exprs.append(self.arena, .{1931 try self.comptime_exprs.append(self.arena, .{
1932 .code = "if(banana) 1 else 0",1932 .code = "if (...) { ... }",
1933 });1933 });
1934 return res;1934 return res;
1935 },1935 },
...@@ -2119,6 +2119,7 @@ fn walkInstruction(...@@ -2119,6 +2119,7 @@ fn walkInstruction(
2119 inst_index,2119 inst_index,
2120 self_ast_node_index,2120 self_ast_node_index,
2121 type_slot_index,2121 type_slot_index,
2122 tags[inst_index] == .func_inferred,
2122 );2123 );
21232124
2124 return result;2125 return result;
...@@ -3416,6 +3417,7 @@ fn analyzeFunction(...@@ -3416,6 +3417,7 @@ fn analyzeFunction(
3416 inst_index: usize,3417 inst_index: usize,
3417 self_ast_node_index: usize,3418 self_ast_node_index: usize,
3418 type_slot_index: usize,3419 type_slot_index: usize,
3420 ret_is_inferred_error_set: bool,
3419) AutodocErrors!DocData.WalkResult {3421) AutodocErrors!DocData.WalkResult {
3420 const tags = file.zir.instructions.items(.tag);3422 const tags = file.zir.instructions.items(.tag);
3421 const data = file.zir.instructions.items(.data);3423 const data = file.zir.instructions.items(.data);
...@@ -3522,13 +3524,23 @@ fn analyzeFunction(...@@ -3522,13 +3524,23 @@ fn analyzeFunction(
3522 else => null,3524 else => null,
3523 };3525 };
35243526
3527 const ret_type: DocData.Expr = blk: {
3528 if (ret_is_inferred_error_set) {
3529 const ret_type_slot_index = self.types.items.len;
3530 try self.types.append(self.arena, .{
3531 .InferredErrorUnion = .{ .payload = ret_type_ref },
3532 });
3533 break :blk .{ .type = ret_type_slot_index };
3534 } else break :blk ret_type_ref;
3535 };
3536
3525 self.ast_nodes.items[self_ast_node_index].fields = param_ast_indexes.items;3537 self.ast_nodes.items[self_ast_node_index].fields = param_ast_indexes.items;
3526 self.types.items[type_slot_index] = .{3538 self.types.items[type_slot_index] = .{
3527 .Fn = .{3539 .Fn = .{
3528 .name = "todo_name func",3540 .name = "todo_name func",
3529 .src = self_ast_node_index,3541 .src = self_ast_node_index,
3530 .params = param_type_refs.items,3542 .params = param_type_refs.items,
3531 .ret = ret_type_ref,3543 .ret = ret_type,
3532 .generic_ret = generic_ret,3544 .generic_ret = generic_ret,
3533 },3545 },
3534 };3546 };