-
Notifications
You must be signed in to change notification settings - Fork 55
fix: improve type narrowing for 'and'/'or' operators with nullable types #926
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
Summary of ChangesHello @lewis6991, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the type narrowing capabilities of the analyzer for Lua's logical Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request improves type narrowing for and and or operators with nullable types, fixing several scenarios where types were not correctly inferred. The changes include adding special handling for x or {} and x and y patterns, and refining how self-assignments are handled in flow analysis. The introduction of comprehensive tests for these cases is a great addition.
My review focuses on improving code clarity and maintainability. I've suggested refactoring a complex if/else if chain to a more readable match statement, and using idiomatic Rust iterator patterns to reduce code duplication and improve conciseness in a few places. Overall, this is a solid improvement to the type inference engine.
crates/emmylua_code_analysis/src/semantic/infer/narrow/get_type_at_flow.rs
Outdated
Show resolved
Hide resolved
crates/emmylua_code_analysis/src/semantic/infer/narrow/get_type_at_flow.rs
Outdated
Show resolved
Hide resolved
crates/emmylua_code_analysis/src/semantic/infer/narrow/narrow_type/false_or_nil_type.rs
Show resolved
Hide resolved
dc41516 to
0d3e222
Compare
Fixes EmmyLuaLs#921 Improve type narrowing for the 'and' and 'or' operators when used with nullable types and table/literal expressions. The analyzer was not properly narrowing types in three scenarios: 1. 'opts or {}' where opts: Opts? was not narrowing to Opts 2. Self-assignment 'opts = opts or {}' resulted in type 'table' instead of Opts 3. 'opts.a and { 'a' }' where opts.a: string? was not properly typed - Added special case in special_or_rule() to detect empty table expressions - When right side is {} and left type is nullable class/table, remove nil - Result: 'opts or {}' where opts: Opts? correctly narrows to Opts - Modified get_type_at_assign_stat() to distinguish DocType from InferType - Only use explicit ---@type annotations, not inferred types - Allows flow-based narrowing when no explicit annotation exists - Result: 'opts = opts or {}' now correctly narrows opts to Opts - Created new file with special_and_rule() for 'and' operator patterns - Moved infer_binary_expr_and() from mod.rs to new file - For 'x and y' where x is nullable: result is falsy_part(x) | y - Examples: - string? and 'value' → 'value' | nil - boolean? and 'value' → 'value' | false | nil - Added union handling to properly extract falsy parts - For boolean: returns BooleanConst(false) - For boolean?: returns false | nil - For string?: returns nil - Recursively processes union members --- This commit was created with AI assistance (OpenCode/Claude).
0d3e222 to
e76eb70
Compare
Fixes #921
Improve type narrowing for the 'and' and 'or' operators when used with
nullable types and table/literal expressions.
The analyzer was not properly narrowing types in three scenarios:
opts or {}whereopts: Opts?was not narrowing toOptsopts = opts or {}resulted in typetableinstead of Optsopts.a and { 'a' }whereopts.a: string?was not properly typedAdded special case in special_or_rule() to detect empty table expressions
When right side is {} and left type is nullable class/table, remove nil
Result:
opts or {}whereopts: Opts?correctly narrows to OptsModified get_type_at_assign_stat() to distinguish DocType from InferType
Only use explicit
---@typeannotations, not inferred typesAllows flow-based narrowing when no explicit annotation exists
Result:
opts = opts or {}now correctly narrows opts to OptsCreated new file with special_and_rule() for 'and' operator patterns
Moved infer_binary_expr_and() from mod.rs to new file
For 'x and y' where x is nullable: result is falsy_part(x) | y
Examples:
Added union handling to properly extract falsy parts
For boolean: returns BooleanConst(false)
For boolean?: returns false | nil
For string?: returns nil
Recursively processes union members
This commit was created with AI assistance (OpenCode/Claude).