mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
fix crash when unwrapping optional field of global variable
closes #379
This commit is contained in:
parent
16f16b9c17
commit
8ae4ffa493
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user