allow implicit cast from *[N]T to ?[*]T (#1398)

* allow implicit cast from *[N]T to ?[*]T
This commit is contained in:
Raul Leal 2018-08-22 18:12:08 +01:00 committed by Andrew Kelley
parent 3d780cf2ef
commit 87b10400c2
2 changed files with 24 additions and 0 deletions

View File

@ -10697,6 +10697,19 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
return ira->codegen->invalid_instruction;
return cast2;
} else if (
wanted_child_type->id == TypeTableEntryIdPointer &&
wanted_child_type->data.pointer.ptr_len == PtrLenUnknown &&
actual_type->id == TypeTableEntryIdPointer &&
actual_type->data.pointer.ptr_len == PtrLenSingle &&
actual_type->data.pointer.child_type->id == TypeTableEntryIdArray &&
actual_type->data.pointer.alignment >= wanted_child_type->data.pointer.alignment &&
types_match_const_cast_only(ira, wanted_child_type->data.pointer.child_type,
actual_type->data.pointer.child_type->data.array.child_type, source_node,
!wanted_child_type->data.pointer.is_const).id == ConstCastResultIdOk)
{
IrInstruction *cast1 = ir_resolve_ptr_of_array_to_slice(ira, source_instr, value, wanted_child_type);
return ir_analyze_maybe_wrap(ira, source_instr, cast1, wanted_type);
}
}

View File

@ -485,3 +485,14 @@ fn MakeType(comptime T: type) type {
}
};
}
test "implicit cast from *[N]T to ?[*]T" {
var x: ?[*]u16 = null;
var y: [4]u16 = [4]u16 {0, 1, 2, 3};
x = &y;
assert(std.mem.eql(u16, x.?[0..4], y[0..4]));
x.?[0] = 8;
y[3] = 6;
assert(std.mem.eql(u16, x.?[0..4], y[0..4]));
}