Skip to content

Commit 81a0d7a

Browse files
committed
Basic types allow contravariance
1 parent dd1bcd8 commit 81a0d7a

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

changelog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
local function callback(a) end --> a: integer
1414
register(callback)
1515
```
16+
* `CHG` Basic types allow contravariance
17+
```lua
18+
---@class int32: integer
19+
20+
---@type integer
21+
local n
22+
23+
---@type int32
24+
local a = n
25+
```
1626

1727
## 3.12.0
1828
`2024-10-30`

script/vm/type.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,11 @@ function vm.isSubType(uri, child, parent, mark, errs)
584584
local x = '' --> `string` set to `A`
585585
]]
586586
if guide.isBasicType(childName)
587-
and guide.isLiteral(child)
588-
and vm.isSubType(uri, parentName, childName, mark) then
589-
return true
587+
and not mark[childName] then
588+
mark[childName] = true
589+
if vm.isSubType(uri, parentName, childName, mark) then
590+
return true
591+
end
590592
end
591593

592594
if errs then

test/diagnostics/param-type-mismatch.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,16 @@ function f(a) end
352352
f(a)
353353
]]
354354

355+
TEST [[
356+
---@class A: integer
357+
358+
---@param a A
359+
local function f(a) end
360+
361+
---@type integer
362+
local i
363+
364+
f(i)
365+
]]
366+
355367
config.set(nil, 'Lua.type.checkTableShape', false)

0 commit comments

Comments
 (0)