mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
parent
f8fe517d12
commit
9c8dfadbb1
2 changed files with 15 additions and 2 deletions
|
|
@ -10838,8 +10838,11 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
|
||||||
{
|
{
|
||||||
ZigType *slice_ptr_type = wanted_type->data.structure.fields[slice_ptr_index].type_entry;
|
ZigType *slice_ptr_type = wanted_type->data.structure.fields[slice_ptr_index].type_entry;
|
||||||
assert(slice_ptr_type->id == ZigTypeIdPointer);
|
assert(slice_ptr_type->id == ZigTypeIdPointer);
|
||||||
if (types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type,
|
ZigType *array_type = actual_type->data.pointer.child_type;
|
||||||
actual_type->data.pointer.child_type->data.array.child_type, source_node,
|
bool const_ok = (slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0
|
||||||
|
|| !actual_type->data.pointer.is_const);
|
||||||
|
if (const_ok && types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type,
|
||||||
|
array_type->data.array.child_type, source_node,
|
||||||
!slice_ptr_type->data.pointer.is_const).id == ConstCastResultIdOk)
|
!slice_ptr_type->data.pointer.is_const).id == ConstCastResultIdOk)
|
||||||
{
|
{
|
||||||
return ir_resolve_ptr_of_array_to_slice(ira, source_instr, value, wanted_type);
|
return ir_resolve_ptr_of_array_to_slice(ira, source_instr, value, wanted_type);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,16 @@
|
||||||
const tests = @import("tests.zig");
|
const tests = @import("tests.zig");
|
||||||
|
|
||||||
pub fn addCases(cases: *tests.CompileErrorContext) void {
|
pub fn addCases(cases: *tests.CompileErrorContext) void {
|
||||||
|
cases.add(
|
||||||
|
"implicit cast const array to mutable slice",
|
||||||
|
\\export fn entry() void {
|
||||||
|
\\ const buffer: [1]u8 = []u8{8};
|
||||||
|
\\ const sliceA: []u8 = &buffer;
|
||||||
|
\\}
|
||||||
|
,
|
||||||
|
".tmp_source.zig:3:27: error: expected type '[]u8', found '*const [1]u8'",
|
||||||
|
);
|
||||||
|
|
||||||
cases.add(
|
cases.add(
|
||||||
"deref slice and get len field",
|
"deref slice and get len field",
|
||||||
\\export fn entry() void {
|
\\export fn entry() void {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue