fix: support bracket index in @cast expressions, to enable type narrowing for array element access#1031
fix: support bracket index in @cast expressions, to enable type narrowing for array element access#1031EfveZombie wants to merge 1 commit intoEmmyLuaLs:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for bracket indexing in @cast expressions, enabling type narrowing for array elements (e.g., ---@cast addresses[1] -nil). The changes include lexer updates to recognize brackets and integer literals, and parser modifications to handle multi-level field access using both dot and bracket notation. Comprehensive tests were added to verify diagnostic behavior for parameter type mismatches and undefined fields. A review comment identifies that TkName tokens used within brackets should be wrapped in a NameExpr node to ensure the index key is correctly retrieved by the analysis engine, similar to how literals are handled.
…wing for array element access like `@cast addresses[1] -nil`
32aae33 to
fa3542c
Compare
|
This shouldn’t be handled this way; I’ve decided to implement it myself. |
Problem
@castexpressions with bracket index access (e.g.,---@cast addresses[1] -nil) were not working correctly. Two issues existed:1. Lexer/Parser did not support
[]in@castexpressionsThe doc lexer's
lex_cast_exprstate did not recognize[,], or integer literals, causing---@cast addresses[1] -nilto be incorrectly parsed. This corrupted the type inference foraddresses, leading to a spuriousUndefined field [1]warning.2. Parsed
IndexExprhad wrong AST structureAfter fixing the parser, the
TkInttoken inside[1]was placed as a bare token in theIndexExprnode. However,LuaIndexExpr::get_index_key()expects a child Node (likeLuaLiteralExpr), not a bare token. This caused:get_index_key()→Noneget_access_path()→Noneget_index_expr_var_ref_id()→Noneaddresses[1]remainedstring|nilinstead of being narrowed tostring, causing a spuriousparam-type-mismatchwarningFix
lua_doc_lexer.rs): Added support for[,], and digit literals inlex_cast_exprtag.rs): Inparse_cast_expr, wrapTkInt/TkStringtokens inside[]in aLiteralExprnode, making the docIndexExprstructure consistent with Lua codeIndexExprTests
test_array_index_with_castinundefined_field_test.rstest_cast_bracket_index_narrows_typeinparam_type_check_test.rsto verify the narrowing actually works