LuaLS / LuaLS/lua-language-server

Type is "lost" of variable within For Loop

Đang mở
#2,374 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

bug
Ngôn ngữ chính
Lua
Star
4.4k
Fork
442
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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?

Type Checking

Expected Behaviour

A variable shouldn't loose its type when being set in a For Loop to another string. The variable is initialised outside of the For Loop.

Actual Behaviour

The variable seems to have lost its type after the For Loop when being set in a For Loop to another string.

Reproduction steps

Paste the sample code below.

--- Pad a number with leading 0's up to the required length and return as a string.
---@param number double
---@param requiredLength uint
---@return string paddedNumber
StringUtils.PadNumberToMinimumDigits = function(number, requiredLength)
	local negativeNumber = false
    if number < 0 then
        negativeNumber = true
        number = 0 - number
    end
    local numberString = tostring(number)
    local shortBy = requiredLength - string_len(numberString)
    for _ = 1, shortBy do
        numberString = "0" .. numberString
    end
    if negativeNumber then
-- ISSUE: `numberString` has lost its type here.
        numberString = "-" .. numberString
    end
    return numberString
end

By the end of the function the variable numberString will have lost its type and be unknown. But in all the places we set the variables value it's being set to a string.

image

Additional Notes

No response

Log File

[23:51:57.944][warn] [#0:script\plugin.lua:101]: plugin not found: C:\Home\Projects\Factorio Modding\Factorio-Zombie-Engineer
[23:52:02.194][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/FactorioModding/data/base/prototypes/entity/entities.lua] takes [0.118] sec, size [466.802] kb.
[23:52:02.663][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/FactorioModding/data/base/prototypes/entity/trees.lua] takes [0.108] sec, size [185.481] kb.
[23:52:03.538][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/FactorioModding/data/base/scenarios/team-production/map_sets.lua] takes [0.112] sec, size [221.774] kb.
[23:52:04.319][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/FactorioModding/data/core/lualib/collision-mask-util.lua] takes [0.109] sec, size [12.290] kb.
[23:52:05.022][warn] [#0:script\files.lua:558]: Parse LuaDoc of [file:///c%3A/Users/mikeh/AppData/Roaming/Code/User/workspaceStorage/34445a4340c7b2315555c5d263edbf99/justarandomgeek.factoriomod-debug/sumneko-3rd/factorio/library/runtime-api-events.lua] takes [0.153] sec, size [203.992] kb.
[23:52:06.694][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/didOpen] takes [9.109]sec. {
jsonrpc = "2.0",
method = "textDocument/didOpen",
params = {
textDocument = {
languageId = "lua",
text = '"***"',
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua",
version = 1
}
}
}
[23:52:06.694][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/codeAction] takes [9.108]sec. {
id = 2,
jsonrpc = "2.0",
method = "textDocument/codeAction",
params = {
context = {
diagnostics = {},
triggerKind = 2
},
range = {
end = {
character = 11,
line = 15
},
start = {
character = 11,
line = 15
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:52:06.694][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/documentColor] takes [9.107]sec. {
id = 3,
jsonrpc = "2.0",
method = "textDocument/documentColor",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:52:06.694][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/foldingRange] takes [8.893]sec. {
id = 6,
jsonrpc = "2.0",
method = "textDocument/foldingRange",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:52:06.694][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/documentSymbol] takes [9.108]sec. {
id = 1,
jsonrpc = "2.0",
method = "textDocument/documentSymbol",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:52:06.694][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [9.000]sec. {
id = 5,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 0,
line = 17
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:52:08.429][warn] [#0:script\core\diagnostics\init.lua:136]: Diagnostics [assign-type-mismatch] @ [file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/emmyLua/math-uint.lua] takes [1.697] sec!
[23:53:31.413][warn] [#0:script\provider\provider.lua:625]: Completion takes 0.107 sec.
[23:55:16.163][warn] [#0:script\core\diagnostics\init.lua:136]: Diagnostics [redundant-value] @ [file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua] takes [0.656] sec!
[23:55:16.429][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/codeAction] takes [0.913]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 442,
jsonrpc = "2.0",
method = "textDocument/codeAction",
params = {
context = {
diagnostics = {},
triggerKind = 2
},
range = {
end = {
character = 16,
line = 38
},
start = {
character = 16,
line = 38
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:20.522][warn] [#0:script\plugin.lua:37]: Call plugin event [OnSetText] takes [0.113] sec
[23:55:20.647][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/FactorioModding/data/base/prototypes/entity/entities.lua] takes [0.123] sec, size [466.802] kb.
[23:55:21.522][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/FactorioModding/data/base/prototypes/entity/trees.lua] takes [0.123] sec, size [185.481] kb.
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/foldingRange] takes [5.995]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 443,
jsonrpc = "2.0",
method = "textDocument/foldingRange",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/documentHighlight] takes [6.292]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 439,
jsonrpc = "2.0",
method = "textDocument/documentHighlight",
params = {
position = {
character = 16,
line = 38
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [6.142]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 441,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 35,
line = 73
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [3.719]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 449,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 56,
line = 67
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [3.373]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 452,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 66,
line = 62
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [3.030]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 454,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 59,
line = 57
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [2.792]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 456,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 69,
line = 52
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [2.025]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 458,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 32,
line = 63
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [1.433]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 461,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 35,
line = 73
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/documentColor] takes [6.295]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 437,
jsonrpc = "2.0",
method = "textDocument/documentColor",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:21.554][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/codeAction] takes [4.869]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 446,
jsonrpc = "2.0",
method = "textDocument/codeAction",
params = {
context = {
diagnostics = {},
triggerKind = 2
},
range = {
end = {
character = 16,
line = 38
},
start = {
character = 16,
line = 38
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:22.007][warn] [#0:script\plugin.lua:37]: Call plugin event [OnSetText] takes [0.133] sec
[23:55:22.522][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [0.931]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 465,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 4,
line = 3
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:55:22.522][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/foldingRange] takes [0.760]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 466,
jsonrpc = "2.0",
method = "textDocument/foldingRange",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:55:22.694][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/FactorioModding/data/base/prototypes/tile/tiles.lua] takes [0.109] sec, size [137.804] kb.
[23:55:23.288][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/FactorioModding/data/base/scenarios/team-production/map_sets.lua] takes [0.120] sec, size [221.774] kb.
[23:55:23.991][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/FactorioModding/data/core/lualib/noise.lua] takes [0.143] sec, size [12.994] kb.
[23:55:24.460][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [1.760]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 470,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 0,
line = 0
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:55:24.460][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/foldingRange] takes [1.648]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 471,
jsonrpc = "2.0",
method = "textDocument/foldingRange",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:55:24.476][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/documentSymbol] takes [1.437]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 473,
jsonrpc = "2.0",
method = "textDocument/documentSymbol",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:55:25.163][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/Users/mikeh/AppData/Roaming/Code/User/workspaceStorage/34445a4340c7b2315555c5d263edbf99/justarandomgeek.factoriomod-debug/sumneko-3rd/factorio/library/runtime-api-LuaEntityPrototype.lua] takes [0.157] sec, size [67.400] kb.
[23:55:25.788][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/codeAction] takes [1.025]sec. {
_closeMessage = "Request cancelled.",
_closeReason = -32800,
id = 480,
jsonrpc = "2.0",
method = "textDocument/codeAction",
params = {
context = {
diagnostics = {},
triggerKind = 2
},
range = {
end = {
character = 16,
line = 38
},
start = {
character = 16,
line = 38
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:25.913][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/documentColor] takes [4.361]sec. {
id = 462,
jsonrpc = "2.0",
method = "textDocument/documentColor",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/test.lua"
}
}
}
[23:55:25.913][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/documentColor] takes [1.293]sec. {
id = 476,
jsonrpc = "2.0",
method = "textDocument/documentColor",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:26.038][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/semanticTokens/range] takes [1.274]sec. {
id = 479,
jsonrpc = "2.0",
method = "textDocument/semanticTokens/range",
params = {
range = {
end = {
character = 35,
line = 73
},
start = {
character = 0,
line = 0
}
},
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:55:26.679][warn] [#0:script\proto\proto.lua:187]: Method [textDocument/foldingRange] takes [1.868]sec. {
id = 481,
jsonrpc = "2.0",
method = "textDocument/foldingRange",
params = {
textDocument = {
uri = "file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua"
}
}
}
[23:57:25.413][warn] [#0:script\files.lua:695]: Compile [file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-Zombie-Engineer/utility/functions/player-weapon.lua] takes [0.368] sec, size [19.117] kb.
[00:04:16.304][warn] [#0:script\await.lua:191]: Await step takes [0.569] sec.
[00:06:55.538][warn] [#0:script\await.lua:191]: Await step takes [0.501] sec.
[00:10:43.444][warn] [#0:script\await.lua:191]: Await step takes [0.524] sec.
[00:13:37.335][warn] [#0:script\await.lua:191]: Await step takes [0.504] sec.
[00:16:23.804][warn] [#0:script\await.lua:191]: Await step takes [0.512] sec.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện vấn đề với ví dụ PadNumberToMinimumDigits được cung cấp trong phần chẩn đoán kiểm tra kiểu của máy chủ ngôn ngữ Lua. Theo dõi cách biến string đã được khởi tạo được phân tích qua vòng lặp for số, sau đó xác minh rằng nó vẫn được định kiểu là string sau vòng lặp và trong phép nối cuối cùng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
lua
Lĩnh vực
devtools
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.