Commit cbc85f4516

Cody Tapscott <topolarity@tapscott.me>
2022-07-07 20:21:39
stage1: Fix seg-fault when slicing string literal with sentinel
1 parent 75c33ba
Changed files (3)
src
stage1
test
behavior
src/stage1/ir.cpp
@@ -21575,6 +21575,7 @@ done_with_return_type:
                     // handle `[N]T`
                     target_len = target->type->data.array.len;
                     target_sentinel = target->type->data.array.sentinel;
+                    expand_undef_array(ira->codegen, target);
                     target_elements = target->data.x_array.data.s_none.elements;
                     break;
                 } else if (target->type->id == ZigTypeIdPointer && target->type->data.pointer.child_type->id == ZigTypeIdArray) {
test/behavior/bugs/12033.zig
@@ -0,0 +1,12 @@
+const std = @import("std");
+
+test {
+    const string = "Hello!\x00World!";
+    try std.testing.expect(@TypeOf(string) == *const [13:0]u8);
+
+    const slice_without_sentinel: []const u8 = string[0..6];
+    try std.testing.expect(@TypeOf(slice_without_sentinel) == []const u8);
+
+    const slice_with_sentinel: [:0]const u8 = string[0..6 :0];
+    try std.testing.expect(@TypeOf(slice_with_sentinel) == [:0]const u8);
+}
test/behavior.zig
@@ -83,6 +83,7 @@ test {
     _ = @import("behavior/bugs/11181.zig");
     _ = @import("behavior/bugs/11213.zig");
     _ = @import("behavior/bugs/12003.zig");
+    _ = @import("behavior/bugs/12033.zig");
     _ = @import("behavior/byteswap.zig");
     _ = @import("behavior/byval_arg_var.zig");
     _ = @import("behavior/call.zig");