authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-29 14:08:39-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-29 14:08:39-04:00
log8ae4ffa49357a4939985884285fef39e86f5f705
tree76d0f2c8e9107a19717e7879620302fe2193a02d
parent16f16b9c17383a9a075ba3067d6b0ef8c8c0df11

fix crash when unwrapping optional field of global variable

closes #379

2 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,10 +11119,9 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
11119 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);11119 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
11120 if (!val)11120 if (!val)
11121 return ira->codegen->builtin_types.entry_invalid;11121 return ira->codegen->builtin_types.entry_invalid;
11122 assert(val->data.x_ptr.special == ConstPtrSpecialRef);11122 ConstExprValue *maybe_val = const_ptr_pointee(ira->codegen, val);
11123 ConstExprValue *maybe_val = val->data.x_ptr.data.ref.pointee;
1112411123
11125 if (maybe_val->special != ConstValSpecialRuntime) {11124 if (val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
11126 if (!maybe_val->data.x_maybe) {11125 if (!maybe_val->data.x_maybe) {
11127 ir_add_error(ira, &unwrap_maybe_instruction->base, buf_sprintf("unable to unwrap null"));11126 ir_add_error(ira, &unwrap_maybe_instruction->base, buf_sprintf("unable to unwrap null"));
11128 return ira->codegen->builtin_types.entry_invalid;11127 return ira->codegen->builtin_types.entry_invalid;
test/cases/null.zig+21
...@@ -122,3 +122,24 @@ fn bar(x: ?void) -> ?void {...@@ -122,3 +122,24 @@ fn bar(x: ?void) -> ?void {
122 return null;122 return null;
123 }123 }
124}124}
125
126
127
128const StructWithNullable = struct {
129 field: ?i32,
130};
131
132var struct_with_nullable: StructWithNullable = undefined;
133
134test "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}