Skip to content

Commit

Permalink
allow implicit cast of undefined to optional
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Jul 16, 2018
1 parent 9b56efc commit 0fa24b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ir.cpp
Expand Up @@ -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;

Expand Down
17 changes: 17 additions & 0 deletions test/cases/cast.zig
Expand Up @@ -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);
}
};
}

0 comments on commit 0fa24b6

Please sign in to comment.