Skip to content

Commit

Permalink
fix assertion when taking slice of zero-length array
Browse files Browse the repository at this point in the history
closes #788
  • Loading branch information
andrewrk committed Mar 6, 2018
1 parent cc0f660 commit 44ae891
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/ir.cpp
Expand Up @@ -15821,9 +15821,13 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
TypeTableEntry *return_type;

if (array_type->id == TypeTableEntryIdArray) {
uint32_t byte_alignment = ptr_type->data.pointer.alignment;
if (array_type->data.array.len == 0 && byte_alignment == 0) {
byte_alignment = get_abi_alignment(ira->codegen, array_type->data.array.child_type);
}
TypeTableEntry *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.array.child_type,
ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
ptr_type->data.pointer.alignment, 0, 0);
byte_alignment, 0, 0);
return_type = get_slice_type(ira->codegen, slice_ptr_type);
} else if (array_type->id == TypeTableEntryIdPointer) {
TypeTableEntry *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, array_type->data.pointer.child_type,
Expand Down
10 changes: 10 additions & 0 deletions test/cases/misc.zig
Expand Up @@ -650,3 +650,13 @@ test "packed struct, enum, union parameters in extern function" {

export fn testPackedStuff(a: &const PackedStruct, b: &const PackedUnion, c: PackedEnum) void {
}


test "slicing zero length array" {
const s1 = ""[0..];
const s2 = ([]u32{})[0..];
assert(s1.len == 0);
assert(s2.len == 0);
assert(mem.eql(u8, s1, ""));
assert(mem.eql(u32, s2, []u32{}));
}

0 comments on commit 44ae891

Please sign in to comment.