LuaLS / LuaLS/lua-language-server

Generics Return Issue

Open
#2,188 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement feat/generic
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Annotations

Expected Behaviour

In this function:

--- Merge two tables together, keys that are the same will use the value from the second table
---@generic K, V, L, W
---@param table1 table<K, V>
---@param table2 table<L, W>
---@return table<K | L, V | W>
function table.merge(table1, table2)
	for k, v in pairs(table2) do
		table1[k] = v
	end

	return table1
end

I would expect the return type to be table<K | L, V | W> however it is actually always table<K, V>
There is also a secondary issue here where k, v are seen as unknown when they should be seen as V, W (I assume this is another generics issue)

Actual Behaviour
Screenshot_1 Screenshot_2
Reproduction steps
  1. Copy example code
  2. Observe result
Additional Notes

No response

Log File

No response

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

Reproduce the annotated table.merge example in the Visual Studio Code extension on Windows. Start by tracing generic annotation and table type inference for @generic, @param, @return, and pairs; done means the return is table<K | L, V | W> and k and v resolve to the second table's types.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.