Commit 0fa24b6b75

Andrew Kelley <superjoe30@gmail.com>
2018-07-17 01:26:15
allow implicit cast of undefined to optional
1 parent 9b56efc
Changed files (2)
src
test
cases
src/ir.cpp
@@ -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;
 
test/cases/cast.zig
@@ -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);
+        }
+    };
+}