Skip to content

Commit 0fa24b6

Browse files
committedJul 16, 2018
allow implicit cast of undefined to optional
·
0.15.10.3.0
1 parent 9b56efc commit 0fa24b6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
 

‎src/ir.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9408,7 +9408,7 @@ static IrInstruction *ir_analyze_maybe_wrap(IrAnalyze *ira, IrInstruction *sourc
94089408
if (type_is_invalid(casted_payload->value.type))
94099409
return ira->codegen->invalid_instruction;
94109410

9411-
ConstExprValue *val = ir_resolve_const(ira, casted_payload, UndefBad);
9411+
ConstExprValue *val = ir_resolve_const(ira, casted_payload, UndefOk);
94129412
if (!val)
94139413
return ira->codegen->invalid_instruction;
94149414

‎test/cases/cast.zig‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,3 +468,20 @@ test "@intCast i32 to u7" {
468468
var z = x >> @intCast(u7, y);
469469
assert(z == 0xff);
470470
}
471+
472+
test "implicit cast undefined to optional" {
473+
assert(MakeType(void).getNull() == null);
474+
assert(MakeType(void).getNonNull() != null);
475+
}
476+
477+
fn MakeType(comptime T: type) type {
478+
return struct {
479+
fn getNull() ?T {
480+
return null;
481+
}
482+
483+
fn getNonNull() ?T {
484+
return T(undefined);
485+
}
486+
};
487+
}

0 commit comments

Comments
 (0)
Please sign in to comment.