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;
826826
827827 function exprName(expr, opts) {
828828 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 }
830836 case "&": {
831837 return "&" + exprName(zigAnalysis.exprs[expr["&"]]);
832838 }
......@@ -1699,6 +1705,12 @@ var zigAnalysis;
16991705 let rhs = exprName(errUnionObj.rhs, opts);
17001706 return lhs + "!" + rhs;
17011707 }
1708 case typeKinds.InferredErrorUnion:
1709 {
1710 let errUnionObj = (typeObj);
1711 let payload = exprName(errUnionObj.payload, opts);
1712 return "!" + payload;
1713 }
17021714 case typeKinds.Fn:
17031715 {
17041716 let fnObj = (typeObj);
src/Autodoc.zig+16-4
......@@ -468,7 +468,7 @@ const DocData = struct {
468468 child: Expr,
469469 },
470470 ErrorUnion: struct { lhs: Expr, rhs: Expr },
471 // ErrorUnion: struct { name: []const u8 },
471 InferredErrorUnion: struct { payload: Expr },
472472 ErrorSet: struct {
473473 name: []const u8,
474474 fields: ?[]const Field = null,
......@@ -582,7 +582,7 @@ const DocData = struct {
582582 typeOf: usize, // index in `exprs`
583583 typeInfo: usize, // index in `exprs`
584584 typeOf_peer: []usize,
585 errorUnion: usize, // index in `exprs`
585 errorUnion: usize, // index in `types`
586586 as: As,
587587 sizeOf: usize, // index in `exprs`
588588 bitSizeOf: usize, // index in `exprs`
......@@ -1929,7 +1929,7 @@ fn walkInstruction(
19291929 .comptimeExpr = self.comptime_exprs.items.len,
19301930 } };
19311931 try self.comptime_exprs.append(self.arena, .{
1932 .code = "if(banana) 1 else 0",
1932 .code = "if (...) { ... }",
19331933 });
19341934 return res;
19351935 },
......@@ -2119,6 +2119,7 @@ fn walkInstruction(
21192119 inst_index,
21202120 self_ast_node_index,
21212121 type_slot_index,
2122 tags[inst_index] == .func_inferred,
21222123 );
21232124
21242125 return result;
......@@ -3416,6 +3417,7 @@ fn analyzeFunction(
34163417 inst_index: usize,
34173418 self_ast_node_index: usize,
34183419 type_slot_index: usize,
3420 ret_is_inferred_error_set: bool,
34193421) AutodocErrors!DocData.WalkResult {
34203422 const tags = file.zir.instructions.items(.tag);
34213423 const data = file.zir.instructions.items(.data);
......@@ -3522,13 +3524,23 @@ fn analyzeFunction(
35223524 else => null,
35233525 };
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
35253537 self.ast_nodes.items[self_ast_node_index].fields = param_ast_indexes.items;
35263538 self.types.items[type_slot_index] = .{
35273539 .Fn = .{
35283540 .name = "todo_name func",
35293541 .src = self_ast_node_index,
35303542 .params = param_type_refs.items,
3531 .ret = ret_type_ref,
3543 .ret = ret_type,
35323544 .generic_ret = generic_ret,
35333545 },
35343546 };