zigtools/zls

Incorrect type inference when slicing a slice with comptime-known range

Opened this issue · 1 comments

Zig Version

0.11.0-dev.3322+82632aff2

Zig Language Server Version

0.11.0-dev.469+4f0762a

Steps to Reproduce

Using VS Code with the Zig extension, with the following code:

const my_slice: []const f32 = &.{1, 2, 3, 4};
std.debug.print("{d:.3}", .{ my_slice[0..2] });

Enter a dot after my_slice[0..2] to trigger auto-completion.

Expected Behavior

Since the range is comptime-known, the type of my_slice[0..2] is *[2]f32, so typing . after my_slice[0..2] should open an autocomplete window that includes *.

Actual Behavior

Autocompleted suggestions are len and ptr, but not *.

This can be further validated by assigning the subslice to a variable and inspecting its type (by hovering over it), which is reported as []const f32.

const my_subslice = my_slice[0..2];

ZLS is not capable of resolving comptime-known values so it assumes that values are runtime-known. A special case that would detect a number literal being used as the start and end index when slicing could be added but anything more complex would be harder to implement.