From edebe0586bd0cb73ef8492fd569e1c2fc7aca05b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 7 Dec 2019 13:03:43 -0500 Subject: [PATCH] remove compile error for peer result comptime_int and null closes #2763 --- src/ir.cpp | 8 +------- test/stage1/behavior/cast.zig | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 27f69a24da..abf315b797 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11438,13 +11438,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT } } } else if (any_are_null && prev_inst->value->type->id != ZigTypeIdNull) { - if (prev_inst->value->type->id == ZigTypeIdComptimeInt || - prev_inst->value->type->id == ZigTypeIdComptimeFloat) - { - ir_add_error_node(ira, source_node, - buf_sprintf("unable to make maybe out of number literal")); - return ira->codegen->builtin_types.entry_invalid; - } else if (prev_inst->value->type->id == ZigTypeIdOptional) { + if (prev_inst->value->type->id == ZigTypeIdOptional) { return prev_inst->value->type; } else { if ((err = type_resolve(ira->codegen, prev_inst->value->type, ResolveStatusSizeKnown))) diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig index 6540166cc7..02a671f9fa 100644 --- a/test/stage1/behavior/cast.zig +++ b/test/stage1/behavior/cast.zig @@ -710,3 +710,24 @@ test "return u8 coercing into ?u32 return type" { S.doTheTest(); comptime S.doTheTest(); } + +test "peer result null and comptime_int" { + const S = struct { + fn blah(n: i32) ?i32 { + if (n == 0) { + return null; + } else if (n < 0) { + return -1; + } else { + return 1; + } + } + }; + + expect(S.blah(0) == null); + comptime expect(S.blah(0) == null); + expect(S.blah(10).? == 1); + comptime expect(S.blah(10).? == 1); + expect(S.blah(-10).? == -1); + comptime expect(S.blah(-10).? == -1); +}