From 1d0b729f28dd5f9341c4f4fe8ab4b25592e6834a Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 24 Aug 2022 20:29:13 +0300 Subject: [PATCH] Sema: fix crash on slice of non-array type Closes #12621 --- src/Sema.zig | 3 ++- test/cases/compile_errors/slice_of_non_array_type.zig | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/cases/compile_errors/slice_of_non_array_type.zig diff --git a/src/Sema.zig b/src/Sema.zig index 846b6af3bd..4e1b885486 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -26139,11 +26139,12 @@ fn analyzeSlice( var array_ty = ptr_ptr_child_ty; var slice_ty = ptr_ptr_ty; var ptr_or_slice = ptr_ptr; - var elem_ty = ptr_ptr_child_ty.childType(); + var elem_ty: Type = undefined; var ptr_sentinel: ?Value = null; switch (ptr_ptr_child_ty.zigTypeTag()) { .Array => { ptr_sentinel = ptr_ptr_child_ty.sentinel(); + elem_ty = ptr_ptr_child_ty.childType(); }, .Pointer => switch (ptr_ptr_child_ty.ptrSize()) { .One => { diff --git a/test/cases/compile_errors/slice_of_non_array_type.zig b/test/cases/compile_errors/slice_of_non_array_type.zig new file mode 100644 index 0000000000..734b026038 --- /dev/null +++ b/test/cases/compile_errors/slice_of_non_array_type.zig @@ -0,0 +1,9 @@ +comptime { + _ = 1[0..]; +} + +// error +// backend=stage2 +// target=native +// +// :2:10: error: slice of non-array type 'comptime_int'