Skip to content

Cannot type for-loop variable #2487

Open

Description

@saecki

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Linux

What is the issue affecting?

Type Checking, Diagnostics/Syntax Checking

Expected Behaviour

The variables s and r are typed according to the @type annotation.

Actual Behaviour

In the following code snippet I cannot type the for loop variables s and r using the @type annotation:

    ---@type integer, string
    for s, r in str:gmatch("[,]?%s*()([^,]+)%s*[,]?") do
        ...
    end

Instead I get a Can not infer type. error for r and s is shown as of type string

Reproduction steps

Additional Notes

I think #1367 enabled this, but only for primitive for loops.

Log File

No response

Activity

  1. asumbek commented on Jan 19, 2024

    @asumbek

    You can use @cast annotation for a workaround, but I think this will be a better alternative to that.

  2. CppCXY commented on Jan 19, 2024

    @CppCXY
    Member

    ---@param s string

  3. saecki commented on Jan 19, 2024

    @saecki
    Author

    Is that documented somewhere? I feel like it's somewhat unintuitive

    Maybe @type could also be supported (or even be used instead), since I feel like it's more in line with a variable assignment than a function definition.

  4. changed the title [-]Cannot type for loop variable[/-] [+]Cannot type for-loop variable[/+] on Jan 31, 2024
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions