File tree Expand file tree Collapse file tree 4 files changed +68
-1
lines changed Expand file tree Collapse file tree 4 files changed +68
-1
lines changed Original file line number Diff line number Diff line change 3
3
## 3.4.1
4
4
* ` NEW ` settings:
5
5
* ` type.weakNilCheck `
6
+ * ` CHG ` allow type contravariance for ` setmetatable ` when initializing a class
7
+ ``` lua
8
+ --- @class A
9
+ local a = {}
10
+
11
+ --- @class B : A
12
+ local b = setmetatable ({}, { __index = a }) -- OK!
13
+ ```
6
14
* ` FIX ` [ #1256 ] ( https://github.com/sumneko/lua-language-server/issues/1256 )
7
15
* ` FIX ` [ #1257 ] ( https://github.com/sumneko/lua-language-server/issues/1257 )
8
16
* ` FIX ` [ #1267 ] ( https://github.com/sumneko/lua-language-server/issues/1267 )
Original file line number Diff line number Diff line change @@ -30,6 +30,20 @@ local function hasMarkType(source)
30
30
return false
31
31
end
32
32
33
+ --- @param source parser.object
34
+ --- @return boolean
35
+ local function hasMarkClass (source )
36
+ if not source .bindDocs then
37
+ return false
38
+ end
39
+ for _ , doc in ipairs (source .bindDocs ) do
40
+ if doc .type == ' doc.class' then
41
+ return true
42
+ end
43
+ end
44
+ return false
45
+ end
46
+
33
47
--- @async
34
48
return function (uri , callback )
35
49
local state = files .getState (uri )
@@ -72,6 +86,18 @@ return function (uri, callback)
72
86
if vm .canCastType (uri , varNode , valueNode ) then
73
87
return
74
88
end
89
+
90
+ if value .type == ' select'
91
+ and value .sindex == 1
92
+ and value .vararg
93
+ and value .vararg .type == ' call'
94
+ and value .vararg .node .special == ' setmetatable'
95
+ and hasMarkClass (source ) then
96
+ if vm .canCastType (uri , valueNode :copy ():remove ' table' , varNode ) then
97
+ return
98
+ end
99
+ end
100
+
75
101
callback {
76
102
start = source .start ,
77
103
finish = source .finish ,
Original file line number Diff line number Diff line change @@ -227,7 +227,7 @@ function mt:remove(name)
227
227
or (c .type == ' doc.type.boolean' and name == ' false' and c [1 ] == false )
228
228
or (c .type == ' doc.type.table' and name == ' table' )
229
229
or (c .type == ' doc.type.array' and name == ' table' )
230
- or (c .type == ' doc.type.sign' and name == ' table' )
230
+ or (c .type == ' doc.type.sign' and name == ' table' )
231
231
or (c .type == ' doc.type.function' and name == ' function' ) then
232
232
table.remove (self , index )
233
233
self [c ] = nil
Original file line number Diff line number Diff line change @@ -631,5 +631,38 @@ n = nb
631
631
]]
632
632
config .set (nil , ' Lua.type.weakNilCheck' , false )
633
633
634
+ TEST [[
635
+ ---@class A
636
+ local a = {}
637
+
638
+ ---@class B: A
639
+ local <!b!> = a
640
+ ]]
641
+
642
+ TEST [[
643
+ ---@class A
644
+ local a = {}
645
+ a.__index = a
646
+
647
+ ---@class B: A
648
+ local b = setmetatable({}, a)
649
+ ]]
650
+
651
+ TEST [[
652
+ ---@class A
653
+ local a = {}
654
+
655
+ ---@class B: A
656
+ local b = setmetatable({}, {__index = a})
657
+ ]]
658
+
659
+ TEST [[
660
+ ---@class A
661
+ local a = {}
662
+
663
+ ---@class B
664
+ local <!b!> = setmetatable({}, {__index = a})
665
+ ]]
666
+
634
667
config .remove (nil , ' Lua.diagnostics.disable' , ' unused-local' )
635
668
config .remove (nil , ' Lua.diagnostics.disable' , ' undefined-global' )
You can’t perform that action at this time.
0 commit comments