Commit 2a62d4b20b

LemonBoy <thatlemon@gmail.com>
2020-10-16 09:16:12
stage1: Expand undefined struct/arrays when indexed
Fixes #6693
1 parent 996a228
Changed files (2)
src
stage1
test
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,
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(){};
         \\}