LuaLS / LuaLS/lua-language-server

Can not infer basic assignments in nested loop.

Open
#2,494 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

Discussed in https://github.com/LuaLS/lua-language-server/discussions/2471

Originally posted by Luke100000 January 8, 2024
Why can it not infer the type here? How would I rewrite this to be type conform, without spamming type annotations?

local y = 0

for i = 1, 2 do
    y = y + 1 -- here, y is inferred as an integer correctly

    for j = 1, 2 do
        y = y + 1 -- can not infer type
    end

    -- sometimes a bit more complexity is required to trigger the issue, so lets add another loop
    for j = 1, 2 do
        y = y + 1 -- can not infer type
    end
end

print(y)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the nested-loop example from the issue and read discussion 2471 for the existing analysis. Trace how the language server infers y across the loop assignments. Done means the shown assignments retain the inferred integer type without requiring repeated type annotations.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.