Skip to content

In a specific case the type of a value in a table can't be obtained in pairs, but can in straight . dot notation  #1467

@muppet9010

Description

@muppet9010

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

That the type of the value of a table is obtained in both pairs and dot notation at all times.

Type of x and the type of xChunkObject.

local x = chunkDetailsByAxis.xChunks[1]

vs

for _, xChunkObject in pairs(chunkDetailsByAxis.xChunks) do
    ...
end

Actual Behaviour

The "value" field in the FOR loop is unable to get the type of the value in the table, but the dot notation can.

dot notation populating the variable x.
image

FOR loop failing to get the type to populate the variable xChunkObject.
image

Both "know" the type of the parent chunkDetailsByAxis at the time of the issue,
image

The actual warning by Sumneko is on the next line when we try to use a field of xChunkObject, as it's trying to use a field of an unknown type. But this xChunkObject is the start of the issues.

It only seems to occur for this 1 for loop. All my other code retains it's correct types with no warnings shown.

Reproduction steps

The usage of the class is in 1 file and the class is declared in another file. I shall refer to these as Usage and Declare below.

If I edit anything in the Usage file to force Sumneko to rescan then the issue goes away.
I can reproduce the issue in the Usage file by editing the Declare file so that it has an invalid Lua code syntax and then back to being valid again. i.e. removing a trailing ) from any function in the file and then putting it back and saving the file again.

The issue with reproduction is you'd need my mod (public github) and all of the libraries I add of class definitions for Factorio (public). So I can provide them if that's needed, but I appreciate it will be some messing around for you to have the same large environment.

Additional Notes

The type definitions of the these top levels of the class hierarchy are below. This should be the complete loop of self referencing classes. The other custom classes used like int and MapPosition are either simple self contained classes, aliases to equally simple classes or literals.

---@class Task_ScanAreasForActionsToComplete_ChunksInCombinedAreas # Is effectively the XChunks class, as no parent object with extra meta data is needed.
---@field minXValue int # The lowest X chunk position value in the xChunks table.
---@field maxXValue int # The highest X chunk position value in the xChunks table.
---@field minYValueAcrossAllXValues int # The lowest Y chunk position value across all of the X chunk tables. Just to save iterating them all each time to find it.
---@field maxYValueAcrossAllXValues int # The highest Y chunk position value across all of the X chunk tables. Just to save iterating them all each time to find it.
---@field xChunks table<int, Task_ScanAreasForActionsToComplete_ChunksInCombinedAreas_XChunkObject> # A table of X values that have included chunks within them. This can be a gappy list.

---@class Task_ScanAreasForActionsToComplete_ChunksInCombinedAreas_XChunkObject
---@field minYValue int # The lowest Y chunk position value in the yChunks table.
---@field maxYValue int # The highest Y chunk position value in the yChunks table.
---@field yChunks table<int, Task_ScanAreasForActionsToComplete_ChunkDetails> # A table of Y values that have included chunks within them. This can be a gappy list.

---@class Task_ScanAreasForActionsToComplete_ChunkDetails
---@field chunkPosition ChunkPosition
---@field chunkPositionString string # A string of the chunk position. Used by other tasks making use of this data set.
---@field toBeDeconstructedEntityDetails table<uint, Task_ScanAreasForActionsToComplete_EntityDetails> # Keyed by the Id of the entity details. These Id keys list will be very gappy.
---@field toBeUpgradedTypes table<string, table<uint, Task_ScanAreasForActionsToComplete_EntityDetails>> # Grouped by the entity item used to upgrade it first and then keyed by the Id of the entity details. These Id keys list will be very gappy.
---@field toBeBuiltTypes table<string, table<uint, Task_ScanAreasForActionsToComplete_EntityDetails>> # Grouped by the entity item type used to build it first and then keyed by the Id of the entity details. These Id keys list will be very gappy.

---@class Task_ScanAreasForActionsToComplete_EntityDetails
---@field entityListKey uint # Key in the entitiesToBe[X] table.
---@field identifier string|uint # The entities unit_number or its name and position as a string
---@field entity LuaEntity
---@field entity_type string # The type of the entity in relation to the actionType. For deconstruction this is the entity to be removed, for build it is the new entity, for upgrades it is the new entity being upgraded too.
---@field entity_name string # The name of the entity in relation to the actionType. For deconstruction this is the entity to be removed, for build it is the new entity, for upgrades it is the new entity being upgraded too.
---@field position MapPosition
---@field chunkDetails Task_ScanAreasForActionsToComplete_ChunkDetails
---@field actionType Task_ScanAreasForActionsToComplete_ActionType
---@field builtByItemName? string # The name of the item type used to build it for the action type, or nil if no item is required for the action type.
---@field builtByItemCount? uint # The count of the item type used to build it for the action type, or nil if no item is required for the action type. Most are 1, but curved rails are more and some modded things could also be >1.

Log File

file_c%3A_Home_Projects_Factorio%20Modding_Factorio-A-Robot-Friend.log
service.log

In terms of file names of the Usage and Declare I referenced:
Declared: scan-areas-for-actions-to-complete.lua
full: file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-A-Robot-Friend/scripts/tasks/scan-areas-for-actions-to-complete.lua
Usage: deconstruct-entities-in-chunk-details.lua
full: file:///c%3A/Home/Projects/Factorio%20Modding/Factorio-A-Robot-Friend/scripts/tasks/deconstruct-entities-in-chunk-details.lua

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions