fix(flow): narrow to assigned function signatures#950
fix(flow): narrow to assigned function signatures#950lewis6991 wants to merge 1 commit intoEmmyLuaLs:mainfrom
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 addresses a type flow issue in the code analysis system by enhancing the type narrowing logic. Previously, the system did not correctly infer types when a function-typed value was assigned to a variable whose type included a function signature. The changes introduce a specific rule to ensure that such assignments lead to the correct narrowing of the variable's type to the assigned function's signature. A new test case has been added to validate this improved type inference behavior. 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. Changelog
Activity
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.
Code Review
This pull request adds logic to handle type narrowing for function signatures. The change itself seems correct for preserving declared signatures on assignment. However, the accompanying test case appears to exercise DocFunction types, not Signature types, and seems to require a different narrowing behavior (narrowing to the assigned function's type rather than preserving the declared type). It would be good to clarify if this is intended and if a corresponding change for DocFunction is also needed.
| LuaType::Signature(_) => { | ||
| if real_source_ref.is_function() { | ||
| return Some(target.clone()); | ||
| } | ||
| } |
There was a problem hiding this comment.
This change correctly preserves the declared Signature type when a function is assigned, which is great for cases where you want to maintain the API contract.
However, the new test test_doc_function_assignment_narrowing uses a DocFunction from a type annotation (--- @type integer|fun():string), not a Signature.
The test expects the type to be narrowed to the assigned function's type (fun() -> nil), which implies that for DocFunction, the narrowing logic should probably use the source type (from the RHS of the assignment). This is different from the logic for Signature where you are correctly returning target to preserve the declaration.
Could you clarify this behavior? It seems a similar change might be needed for DocFunction but with a different implementation (returning source instead of target).
There was a problem hiding this comment.
In narrow_down_type, source is the antecedent/declared type and target is the assignment RHS type. For a closure RHS we infer Signature, and we intentionally return the target signature even when the antecedent is a DocFunction. That matches the runtime value (the assigned closure) and is why the new test fails without this change. So no extra DocFunction-specific branch is needed here; is_function() already covers it.
treat signature targets as compatible when source is function-typed
7298008 to
e2eddfc
Compare
treat signature targets as compatible when source is function-typed