Skip to content

Commit edebe05

Browse files
committed
remove compile error for peer result comptime_int and null
closes #2763
1 parent ecb77af commit edebe05

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/ir.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11438,13 +11438,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1143811438
}
1143911439
}
1144011440
} else if (any_are_null && prev_inst->value->type->id != ZigTypeIdNull) {
11441-
if (prev_inst->value->type->id == ZigTypeIdComptimeInt ||
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) {
11441+
if (prev_inst->value->type->id == ZigTypeIdOptional) {
1144811442
return prev_inst->value->type;
1144911443
} else {
1145011444
if ((err = type_resolve(ira->codegen, prev_inst->value->type, ResolveStatusSizeKnown)))

test/stage1/behavior/cast.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,3 +710,24 @@ test "return u8 coercing into ?u32 return type" {
710710
S.doTheTest();
711711
comptime S.doTheTest();
712712
}
713+
714+
test "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+
}

0 commit comments

Comments
 (0)