Skip to content

Commit 4ad4cd2

Browse files
committedJul 6, 2018
fix iterating over a void slice
closes #1203
1 parent 1cf7511 commit 4ad4cd2

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed
 

‎src/codegen.cpp‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,18 +2992,26 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
29922992
return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, "");
29932993
} else if (array_type->id == TypeTableEntryIdStruct) {
29942994
assert(array_type->data.structure.is_slice);
2995+
if (!type_has_bits(instruction->base.value.type)) {
2996+
if (safety_check_on) {
2997+
assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMIntegerTypeKind);
2998+
add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, array_ptr);
2999+
}
3000+
return nullptr;
3001+
}
3002+
29953003
assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);
29963004
assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind);
29973005

29983006
if (safety_check_on) {
2999-
size_t len_index = array_type->data.structure.fields[1].gen_index;
3007+
size_t len_index = array_type->data.structure.fields[slice_len_index].gen_index;
30003008
assert(len_index != SIZE_MAX);
30013009
LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, array_ptr, (unsigned)len_index, "");
30023010
LLVMValueRef len = gen_load_untyped(g, len_ptr, 0, false, "");
30033011
add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, len);
30043012
}
30053013

3006-
size_t ptr_index = array_type->data.structure.fields[0].gen_index;
3014+
size_t ptr_index = array_type->data.structure.fields[slice_ptr_index].gen_index;
30073015
assert(ptr_index != SIZE_MAX);
30083016
LLVMValueRef ptr_ptr = LLVMBuildStructGEP(g->builder, array_ptr, (unsigned)ptr_index, "");
30093017
LLVMValueRef ptr = gen_load_untyped(g, ptr_ptr, 0, false, "");
@@ -3983,11 +3991,15 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
39833991
add_bounds_check(g, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);
39843992
}
39853993

3986-
LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
3987-
LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, &start_val, 1, "");
3988-
gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
3994+
if (type_has_bits(array_type)) {
3995+
size_t gen_ptr_index = instruction->base.value.type->data.structure.fields[slice_ptr_index].gen_index;
3996+
LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_ptr_index, "");
3997+
LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, &start_val, 1, "");
3998+
gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
3999+
}
39894000

3990-
LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_len_index, "");
4001+
size_t gen_len_index = instruction->base.value.type->data.structure.fields[slice_len_index].gen_index;
4002+
LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_len_index, "");
39914003
LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
39924004
gen_store_untyped(g, len_value, len_field_ptr, 0, false);
39934005

‎test/cases/void.zig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ test "compare void with void compile time known" {
1616
assert(foo.a == {});
1717
}
1818
}
19+
20+
test "iterate over a void slice" {
21+
var j: usize = 0;
22+
for (times(10)) |_, i| {
23+
assert(i == j);
24+
j += 1;
25+
}
26+
}
27+
28+
fn times(n: usize) []const void {
29+
return ([*]void)(undefined)[0..n];
30+
}

0 commit comments

Comments
 (0)