-
Notifications
You must be signed in to change notification settings - Fork 40
feat: treat array index as nil-able #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Expressions like this: ```lua ---@type integer[] local arr = {1, 2, 3} return arr[2] ``` should be considered as type `integer|nil`, since array accesses can always *potentially* return `nil`. This commit marks those expressions as such.
return Ok(TypeOps::Union.apply(base, &LuaType::Nil)); | ||
} else if member_key.is_expr() { | ||
let expr = member_key.get_expr().ok_or(InferFailReason::None)?; | ||
let expr_type = infer_expr(db, cache, expr.clone())?; | ||
if check_type_compact(db, &LuaType::Number, &expr_type).is_ok() { | ||
return Ok(base.clone()); | ||
return Ok(TypeOps::Union.apply(base, &LuaType::Nil)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am assuming this is also necessary?
cc @lewis6991, will this be too annoying in practice? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
I think we need a configuration for strictly indexing arrays, because I noticed that even in Rust, array access operations do not return Option, and other languages, including TypeScript, do not return T | null. |
I've gated this change behind a config option, defaulting to |
Expressions like this:
should be considered as type
integer|nil
, since array accesses can always potentially returnnil
. This commit marks those expressions as such.Related to #248