allow implicit cast of undefined to optional

This commit is contained in:
Andrew Kelley 2018-07-16 19:26:15 -04:00
parent 9b56efc957
commit 0fa24b6b75
2 changed files with 18 additions and 1 deletions

View File

@ -9408,7 +9408,7 @@ static IrInstruction *ir_analyze_maybe_wrap(IrAnalyze *ira, IrInstruction *sourc
if (type_is_invalid(casted_payload->value.type))
return ira->codegen->invalid_instruction;
ConstExprValue *val = ir_resolve_const(ira, casted_payload, UndefBad);
ConstExprValue *val = ir_resolve_const(ira, casted_payload, UndefOk);
if (!val)
return ira->codegen->invalid_instruction;

View File

@ -468,3 +468,20 @@ test "@intCast i32 to u7" {
var z = x >> @intCast(u7, y);
assert(z == 0xff);
}
test "implicit cast undefined to optional" {
assert(MakeType(void).getNull() == null);
assert(MakeType(void).getNonNull() != null);
}
fn MakeType(comptime T: type) type {
return struct {
fn getNull() ?T {
return null;
}
fn getNonNull() ?T {
return T(undefined);
}
};
}