Skip to content

Commit 7fa9e42

Browse files
committed
fix: LuaJIT ?? strips optional ? from the left-hand type
integer? stores nil as a node flag, so ?? never saw a nil object and copied the optional type unchanged. Treat isOptional() as hasNil so the existing removeOptional() branch runs.
1 parent 66d6405 commit 7fa9e42

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
5+
* `FIX` LuaJIT nil-coalescing `??` now strips the optional `?` from the left-hand type (e.g. `integer? ?? 2` infers `integer`) [#3459](https://github.com/LuaLS/lua-language-server/issues/3459)
56
* `FIX` A `.gitignore` line containing only whitespace no longer ignores the entire workspace [#3458](https://github.com/LuaLS/lua-language-server/issues/3458)
67
* `FIX` `need-check-nil` diagnostic is no longer reported on safe navigation access (e.g. `x?.field`, `f?.()`, `t?.[key]`), since the optional access itself already handles the nil check. Note that a non-safe access chained after a safe one (e.g. `x.upper()?.field`) still reports, because the safe access only protects its own result.
78

‎script/vm/operator.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ vm.binarySwitch = util.switch()
272272
local node2 = vm.compileNode(source[2])
273273
-- 统计具体类型:variable/local 是引用元信息,无具体类型时视为未知(可能为 nil)
274274
local count = 0
275-
local hasNil = false
275+
local hasNil = node1:isOptional()
276276
for c in node1:eachObject() do
277277
if c.type == 'nil'
278278
or (c.type == 'global' and c.cate == 'type' and c.name == 'nil') then

‎test/type_inference/common.lua‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,6 +3312,14 @@ end
33123312
local <?x?> = n or 0
33133313
]]
33143314

3315+
config.set(nil, 'Lua.runtime.nonstandardSymbol', { '??' })
3316+
TEST 'integer' [[
3317+
---@type integer?
3318+
local foo = 1
3319+
local <?bar?> = foo ?? 2
3320+
]]
3321+
config.set(nil, 'Lua.runtime.nonstandardSymbol', {})
3322+
33153323
TEST 'number' [=[
33163324
local <?x?> = F()--[[@as number]]
33173325
]=]

0 commit comments

Comments
 (0)