authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-25 12:03:39-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-25 12:03:39-04:00
log2e562a5f366aabefd34a4191b2cd0b5e72d3d089
tree96192ee77cac902e9954d534a5009c4c04e28855
parent839492d0e82e5d7b2a5cee54acb9e64b414e9817
signaturelock-open Commit is signed but in an unrecognized format.

fix crash on runtime index into slice of comptime type

closes #1435

3 files changed, 27 insertions(+), 2 deletions(-)

src/analyze.cpp+5-2
...@@ -1187,8 +1187,11 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -1187,8 +1187,11 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1187 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))1187 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
1188 return g->builtin_types.entry_invalid;1188 return g->builtin_types.entry_invalid;
11891189
1190 if (is_c_abi)1190 if (is_c_abi) {
1191 if ((err = type_resolve(g, type_entry, ResolveStatusSizeKnown)))
1192 return g->builtin_types.entry_invalid;
1191 continue;1193 continue;
1194 }
11921195
1193 if (type_has_bits(type_entry)) {1196 if (type_has_bits(type_entry)) {
1194 ZigType *gen_type;1197 ZigType *gen_type;
...@@ -4464,7 +4467,7 @@ bool handle_is_ptr(ZigType *type_entry) {...@@ -4464,7 +4467,7 @@ bool handle_is_ptr(ZigType *type_entry) {
4464 return type_has_bits(type_entry->data.maybe.child_type) &&4467 return type_has_bits(type_entry->data.maybe.child_type) &&
4465 !type_is_codegen_pointer(type_entry->data.maybe.child_type);4468 !type_is_codegen_pointer(type_entry->data.maybe.child_type);
4466 case ZigTypeIdUnion:4469 case ZigTypeIdUnion:
4467 assert(type_entry->data.unionation.complete);4470 assert(type_entry->data.unionation.zero_bits_known);
4468 if (type_entry->data.unionation.gen_field_count == 0)4471 if (type_entry->data.unionation.gen_field_count == 0)
4469 return false;4472 return false;
4470 if (!type_has_bits(type_entry))4473 if (!type_has_bits(type_entry))
src/ir.cpp+6
...@@ -14640,6 +14640,12 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle...@@ -14640,6 +14640,12 @@ static ZigType *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstructionEle
1464014640
14641 } else {14641 } else {
14642 // runtime known element index14642 // runtime known element index
14643 if (type_requires_comptime(return_type)) {
14644 ir_add_error(ira, elem_index,
14645 buf_sprintf("values of type '%s' must be comptime known, but index value is runtime known",
14646 buf_ptr(&return_type->data.pointer.child_type->name)));
14647 return ira->codegen->builtin_types.entry_invalid;
14648 }
14643 if (ptr_align < abi_align) {14649 if (ptr_align < abi_align) {
14644 if (elem_size >= ptr_align && elem_size % ptr_align == 0) {14650 if (elem_size >= ptr_align && elem_size % ptr_align == 0) {
14645 return_type = adjust_ptr_align(ira->codegen, return_type, ptr_align);14651 return_type = adjust_ptr_align(ira->codegen, return_type, ptr_align);
test/compile_errors.zig+16
...@@ -1,6 +1,22 @@...@@ -1,6 +1,22 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompileErrorContext) void {3pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "runtime index into comptime type slice",
6 \\const Struct = struct {
7 \\ a: u32,
8 \\};
9 \\fn getIndex() usize {
10 \\ return 2;
11 \\}
12 \\export fn entry() void {
13 \\ const index = getIndex();
14 \\ const field = @typeInfo(Struct).Struct.fields[index];
15 \\}
16 ,
17 ".tmp_source.zig:9:51: error: values of type 'StructField' must be comptime known, but index value is runtime known",
18 );
19
4 cases.add(20 cases.add(
5 "compile log statement inside function which must be comptime evaluated",21 "compile log statement inside function which must be comptime evaluated",
6 \\fn Foo(comptime T: type) type {22 \\fn Foo(comptime T: type) type {