Skip to content

Bad completion and signature help on functions assigned to const #3282

Open

Description

@psznm

Zig Version

0.17.0

ZLS Version

0.17.0-dev.91+ff89747f

Client / Code Editor / Extensions

nvim

Steps to Reproduce and Observed Behavior

ZLS show completion and signature help for different parameter than cursor is at when the method has been assigned to a const.

This is particularly bad when working with files generated by translate c, but I also noticed the issue with my own structures which have functions generated in comptime.

const other_file = struct {
    fn testFn(_: Test, _: enum{yes, no}) void { }
};
const Test = struct {
    idk: usize = 6,
    pub const testFn = other_file.testFn;
};
comptime {
    const idk: Test = undefined;
    idk.testFn(param: enum {...}) // completed by selecting testFn completion on idk.<cursor>: already OK
    idk.testFn(.<cursor>) // no completion
    idk.testFn(undefined, .<cursor>) // yes/no enum completion
    idk.testFn(.yes) // signature help on ".yes" points to "_: Test"
    idk.testFn(.yes) // go to reference on ".yes" goes to "yes" in "_: enum{yes, no}": already OK
}

Expected Behavior

    idk.testFn(param: enum {...}) // // completed by selecting testFn completion on idk.<cursor>: already OK
    idk.testFn(.<cursor>) // yes/no completion
    idk.testFn(undefined, .<cursor>) // no completion - parameter at that index does not exist
    idk.testFn(.yes) // signature help on ".yes" points to "_: enum{yes, no}"
    idk.testFn(.yes) // go to reference on ".yes" goes to "yes" in "_: enum{yes, no}": already OK

Activity

  1. psznm commented on Oct 17, 2026

    @psznm
    ContributorAuthor

    I made little progress on this. I managed to fix a test regarding this that I added. I thought the issue was that a hasSelfParam function returned false for the function in question. But fixing that broke one of the assertions (the last one) in completion "function alias" test which apparently relied on the broken hasSelfParam, and one more test that seemed to have suffered from the same issue. Not fully understanding what is going on, not sure if hasSelfParam is wrong or is just being given wrong data, I took a step back and started debugging to understand more. Eventually as I learned debugging what exactly is being inspected (some dedicated debug utils would be really nice), I found that hasSelfParam = false was reported for fn foo in

    const S = struct {
        fn foo(_: S, _: enum{ alpha, beta }) void {} // reported has self param = false during getting completions for s.bar
        const bar = foo;
    };
    const s: S = undefined;
    const baz = s.bar(.<cursor>); // missing completons, if I add fields on S, it tries to complete fields of S
    

    And since there was a condition for namespace I made sure that it is being badly reported also for non-namespaces - for non namespaces it just tries to complete fields of the container.

    So I came to the conclusion that the hasSelfParam is indeed wrong and needs to be fixed. So my fix of hasSelfParam, while maybe correct, broke other things that rely on hasSelfParam being wrong. So I will see if I can fix that too...

  2. techatrix commented on Oct 17, 2026

    @techatrix
    Member

    Yes hasSelfParam is indeed flawed for the following reasons:

    • Namespaces (struct with no fields) can actually be called with the my_namespace.my_func() syntax. Related Methods on namespaces #3278
    • It compares the first parameter with the container type the function is defined in as opposed to using the type of foo in foo.my_func() which doesn't have to be same because my_func might be an alias.

    I addressed this for signature help by replacing hasSelfParam with firstParamIs in #3290. The same should be done for autocomplete which I haven't look much into yet. Let me know if I can do anything to assist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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