From 8ae4ffa49357a4939985884285fef39e86f5f705 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 29 May 2017 14:08:39 -0400 Subject: [PATCH] fix crash when unwrapping optional field of global variable closes #379 --- src/ir.cpp | 5 ++--- test/cases/null.zig | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 8e1a974359b64bc15b9b882fdc9321f6960145b9..669de6e28144bee8bd932a3f6eab13a6c5389122 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11119,10 +11119,9 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira, ConstExprValue *val = ir_resolve_const(ira, value, UndefBad); if (!val) return ira->codegen->builtin_types.entry_invalid; - assert(val->data.x_ptr.special == ConstPtrSpecialRef); - ConstExprValue *maybe_val = val->data.x_ptr.data.ref.pointee; + ConstExprValue *maybe_val = const_ptr_pointee(ira->codegen, val); - if (maybe_val->special != ConstValSpecialRuntime) { + if (val->data.x_ptr.mut != ConstPtrMutRuntimeVar) { if (!maybe_val->data.x_maybe) { ir_add_error(ira, &unwrap_maybe_instruction->base, buf_sprintf("unable to unwrap null")); return ira->codegen->builtin_types.entry_invalid; diff --git a/test/cases/null.zig b/test/cases/null.zig index 2b3b9aa13ea887629eaf77064b730b915e3c50fb..484e3a076c29098c0392f7134aeecbb41a17e149 100644 --- a/test/cases/null.zig +++ b/test/cases/null.zig @@ -122,3 +122,24 @@ fn bar(x: ?void) -> ?void { return null; } } + + + +const StructWithNullable = struct { + field: ?i32, +}; + +var struct_with_nullable: StructWithNullable = undefined; + +test "unwrap nullable which is field of global var" { + struct_with_nullable.field = null; + if (struct_with_nullable.field) |payload| { + unreachable; + } + struct_with_nullable.field = 1234; + if (struct_with_nullable.field) |payload| { + assert(payload == 1234); + } else { + unreachable; + } +} -- 2.54.0