Skip to content

zls can give incorrect type hints with slices #2534

@TheDWPCer

Description

@TheDWPCer

Zig Version

0.15.2

ZLS Version

0.15.1

Client / Code Editor / Extensions

Zed and VSCode

Steps to Reproduce and Observed Behavior

(Looking back on this, this is probably more of an enhancement)
The interesting thing with almost of these type hints, is, that zls picks a type that a general slice can be coerced to.

Example 1:

const the_string2 = try std.fmt.allocPrintSentinel(alloc, "{s}", .{the_string}, 0); //zls thinks this is []u8
std.debug.print("type: {any}\n", .{@TypeOf(the_string2)}); //however it is [:0]u8

Example 2:

const attempt2 = the_string2[0..the_string2.len] //zls thinks this is []const u8
std.debug.print("type: {any}\n", .{@TypeOf(attempt2)}); //however it is []u8

Example 3:

const attempt2 = the_string2[0.. :0] //zls thinks this is []u8
std.debug.print("type: {any}\n", .{@TypeOf(attempt2)}); //however it is [:0]u8

Example 4:

const attempt2 = the_string2[0..the_string2.len :0] //zls thinks this is [:0]const u8
std.debug.print("type: {any}\n", .{@TypeOf(attempt2)}); //however it is [:0]u8

The problem with these types being slightly incorrect is that the confusion stacks.
Take for example the following:

const the_string2 = try std.fmt.allocPrintSentinel(alloc, "{s}", .{the_string}, 0); //returns [:0]u8
const attempt2 = the_string2[0..the_string2.len :0] //this works fine.
const the_string2: []u8 = try std.fmt.allocPrintSentinel(alloc, "{s}", .{the_string}, 0); //changed to what zls assumes the return type is.
const attempt2 = the_string2[0..the_string2.len :0] //this is illegal behavior leading to a panic (ArrayIndexOutOfBounds).

This can be confusing for newer people.

Expected Behavior

Log Output

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions