File tree Expand file tree Collapse file tree 11 files changed +1263
-1289
lines changed
Expand file tree Collapse file tree 11 files changed +1263
-1289
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1+ TEST [[
2+ local x = 0
3+
4+ <!x!> = true
5+ ]]
6+
7+ TEST [[
8+ ---@type integer
9+ local x
10+
11+ <!x!> = true
12+ ]]
13+
14+ TEST [[
15+ ---@type unknown
16+ local x
17+
18+ x = nil
19+ ]]
20+
21+ TEST [[
22+ ---@type unknown
23+ local x
24+
25+ x = 1
26+ ]]
27+
28+ TEST [[
29+ ---@type unknown|nil
30+ local x
31+
32+ x = 1
33+ ]]
34+
35+ TEST [[
36+ local x = {}
37+
38+ x = nil
39+ ]]
40+
41+ TEST [[
42+ ---@type string
43+ local x
44+
45+ <?x?> = nil
46+ ]]
47+
48+ TEST [[
49+ ---@type string?
50+ local x
51+
52+ x = nil
53+ ]]
54+
55+ TEST [[
56+ ---@type table
57+ local x
58+
59+ <!x!> = nil
60+ ]]
61+
62+ TEST [[
63+ local x
64+
65+ x = nil
66+ ]]
67+
68+ TEST [[
69+ ---@type integer
70+ local x
71+
72+ ---@type number
73+ <!x!> = f()
74+ ]]
75+
76+ TEST [[
77+ ---@type number
78+ local x
79+
80+ ---@type integer
81+ x = f()
82+ ]]
83+
84+ TEST [[
85+ ---@type number|boolean
86+ local x
87+
88+ ---@type string
89+ <!x!> = f()
90+ ]]
91+
92+ TEST [[
93+ ---@type number|boolean
94+ local x
95+
96+ ---@type boolean
97+ x = f()
98+ ]]
99+
100+ TEST [[
101+ ---@type number|boolean
102+ local x
103+
104+ ---@type boolean|string
105+ <!x!> = f()
106+ ]]
107+
108+ TEST [[
109+ ---@type boolean
110+ local x
111+
112+ if not x then
113+ return
114+ end
115+
116+ x = f()
117+ ]]
118+
119+ TEST [[
120+ ---@type boolean
121+ local x
122+
123+ ---@type integer
124+ local y
125+
126+ <!x!> = y
127+ ]]
128+
129+ TEST [[
130+ local y = true
131+
132+ local x
133+ x = 1
134+ x = y
135+ ]]
136+
137+ TEST [[
138+ local t = {}
139+
140+ local x = 0
141+ x = x + #t
142+ ]]
143+
144+ TEST [[
145+ local x = 0
146+
147+ x = 1.0
148+ ]]
149+
150+ TEST [[
151+ ---@class A
152+
153+ local t = {}
154+
155+ ---@type A
156+ local a
157+
158+ t = a
159+ ]]
160+
161+ TEST [[
162+ ---@type integer
163+ local x
164+
165+ x = 1.0
166+ ]]
167+
168+ TEST [[
169+ ---@type integer
170+ local x
171+
172+ <!x!> = 1.5
173+ ]]
174+
175+ TEST [[
176+ ---@type integer
177+ local x
178+
179+ x = 1 + G
180+ ]]
181+
182+ TEST [[
183+ ---@type integer
184+ local x
185+
186+ x = 1 + G
187+ ]]
188+
189+ TEST [[
190+ ---@alias A integer
191+
192+ ---@type A
193+ local a
194+
195+ ---@type integer
196+ local b
197+
198+ b = a
199+ ]]
200+
201+ TEST [[
202+ ---@type string[]
203+ local t
204+
205+ <!t!> = 'xxx'
206+ ]]
207+
208+ TEST [[
209+ ---@type 1|2
210+ local x
211+
212+ x = 1
213+ x = 2
214+ <!x!> = 3
215+ ]]
216+
217+ TEST [[
218+ ---@type 'x'|'y'
219+ local x
220+
221+ x = 'x'
222+ x = 'y'
223+ <!x!> = 'z'
224+ ]]
225+
226+ TEST [[
227+ local t = {
228+ x = 1,
229+ }
230+
231+ local x
232+ t[x] = true
233+ ]]
234+
235+ TEST [[
236+ ---@type table<string, string>
237+ local x
238+
239+ ---@type table<number, string>
240+ local y
241+
242+ <!x!> = y
243+ ]]
244+
245+ TEST [[
246+ ---@type table<string, string>
247+ local x
248+
249+ ---@type table<string, number>
250+ local y
251+
252+ <!x!> = y
253+ ]]
254+
255+ TEST [[
256+ ---@type table<string, string>
257+ local x
258+
259+ ---@type table<string, string>
260+ local y
261+
262+ x = y
263+ ]]
264+
265+ TEST [[
266+ ---@type { x: number, y: number }
267+ local t1
268+
269+ ---@type { x: number }
270+ local t2
271+
272+ <!t1!> = t2
273+ ]]
274+
275+ TEST [[
276+ ---@type { x: number, [integer]: number }
277+ local t1
278+
279+ ---@type { x: number }
280+ local t2
281+
282+ <!t1!> = t2
283+ ]]
284+
285+ TEST [[
286+ local x
287+
288+ if X then
289+ x = 'A'
290+ elseif X then
291+ x = 'B'
292+ else
293+ x = 'C'
294+ end
295+
296+ local y = x
297+
298+ <!y!> = nil
299+ ]]
300+ (function (diags )
301+ local diag = diags [1 ]
302+ assert (diag .message == [[
303+ 已显式定义变量的类型为 `string` ,不能再将其类型转换为 `nil`。
304+ - `nil` 无法匹配 `string`
305+ - 类型 `nil` 无法匹配 `string`]] )
306+ end )
307+
308+ TEST [[
309+ ---@type 'A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'|'N'|'O'|'P'|'Q'|'R'|'S'|'T'|'U'|'V'|'W'|'X'|'Y'|'Z'
310+ local x
311+
312+ <!x!> = nil
313+ ]]
314+ (function (diags )
315+ local diag = diags [1 ]
316+ assert (diag .message == [[
317+ 已显式定义变量的类型为 `'A'|'B'|'C'|'D'|'E'...(+21)` ,不能再将其类型转换为 `nil`。
318+ - `nil` 无法匹配 `'A'|'B'|'C'|'D'|'E'...(+21)`
319+ - `nil` 无法匹配 `'A'|'B'|'C'|'D'|'E'...(+21)` 中的任何子类
320+ - 类型 `nil` 无法匹配 `'Z'`
321+ - 类型 `nil` 无法匹配 `'Y'`
322+ - 类型 `nil` 无法匹配 `'X'`
323+ - 类型 `nil` 无法匹配 `'W'`
324+ - 类型 `nil` 无法匹配 `'V'`
325+ - 类型 `nil` 无法匹配 `'U'`
326+ - 类型 `nil` 无法匹配 `'T'`
327+ - 类型 `nil` 无法匹配 `'S'`
328+ - 类型 `nil` 无法匹配 `'R'`
329+ - 类型 `nil` 无法匹配 `'Q'`
330+ ...(+13)
331+ - 类型 `nil` 无法匹配 `'C'`
332+ - 类型 `nil` 无法匹配 `'B'`
333+ - 类型 `nil` 无法匹配 `'A'`]] )
334+ end )
Original file line number Diff line number Diff line change 1+ TEST [[
2+ ---@type string|boolean
3+ local t
4+
5+ ---@cast t string
6+ ]]
7+
8+ TEST [[
9+ ---@type string|boolean
10+ local t
11+
12+ ---@cast t <!number!>
13+ ]]
You can’t perform that action at this time.
0 commit comments