authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-07 13:03:43-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-07 13:03:43-05:00
logedebe0586bd0cb73ef8492fd569e1c2fc7aca05b
tree18cc72b27a329a10331ceea1777b3df4a51e28bd
parentecb77af5343c58bbd8023ded0ebf241abe1b0dfa
signaturelock-open Commit is signed but in an unrecognized format.

remove compile error for peer result comptime_int and null

closes #2763

2 files changed, 22 insertions(+), 7 deletions(-)

src/ir.cpp+1-7
...@@ -11438,13 +11438,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -11438,13 +11438,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
11438 }11438 }
11439 }11439 }
11440 } else if (any_are_null && prev_inst->value->type->id != ZigTypeIdNull) {11440 } else if (any_are_null && prev_inst->value->type->id != ZigTypeIdNull) {
11441 if (prev_inst->value->type->id == ZigTypeIdComptimeInt ||11441 if (prev_inst->value->type->id == ZigTypeIdOptional) {
11442 prev_inst->value->type->id == ZigTypeIdComptimeFloat)
11443 {
11444 ir_add_error_node(ira, source_node,
11445 buf_sprintf("unable to make maybe out of number literal"));
11446 return ira->codegen->builtin_types.entry_invalid;
11447 } else if (prev_inst->value->type->id == ZigTypeIdOptional) {
11448 return prev_inst->value->type;11442 return prev_inst->value->type;
11449 } else {11443 } else {
11450 if ((err = type_resolve(ira->codegen, prev_inst->value->type, ResolveStatusSizeKnown)))11444 if ((err = type_resolve(ira->codegen, prev_inst->value->type, ResolveStatusSizeKnown)))
test/stage1/behavior/cast.zig+21
...@@ -710,3 +710,24 @@ test "return u8 coercing into ?u32 return type" {...@@ -710,3 +710,24 @@ test "return u8 coercing into ?u32 return type" {
710 S.doTheTest();710 S.doTheTest();
711 comptime S.doTheTest();711 comptime S.doTheTest();
712}712}
713
714test "peer result null and comptime_int" {
715 const S = struct {
716 fn blah(n: i32) ?i32 {
717 if (n == 0) {
718 return null;
719 } else if (n < 0) {
720 return -1;
721 } else {
722 return 1;
723 }
724 }
725 };
726
727 expect(S.blah(0) == null);
728 comptime expect(S.blah(0) == null);
729 expect(S.blah(10).? == 1);
730 comptime expect(S.blah(10).? == 1);
731 expect(S.blah(-10).? == -1);
732 comptime expect(S.blah(-10).? == -1);
733}