authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-28 11:17:20-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-28 11:17:20-04:00
log7139eef4cfc7c75655f9951849a994b116e96abe
treeda10043f4783052423a2b394309d523a0f6840eb
parentac477f3c9a94b6d32a56d89a01ae24c68143ee5d
signaturelock-open Commit is signed but in an unrecognized format.

implement lazy values for error union types

closes #3129

4 files changed, 106 insertions(+), 21 deletions(-)

src/all_types.hpp+9
......@@ -312,6 +312,7 @@ enum LazyValueId {
312312 LazyValueIdOptType,
313313 LazyValueIdSliceType,
314314 LazyValueIdFnType,
315 LazyValueIdErrUnionType,
315316};
316317
317318struct LazyValue {
......@@ -372,6 +373,14 @@ struct LazyValueFnType {
372373 bool is_generic;
373374};
374375
376struct LazyValueErrUnionType {
377 LazyValue base;
378
379 IrAnalyze *ira;
380 IrInstruction *err_set_type;
381 IrInstruction *payload_type;
382};
383
375384struct ConstExprValue {
376385 ZigType *type;
377386 ConstValSpecial special;
src/analyze.cpp+38-5
......@@ -1015,6 +1015,7 @@ static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, Zi
10151015 }
10161016 case LazyValueIdOptType:
10171017 case LazyValueIdSliceType:
1018 case LazyValueIdErrUnionType:
10181019 *is_zero_bits = false;
10191020 return ErrorNone;
10201021 case LazyValueIdFnType: {
......@@ -1040,6 +1041,7 @@ Error type_val_resolve_is_opaque_type(CodeGen *g, ConstExprValue *type_val, bool
10401041 case LazyValueIdPtrType:
10411042 case LazyValueIdFnType:
10421043 case LazyValueIdOptType:
1044 case LazyValueIdErrUnionType:
10431045 *is_opaque_type = false;
10441046 return ErrorNone;
10451047 }
......@@ -1094,6 +1096,11 @@ static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue
10941096 }
10951097 return ReqCompTimeNo;
10961098 }
1099 case LazyValueIdErrUnionType: {
1100 LazyValueErrUnionType *lazy_err_union_type =
1101 reinterpret_cast<LazyValueErrUnionType *>(type_val->data.x_lazy);
1102 return type_val_resolve_requires_comptime(g, &lazy_err_union_type->payload_type->value);
1103 }
10971104 }
10981105 zig_unreachable();
10991106}
......@@ -1102,10 +1109,8 @@ static Error type_val_resolve_abi_size(CodeGen *g, AstNode *source_node, ConstEx
11021109 size_t *abi_size, size_t *size_in_bits)
11031110{
11041111 Error err;
1105 if (type_val->data.x_lazy->id == LazyValueIdOptType) {
1106 if ((err = ir_resolve_lazy(g, source_node, type_val)))
1107 return err;
1108 }
1112
1113start_over:
11091114 if (type_val->special != ConstValSpecialLazy) {
11101115 assert(type_val->special == ConstValSpecialStatic);
11111116 ZigType *ty = type_val->data.x_type;
......@@ -1129,7 +1134,10 @@ static Error type_val_resolve_abi_size(CodeGen *g, AstNode *source_node, ConstEx
11291134 *size_in_bits = g->builtin_types.entry_usize->size_in_bits;
11301135 return ErrorNone;
11311136 case LazyValueIdOptType:
1132 zig_unreachable();
1137 case LazyValueIdErrUnionType:
1138 if ((err = ir_resolve_lazy(g, source_node, type_val)))
1139 return err;
1140 goto start_over;
11331141 }
11341142 zig_unreachable();
11351143}
......@@ -1161,6 +1169,19 @@ Error type_val_resolve_abi_align(CodeGen *g, ConstExprValue *type_val, uint32_t
11611169 LazyValueOptType *lazy_opt_type = reinterpret_cast<LazyValueOptType *>(type_val->data.x_lazy);
11621170 return type_val_resolve_abi_align(g, &lazy_opt_type->payload_type->value, abi_align);
11631171 }
1172 case LazyValueIdErrUnionType: {
1173 LazyValueErrUnionType *lazy_err_union_type =
1174 reinterpret_cast<LazyValueErrUnionType *>(type_val->data.x_lazy);
1175 uint32_t payload_abi_align;
1176 if ((err = type_val_resolve_abi_align(g, &lazy_err_union_type->payload_type->value,
1177 &payload_abi_align)))
1178 {
1179 return err;
1180 }
1181 *abi_align = (payload_abi_align > g->err_tag_type->abi_align) ?
1182 payload_abi_align : g->err_tag_type->abi_align;
1183 return ErrorNone;
1184 }
11641185 }
11651186 zig_unreachable();
11661187}
......@@ -1189,6 +1210,18 @@ static OnePossibleValue type_val_resolve_has_one_possible_value(CodeGen *g, Cons
11891210 return OnePossibleValueNo;
11901211 }
11911212 }
1213 case LazyValueIdErrUnionType: {
1214 LazyValueErrUnionType *lazy_err_union_type =
1215 reinterpret_cast<LazyValueErrUnionType *>(type_val->data.x_lazy);
1216 switch (type_val_resolve_has_one_possible_value(g, &lazy_err_union_type->err_set_type->value)) {
1217 case OnePossibleValueInvalid:
1218 return OnePossibleValueInvalid;
1219 case OnePossibleValueNo:
1220 return OnePossibleValueNo;
1221 case OnePossibleValueYes:
1222 return type_val_resolve_has_one_possible_value(g, &lazy_err_union_type->payload_type->value);
1223 }
1224 }
11921225 }
11931226 zig_unreachable();
11941227}
src/ir.cpp+39-16
......@@ -14626,28 +14626,23 @@ static IrInstruction *ir_analyze_instruction_error_return_trace(IrAnalyze *ira,
1462614626static IrInstruction *ir_analyze_instruction_error_union(IrAnalyze *ira,
1462714627 IrInstructionErrorUnion *instruction)
1462814628{
14629 Error err;
14629 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
14630 result->value.special = ConstValSpecialLazy;
1463014631
14631 ZigType *err_set_type = ir_resolve_type(ira, instruction->err_set->child);
14632 if (type_is_invalid(err_set_type))
14633 return ira->codegen->invalid_instruction;
14632 LazyValueErrUnionType *lazy_err_union_type = allocate<LazyValueErrUnionType>(1);
14633 lazy_err_union_type->ira = ira;
14634 result->value.data.x_lazy = &lazy_err_union_type->base;
14635 lazy_err_union_type->base.id = LazyValueIdErrUnionType;
1463414636
14635 ZigType *payload_type = ir_resolve_type(ira, instruction->payload->child);
14636 if (type_is_invalid(payload_type))
14637 lazy_err_union_type->err_set_type = instruction->err_set->child;
14638 if (ir_resolve_type_lazy(ira, lazy_err_union_type->err_set_type) == nullptr)
1463714639 return ira->codegen->invalid_instruction;
1463814640
14639 if (err_set_type->id != ZigTypeIdErrorSet) {
14640 ir_add_error(ira, instruction->err_set->child,
14641 buf_sprintf("expected error set type, found type '%s'",
14642 buf_ptr(&err_set_type->name)));
14641 lazy_err_union_type->payload_type = instruction->payload->child;
14642 if (ir_resolve_type_lazy(ira, lazy_err_union_type->payload_type) == nullptr)
1464314643 return ira->codegen->invalid_instruction;
14644 }
1464514644
14646 if ((err = type_resolve(ira->codegen, payload_type, ResolveStatusSizeKnown)))
14647 return ira->codegen->invalid_instruction;
14648 ZigType *result_type = get_error_union_type(ira->codegen, err_set_type, payload_type);
14649
14650 return ir_const_type(ira, &instruction->base, result_type);
14645 return result;
1465114646}
1465214647
1465314648static IrInstruction *ir_analyze_alloca(IrAnalyze *ira, IrInstruction *source_inst, ZigType *var_type,
......@@ -25698,6 +25693,34 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
2569825693 val->data.x_type = fn_type;
2569925694 return ErrorNone;
2570025695 }
25696 case LazyValueIdErrUnionType: {
25697 LazyValueErrUnionType *lazy_err_union_type =
25698 reinterpret_cast<LazyValueErrUnionType *>(val->data.x_lazy);
25699 IrAnalyze *ira = lazy_err_union_type->ira;
25700
25701 ZigType *err_set_type = ir_resolve_type(ira, lazy_err_union_type->err_set_type);
25702 if (type_is_invalid(err_set_type))
25703 return ErrorSemanticAnalyzeFail;
25704
25705 ZigType *payload_type = ir_resolve_type(ira, lazy_err_union_type->payload_type);
25706 if (type_is_invalid(payload_type))
25707 return ErrorSemanticAnalyzeFail;
25708
25709 if (err_set_type->id != ZigTypeIdErrorSet) {
25710 ir_add_error(ira, lazy_err_union_type->err_set_type,
25711 buf_sprintf("expected error set type, found type '%s'",
25712 buf_ptr(&err_set_type->name)));
25713 return ErrorSemanticAnalyzeFail;
25714 }
25715
25716 if ((err = type_resolve(ira->codegen, payload_type, ResolveStatusSizeKnown)))
25717 return ErrorSemanticAnalyzeFail;
25718
25719 assert(val->type->id == ZigTypeIdMetaType);
25720 val->data.x_type = get_error_union_type(ira->codegen, err_set_type, payload_type);
25721 val->special = ConstValSpecialStatic;
25722 return ErrorNone;
25723 }
2570125724 }
2570225725 zig_unreachable();
2570325726}
test/stage1/behavior/error.zig+20
......@@ -375,3 +375,23 @@ test "implicit cast to optional to error union to return result loc" {
375375 S.entry();
376376 //comptime S.entry(); TODO
377377}
378
379test "function pointer with return type that is error union with payload which is pointer of parent struct" {
380 const S = struct {
381 const Foo = struct {
382 fun: fn (a: i32) (anyerror!*Foo),
383 };
384
385 const Err = error{UnspecifiedErr};
386
387 fn bar(a: i32) anyerror!*Foo {
388 return Err.UnspecifiedErr;
389 }
390
391 fn doTheTest() void {
392 var x = Foo{ .fun = bar };
393 expectError(error.UnspecifiedErr, x.fun(1));
394 }
395 };
396 S.doTheTest();
397}