authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-08 13:41:13-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-08 13:41:13-04:00
log784a493dc7114b6e6807c2a37d03dbadecee81e6
treeb2a9709664aed3f9a2d53aa4c233a3887aa93984
parent03a6b33a73612500038635633d4c8326c2b5cf5f
signaturelock-open Commit is signed but in an unrecognized format.

generated docs: functions with inferred error sets display nicely

infrastructure in place for displaying error sets

6 files changed, 156 insertions(+), 20 deletions(-)

lib/std/special/docs/index.html+1
......@@ -262,6 +262,7 @@
262262 </div>
263263 <h1 id="hdrName" class="hidden"></h1>
264264 <div id="fnDocs" class="hidden"></div>
265 <div id="fnErrors" class="hidden"></div>
265266 <div id="fnExamples" class="hidden"></div>
266267 <div id="fnNoExamples" class="hidden">
267268 <p>This function is not tested or referenced.</p>
lib/std/special/docs/main.js+56-8
......@@ -11,6 +11,7 @@
1111 var domFnProto = document.getElementById("fnProto");
1212 var domFnProtoCode = document.getElementById("fnProtoCode");
1313 var domFnDocs = document.getElementById("fnDocs");
14 var domFnErrors = document.getElementById("fnErrors");
1415 var domFnExamples = document.getElementById("fnExamples");
1516 var domFnNoExamples = document.getElementById("fnNoExamples");
1617 var domSearch = document.getElementById("search");
......@@ -33,6 +34,8 @@
3334 var typeKindFloatId;
3435 var typeKindIntId;
3536 var typeKindBoolId;
37 var typeKindErrSetId;
38 var typeKindErrUnionId;
3639 findTypeKinds();
3740
3841 // for each package, is an array with packages to get to this one
......@@ -96,6 +99,7 @@
9699 domSectInfo.classList.add("hidden");
97100 domHdrName.classList.add("hidden");
98101 domSectNav.classList.add("hidden");
102 domFnErrors.classList.add("hidden");
99103 domFnExamples.classList.add("hidden");
100104 domFnNoExamples.classList.add("hidden");
101105
......@@ -183,7 +187,7 @@
183187
184188 protoHtml += ') ';
185189 if (typeObj.ret != null) {
186 protoHtml += typeIndexName(typeObj.ret, true, true);
190 protoHtml += typeIndexName(typeObj.ret, true, true, fnDecl.value);
187191 } else {
188192 protoHtml += '<span class="tok-kw">var</span>';
189193 }
......@@ -335,24 +339,24 @@
335339 }
336340 }
337341
338 function typeIndexName(typeIndex, wantHtml, wantLink) {
342 function typeIndexName(typeIndex, wantHtml, wantLink, fnIndex) {
339343 var typeObj = zigAnalysis.types[typeIndex];
340344 if (wantLink) {
341345 var declIndex = getCanonTypeDecl(typeIndex);
342346 var declPath = getCanonDeclPath(declIndex);
343347 var haveLink = declPath != null;
344 var typeNameHtml = typeName(typeObj, true, !haveLink);
348 var typeNameHtml = typeName(typeObj, true, !haveLink, fnIndex);
345349 if (haveLink) {
346350 return '<a href="' + navLink(declPath.pkgNames, declPath.declNames) + '">' + typeNameHtml + '</a>';
347351 } else {
348352 return typeNameHtml;
349353 }
350354 } else {
351 return typeName(typeObj, wantHtml);
355 return typeName(typeObj, wantHtml, false, fnIndex);
352356 }
353357 }
354358
355 function typeName(typeObj, wantHtml, wantSubLink) {
359 function typeName(typeObj, wantHtml, wantSubLink, fnIndex) {
356360 switch (typeObj.kind) {
357361 case typeKindPtrId:
358362 var name = "";
......@@ -397,11 +401,22 @@
397401 name += typeObj.align;
398402 }
399403 if (typeObj.hostIntBytes != null) {
400 name += ":" + typeObj.bitOffsetInHost + ":" + typeObj.hostIntBytes;
404 name += ":";
405 if (wantHtml) {
406 name += '<span class="tok-number">' + typeObj.bitOffsetInHost + '</span>';
407 } else {
408 name += typeObj.bitOffsetInHost;
409 }
410 name += ":";
411 if (wantHtml) {
412 name += '<span class="tok-number">' + typeObj.hostIntBytes + '</span>';
413 } else {
414 name += typeObj.hostIntBytes;
415 }
401416 }
402417 name += ") ";
403418 }
404 name += typeIndexName(typeObj.elem, wantHtml, wantSubLink);
419 name += typeIndexName(typeObj.elem, wantHtml, wantSubLink, null);
405420 return name;
406421 case typeKindFloatId:
407422 if (wantHtml) {
......@@ -429,6 +444,29 @@
429444 } else {
430445 return "bool";
431446 }
447 case typeKindErrSetId:
448 if (typeObj.errors == null) {
449 if (wantHtml) {
450 return '<span class="tok-type">anyerror</span>';
451 } else {
452 return "anyerror";
453 }
454 } else {
455 if (wantHtml) {
456 return escapeHtml(typeObj.name);
457 } else {
458 return typeObj.name;
459 }
460 }
461 case typeKindErrUnionId:
462 var errSetTypeObj = zigAnalysis.types[typeObj.err];
463 var payloadHtml = typeIndexName(typeObj.payload, wantHtml, wantSubLink, null);
464 if (errSetTypeObj.fn != null && errSetTypeObj.fn == fnIndex) {
465 // function index parameter supplied and this is the inferred error set of it
466 return "!" + payloadHtml;
467 } else {
468 return typeIndexName(typeObj.err, wantHtml, wantSubLink, null) + "!" + payloadHtml;
469 }
432470 default:
433471 if (wantHtml) {
434472 return escapeHtml(typeObj.name);
......@@ -439,7 +477,7 @@
439477 }
440478
441479 function renderType(typeObj) {
442 var name = typeName(typeObj);
480 var name = typeName(typeObj, false, false);
443481 if (name != null && name != "") {
444482 domHdrName.innerText = zigAnalysis.typeKinds[typeObj.kind] + " " + name;
445483 domHdrName.classList.remove("hidden");
......@@ -543,6 +581,10 @@
543581 typeKindIntId = i;
544582 } else if (zigAnalysis.typeKinds[i] === "Bool") {
545583 typeKindBoolId = i;
584 } else if (zigAnalysis.typeKinds[i] === "ErrorSet") {
585 typeKindErrSetId = i;
586 } else if (zigAnalysis.typeKinds[i] === "ErrorUnion") {
587 typeKindErrUnionId = i;
546588 }
547589 }
548590 if (typeKindTypeId == null) {
......@@ -563,6 +605,12 @@
563605 if (typeKindBoolId == null) {
564606 throw new Error("No type kind 'Bool' found");
565607 }
608 if (typeKindErrSetId == null) {
609 throw new Error("No type kind 'ErrorSet' found");
610 }
611 if (typeKindErrUnionId == null) {
612 throw new Error("No type kind 'ErrorUnion' found");
613 }
566614 }
567615
568616 function findTypeTypeId() {
src/all_types.hpp+5-1
......@@ -1290,9 +1290,10 @@ struct ZigTypeErrorUnion {
12901290};
12911291
12921292struct ZigTypeErrorSet {
1293 uint32_t err_count;
12941293 ErrorTableEntry **errors;
12951294 ZigFn *infer_fn;
1295 uint32_t err_count;
1296 bool incomplete;
12961297};
12971298
12981299struct ZigTypeEnum {
......@@ -1328,6 +1329,9 @@ bool node_ptr_eql(const AstNode *a, const AstNode *b);
13281329uint32_t fn_ptr_hash(const ZigFn *ptr);
13291330bool fn_ptr_eql(const ZigFn *a, const ZigFn *b);
13301331
1332uint32_t err_ptr_hash(const ErrorTableEntry *ptr);
1333bool err_ptr_eql(const ErrorTableEntry *a, const ErrorTableEntry *b);
1334
13311335struct ZigTypeUnion {
13321336 AstNode *decl_node;
13331337 TypeUnionField *fields;
src/analyze.cpp+19-6
......@@ -1633,6 +1633,7 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
16331633 err_set_type->data.error_set.err_count = 0;
16341634 err_set_type->data.error_set.errors = nullptr;
16351635 err_set_type->data.error_set.infer_fn = fn_entry;
1636 err_set_type->data.error_set.incomplete = true;
16361637 err_set_type->size_in_bits = g->builtin_types.entry_global_error_set->size_in_bits;
16371638 err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align;
16381639 err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size;
......@@ -4277,12 +4278,12 @@ static void define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) {
42774278bool resolve_inferred_error_set(CodeGen *g, ZigType *err_set_type, AstNode *source_node) {
42784279 assert(err_set_type->id == ZigTypeIdErrorSet);
42794280 ZigFn *infer_fn = err_set_type->data.error_set.infer_fn;
4280 if (infer_fn != nullptr) {
4281 if (infer_fn != nullptr && err_set_type->data.error_set.incomplete) {
42814282 if (infer_fn->anal_state == FnAnalStateInvalid) {
42824283 return false;
42834284 } else if (infer_fn->anal_state == FnAnalStateReady) {
42844285 analyze_fn_body(g, infer_fn);
4285 if (err_set_type->data.error_set.infer_fn != nullptr) {
4286 if (err_set_type->data.error_set.incomplete) {
42864287 assert(g->errors.length != 0);
42874288 return false;
42884289 }
......@@ -4509,7 +4510,9 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {
45094510
45104511 if (fn_type_id->return_type->id == ZigTypeIdErrorUnion) {
45114512 ZigType *return_err_set_type = fn_type_id->return_type->data.error_union.err_set_type;
4512 if (return_err_set_type->data.error_set.infer_fn != nullptr) {
4513 if (return_err_set_type->data.error_set.infer_fn != nullptr &&
4514 return_err_set_type->data.error_set.incomplete)
4515 {
45134516 ZigType *inferred_err_set_type;
45144517 if (fn->src_implicit_return_type->id == ZigTypeIdErrorSet) {
45154518 inferred_err_set_type = fn->src_implicit_return_type;
......@@ -4522,14 +4525,16 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {
45224525 return;
45234526 }
45244527
4525 if (inferred_err_set_type->data.error_set.infer_fn != nullptr) {
4528 if (inferred_err_set_type->data.error_set.infer_fn != nullptr &&
4529 inferred_err_set_type->data.error_set.incomplete)
4530 {
45264531 if (!resolve_inferred_error_set(g, inferred_err_set_type, return_type_node)) {
45274532 fn->anal_state = FnAnalStateInvalid;
45284533 return;
45294534 }
45304535 }
45314536
4532 return_err_set_type->data.error_set.infer_fn = nullptr;
4537 return_err_set_type->data.error_set.incomplete = false;
45334538 if (type_is_global_error_set(inferred_err_set_type)) {
45344539 return_err_set_type->data.error_set.err_count = UINT32_MAX;
45354540 } else {
......@@ -7336,6 +7341,14 @@ bool fn_ptr_eql(const ZigFn *a, const ZigFn *b) {
73367341 return a == b;
73377342}
73387343
7344uint32_t err_ptr_hash(const ErrorTableEntry *ptr) {
7345 return hash_ptr((void*)ptr);
7346}
7347
7348bool err_ptr_eql(const ErrorTableEntry *a, const ErrorTableEntry *b) {
7349 return a == b;
7350}
7351
73397352ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
73407353 Tld *tld = get_container_scope(codegen->compile_var_import)->decl_table.get(buf_create_from_str(name));
73417354 resolve_top_level_decl(codegen, tld, nullptr, false);
......@@ -7348,7 +7361,7 @@ ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
73487361
73497362bool type_is_global_error_set(ZigType *err_set_type) {
73507363 assert(err_set_type->id == ZigTypeIdErrorSet);
7351 assert(err_set_type->data.error_set.infer_fn == nullptr);
7364 assert(!err_set_type->data.error_set.incomplete);
73527365 return err_set_type->data.error_set.err_count == UINT32_MAX;
73537366}
73547367
src/dump_analysis.cpp+72-2
......@@ -357,6 +357,9 @@ struct AnalDumpCtx {
357357
358358 ZigList<AstNode *> node_list;
359359 HashMap<const AstNode *, uint32_t, node_ptr_hash, node_ptr_eql> node_map;
360
361 ZigList<ErrorTableEntry *> err_list;
362 HashMap<const ErrorTableEntry *, uint32_t, err_ptr_hash, err_ptr_eql> err_map;
360363};
361364
362365static uint32_t anal_dump_get_type_id(AnalDumpCtx *ctx, ZigType *ty);
......@@ -444,6 +447,17 @@ static uint32_t anal_dump_get_fn_id(AnalDumpCtx *ctx, ZigFn *fn) {
444447 return fn_id;
445448}
446449
450static uint32_t anal_dump_get_err_id(AnalDumpCtx *ctx, ErrorTableEntry *err) {
451 uint32_t err_id = ctx->err_list.length;
452 auto existing_entry = ctx->err_map.put_unique(err, err_id);
453 if (existing_entry == nullptr) {
454 ctx->err_list.append(err);
455 } else {
456 err_id = existing_entry->value;
457 }
458 return err_id;
459}
460
447461static uint32_t anal_dump_get_decl_id(AnalDumpCtx *ctx, Tld *tld) {
448462 uint32_t decl_id = ctx->decl_list.length;
449463 auto existing_entry = ctx->decl_map.put_unique(tld, decl_id);
......@@ -513,6 +527,11 @@ static void anal_dump_fn_ref(AnalDumpCtx *ctx, ZigFn *fn) {
513527 jw_int(&ctx->jw, fn_id);
514528}
515529
530static void anal_dump_err_ref(AnalDumpCtx *ctx, ErrorTableEntry *err) {
531 uint32_t err_id = anal_dump_get_err_id(ctx, err);
532 jw_int(&ctx->jw, err_id);
533}
534
516535static void anal_dump_decl_ref(AnalDumpCtx *ctx, Tld *tld) {
517536 uint32_t decl_id = anal_dump_get_decl_id(ctx, tld);
518537 jw_int(&ctx->jw, decl_id);
......@@ -841,6 +860,33 @@ static void anal_dump_type(AnalDumpCtx *ctx, ZigType *ty) {
841860 anal_dump_pointer_attrs(ctx, ty);
842861 break;
843862 }
863 case ZigTypeIdErrorSet: {
864 if (type_is_global_error_set(ty)) {
865 break;
866 }
867 if (ty->data.error_set.infer_fn != nullptr) {
868 jw_object_field(jw, "fn");
869 anal_dump_fn_ref(ctx, ty->data.error_set.infer_fn);
870 }
871 jw_object_field(jw, "errors");
872 jw_begin_array(jw);
873 for (uint32_t i = 0; i < ty->data.error_set.err_count; i += 1) {
874 jw_array_elem(jw);
875 ErrorTableEntry *err = ty->data.error_set.errors[i];
876 anal_dump_err_ref(ctx, err);
877 }
878 jw_end_array(jw);
879 break;
880 }
881 case ZigTypeIdErrorUnion: {
882 jw_object_field(jw, "err");
883 anal_dump_type_ref(ctx, ty->data.error_union.err_set_type);
884
885 jw_object_field(jw, "payload");
886 anal_dump_type_ref(ctx, ty->data.error_union.payload_type);
887
888 break;
889 }
844890 default:
845891 jw_object_field(jw, "name");
846892 jw_string(jw, buf_ptr(&ty->name));
......@@ -849,7 +895,7 @@ static void anal_dump_type(AnalDumpCtx *ctx, ZigType *ty) {
849895 jw_end_object(jw);
850896}
851897
852void anal_dump_node(AnalDumpCtx *ctx, const AstNode *node) {
898static void anal_dump_node(AnalDumpCtx *ctx, const AstNode *node) {
853899 JsonWriter *jw = &ctx->jw;
854900
855901 jw_begin_object(jw);
......@@ -892,7 +938,21 @@ void anal_dump_node(AnalDumpCtx *ctx, const AstNode *node) {
892938 jw_end_object(jw);
893939}
894940
895void anal_dump_fn(AnalDumpCtx *ctx, ZigFn *fn) {
941static void anal_dump_err(AnalDumpCtx *ctx, const ErrorTableEntry *err) {
942 JsonWriter *jw = &ctx->jw;
943
944 jw_begin_object(jw);
945
946 jw_object_field(jw, "src");
947 anal_dump_node_ref(ctx, err->decl_node);
948
949 jw_object_field(jw, "name");
950 jw_string(jw, buf_ptr(&err->name));
951
952 jw_end_object(jw);
953}
954
955static void anal_dump_fn(AnalDumpCtx *ctx, ZigFn *fn) {
896956 JsonWriter *jw = &ctx->jw;
897957
898958 jw_begin_object(jw);
......@@ -918,6 +978,7 @@ void zig_print_analysis_dump(CodeGen *g, FILE *f, const char *one_indent, const
918978 ctx.decl_map.init(16);
919979 ctx.node_map.init(16);
920980 ctx.fn_map.init(16);
981 ctx.err_map.init(16);
921982
922983 jw_begin_object(jw);
923984
......@@ -1055,6 +1116,15 @@ void zig_print_analysis_dump(CodeGen *g, FILE *f, const char *one_indent, const
10551116 }
10561117 jw_end_array(jw);
10571118
1119 jw_object_field(jw, "errors");
1120 jw_begin_array(jw);
1121 for (uint32_t i = 0; i < ctx.err_list.length; i += 1) {
1122 const ErrorTableEntry *err = ctx.err_list.at(i);
1123 jw_array_elem(jw);
1124 anal_dump_err(&ctx, err);
1125 }
1126 jw_end_array(jw);
1127
10581128 jw_object_field(jw, "astNodes");
10591129 jw_begin_array(jw);
10601130 for (uint32_t i = 0; i < ctx.node_list.length; i += 1) {
src/ir.cpp+3-3
......@@ -9692,7 +9692,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
96929692 ZigType *container_set = wanted_type;
96939693
96949694 // if the container set is inferred, then this will always work.
9695 if (container_set->data.error_set.infer_fn != nullptr) {
9695 if (container_set->data.error_set.infer_fn != nullptr && container_set->data.error_set.incomplete) {
96969696 return result;
96979697 }
96989698 // if the container set is the global one, it will always work.
......@@ -16157,7 +16157,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1615716157 UndefOk);
1615816158
1615916159 if (inferred_err_set_type != nullptr) {
16160 inferred_err_set_type->data.error_set.infer_fn = nullptr;
16160 inferred_err_set_type->data.error_set.incomplete = false;
1616116161 if (result->type->id == ZigTypeIdErrorUnion) {
1616216162 ErrorTableEntry *err = result->data.x_err_union.error_set->data.x_err_set;
1616316163 if (err != nullptr) {
......@@ -23617,7 +23617,7 @@ static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruct
2361723617 if (!type_is_global_error_set(err_set_type) &&
2361823618 err_set_type->data.error_set.err_count == 0)
2361923619 {
23620 assert(err_set_type->data.error_set.infer_fn == nullptr);
23620 assert(!err_set_type->data.error_set.incomplete);
2362123621 return ir_const_bool(ira, &instruction->base, false);
2362223622 }
2362323623 }