From 2a62d4b20be9b99f53367e74434e1971ced78848 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 16 Oct 2020 09:16:12 +0200 Subject: [PATCH] stage1: Expand undefined struct/arrays when indexed Fixes #6693 --- src/stage1/ir.cpp | 4 ++++ test/compile_errors.zig | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 62c52c8a97..dc74fb9c14 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -22188,6 +22188,8 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP } return result; } else if (is_slice(array_type)) { + expand_undef_struct(ira->codegen, array_ptr_val); + ZigValue *ptr_field = array_ptr_val->data.x_struct.fields[slice_ptr_index]; ir_assert(ptr_field != nullptr, &elem_ptr_instruction->base.base); if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) { @@ -22252,6 +22254,8 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP } return result; } else if (array_type->id == ZigTypeIdArray || array_type->id == ZigTypeIdVector) { + expand_undef_array(ira->codegen, array_ptr_val); + IrInstGen *result; if (orig_array_ptr_val->data.x_ptr.mut == ConstPtrMutInfer) { result = ir_build_elem_ptr_gen(ira, elem_ptr_instruction->base.base.scope, diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 44ee58f002..062a357a93 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,15 @@ const tests = @import("tests.zig"); const std = @import("std"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add("indexing a undefined slice at comptime", + \\comptime { + \\ var slice: []u8 = undefined; + \\ slice[0] = 2; + \\} + , &[_][]const u8{ + "tmp.zig:3:10: error: index 0 outside slice of size 0", + }); + cases.add("array in c exported function", \\export fn zig_array(x: [10]u8) void { \\ expect(std.mem.eql(u8, &x, "1234567890")); @@ -7714,7 +7723,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { }); cases.add( // fixed bug #2032 - "compile diagnostic string for top level decl type", + "compile diagnostic string for top level decl type", \\export fn entry() void { \\ var foo: u32 = @This(){}; \\}