zls icon indicating copy to clipboard operation
zls copied to clipboard

Method functions not recognized in file they're declared

Open MiahDrao97 opened this issue 6 months ago • 1 comments

Zig Version

0.15.0-dev.936+fc2c1883b

ZLS Version

0.15.0-dev.274+94705cba

Client / Code Editor / Extensions

nvim 0.10.2

Steps to Reproduce and Observed Behavior

There seem to be 2 ways this is reproduced:

First, when a struct is defined as the file itself, then method declarations as well as method calls are not assigned the expected @lsp.type.method semantic token.

const This = @This();

foo: u16,

pub fn init(wchar: u16) This {
    return .{ .foo = wchar };
}

/// this method declaration is not recognized as a method
pub fn toByte(this: This) u8 {
    return @truncate(this.foo);
}

test "there is no method here" {
    const one_of_these: This = .init('a'); 
    try @import("std").testing.expectEqual('a', one_of_these.toByte()); // <-- `toByte()` is not recognized as a method
}

Second, on normal struct declaration, method declarations are correctly recognized. However, if you call a method defined in the same file (by unit test or from another function), it's not identified as a method. The code snippet below reproduces the issue, but I also see spots in my own project code where that isn't always the case.

pub const Baz = struct {
    bar: u16,

    pub fn init(wchar: u16) Baz {
        return .{ .bar = wchar };
    }

    /// this method declaration IS correctly recognized
    pub fn toByte(this: Baz) u8 {
        return @truncate(this.bar);
    }
};

test "there is still no method here" {
    const one_of_those: Baz = .init('a');
    try @import("std").testing.expectEqual('a', one_of_those.toByte()); // <-- however, `toByte()` here is not recognized as a method
}

Expected Behavior

Scenario A:

const This = @This();

foo: u16,

pub fn init(wchar: u16) This {
    return .{ .foo = wchar };
}

/// this method declaration should be recognized as a method
pub fn toByte(this: This) u8 {
    return @truncate(this.foo);
}

test "there is no method here" {
    const one_of_these: This = .init('a'); 
    try @import("std").testing.expectEqual('a', one_of_these.toByte()); // <-- `toByte()` should be recognized as a method
}

Scenario B:

pub const Baz = struct {
    bar: u16,

    pub fn init(wchar: u16) Baz {
        return .{ .bar = wchar };
    }

    /// this declaration should be recognized as a method (which it currently is)
    pub fn toByte(this: Baz) u8 {
        return @truncate(this.bar);
    }
};

test "there is still no method here" {
    const one_of_those: Baz = .init('a');
    try @import("std").testing.expectEqual('a', one_of_those.toByte()); // <-- `toByte()` should be recognized as a method

More Comments On The Issue

Hovering still works as expected, and auto-complete (using hrsh7th/nvim-cmp) correctly identifies toByte() as a method in both cases, so that's good.

Outside of the file they're correctly identified as methods... usually. Now I don't have any way to consistently reproduce this, but check this out. Image The above screenshot was taken from a unit test file, where the types and declarations were made in a different file. The functions correctly identified as methods are yellow while non-method functions are light blue. And as you'd guess, the len() function doesn't have the @lsp.type.method semantic token on inspection when it should. But as I mentioned before, I don't have any way to reproduce it, and which methods are not recognized when seems almost random. So these two cases are the only concrete things I've got at the moment. It would appear that methods may be more widely borked, but hopefully these two cases are helpful hints in figuring out a potentially larger problem.

I will say, that on ZLS 0.14.0, this is a non-issue entirely, so it may be some kind of regression between now and then.

Relevant log output


MiahDrao97 avatar Jul 12 '25 15:07 MiahDrao97

Ope, I realized I'm missing a closing curly on the test block Expected Behavior Scenario B, but I think you get the idea.

MiahDrao97 avatar Jul 12 '25 16:07 MiahDrao97