Skip to content

Recursive alias results in stack overflow #3283

@MarkLagodych

Description

@MarkLagodych

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Linux

What is the issue affecting?

Type Checking, Annotations, Hover

Expected Behaviour

  1. Aliases (especially recursive) are not expanded in hover tips.
  2. The language server does not crash and stays responsive even when some aliases are recursive.

Actual Behaviour

  1. Recursive aliases get expanded recursively. This behavior in hover tips is not helpful at all because it gets very hard to understand the types.
Image 2. The language server produces "stack overflow" errors in the output and freezes for a few seconds on every file save. Linting and format-on-save become very slow and annoying. Also, file saving does not work 100% of the time.

Reproduction steps

  1. Create a file with the code:
---@alias Tree
---| { parent: Tree|nil, type: "leaf", [1]: string }
---| { parent: Tree|nil, type: "node", [1]: Tree, [2]: Tree }


---@param t Tree
---@return Tree
local function do_something(t)
    if t.type == "leaf" then
        return t
    elseif t.type == "node" and #t == 1 then
        table.insert(t, { type = "leaf", "b" })
        return t
    else
        return t
    end
end

local function main()
    local leaf = { type = "leaf", "a" }
    local node = { type = "node", leaf }
    leaf.parent = node
    print(do_something(node))
end

main()
  1. Try to make a small change, e.g. add some spaces after return in return t and save the changes. The saving and format-on-save are largely delayed and the server produces the error log provided below.
  2. Try hovering the Tree in @param t Tree. The hover tip is recursively expanded and messy as shown above.

Additional Notes

I really need to create recursive data types like trees and doubly-linked lists in my code, but just using an @alias like that gets tedious with the current language server version. If there is a simple workaround, I would love to use that.

Log File

file_home_mark_Projects_try-lua-ls.log

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