| author | |
| committer | |
| log | 8ae4ffa49357a4939985884285fef39e86f5f705 |
| tree | 76d0f2c8e9107a19717e7879620302fe2193a02d |
| parent | 16f16b9c17383a9a075ba3067d6b0ef8c8c0df11 |
closes #3792 files changed, 23 insertions(+), 3 deletions(-)
src/ir.cpp+2-3| ... | ... | @@ -11119,10 +11119,9 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira, |
| 11119 | 11119 | ConstExprValue *val = ir_resolve_const(ira, value, UndefBad); |
| 11120 | 11120 | if (!val) |
| 11121 | 11121 | return ira->codegen->builtin_types.entry_invalid; |
| 11122 | assert(val->data.x_ptr.special == ConstPtrSpecialRef); | |
| 11123 | ConstExprValue *maybe_val = val->data.x_ptr.data.ref.pointee; | |
| 11122 | ConstExprValue *maybe_val = const_ptr_pointee(ira->codegen, val); | |
| 11124 | 11123 | |
| 11125 | if (maybe_val->special != ConstValSpecialRuntime) { | |
| 11124 | if (val->data.x_ptr.mut != ConstPtrMutRuntimeVar) { | |
| 11126 | 11125 | if (!maybe_val->data.x_maybe) { |
| 11127 | 11126 | ir_add_error(ira, &unwrap_maybe_instruction->base, buf_sprintf("unable to unwrap null")); |
| 11128 | 11127 | return ira->codegen->builtin_types.entry_invalid; |
test/cases/null.zig+21| ... | ... | @@ -122,3 +122,24 @@ fn bar(x: ?void) -> ?void { |
| 122 | 122 | return null; |
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | ||
| 126 | ||
| 127 | ||
| 128 | const StructWithNullable = struct { | |
| 129 | field: ?i32, | |
| 130 | }; | |
| 131 | ||
| 132 | var struct_with_nullable: StructWithNullable = undefined; | |
| 133 | ||
| 134 | test "unwrap nullable which is field of global var" { | |
| 135 | struct_with_nullable.field = null; | |
| 136 | if (struct_with_nullable.field) |payload| { | |
| 137 | unreachable; | |
| 138 | } | |
| 139 | struct_with_nullable.field = 1234; | |
| 140 | if (struct_with_nullable.field) |payload| { | |
| 141 | assert(payload == 1234); | |
| 142 | } else { | |
| 143 | unreachable; | |
| 144 | } | |
| 145 | } |