Skip to content

fix: type guard narrowing for union functions#949

Open
lewis6991 wants to merge 1 commit intoEmmyLuaLs:mainfrom
lewis6991:issue696
Open

fix: type guard narrowing for union functions#949
lewis6991 wants to merge 1 commit intoEmmyLuaLs:mainfrom
lewis6991:issue696

Conversation

@lewis6991
Copy link
Collaborator

Fixes #696

@gemini-code-assist
Copy link

Summary of Changes

Hello @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 resolves a bug in the type inference system where type guards for union types, particularly those involving functions, were not correctly narrowing the types. Previously, this could lead to erroneous ParamTypeMismatch diagnostics. The changes introduce a crucial check to ensure that if a union type is narrowed down to an empty set of types, the narrowing process correctly indicates no valid type, thereby preventing incorrect type assertions and improving the accuracy of the code analysis.

Highlights

  • Fix Type Guard Narrowing for Union Functions: This pull request addresses an issue where type guards for union types, specifically those involving functions, were not correctly narrowing the types. This led to false positive ParamTypeMismatch diagnostics.
  • Improved Union Type Narrowing Logic: The type narrowing logic has been enhanced to correctly handle scenarios where a union type becomes empty after narrowing. A check if union_types.is_empty() { return None; } was added to ensure that if all types in a union are filtered out, the result correctly indicates no valid type.
  • New Test Case Added: A dedicated test case, test_issue_696, has been introduced to reproduce and validate the fix for this specific type guard narrowing problem, ensuring no regressions occur.

🧠 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
  • crates/emmylua_code_analysis/src/diagnostic/test/param_type_check_test.rs
    • Added test_issue_696 to specifically test the scenario where a union type containing a function is narrowed by a type guard, ensuring no ParamTypeMismatch is reported when the types are compatible.
  • crates/emmylua_code_analysis/src/semantic/infer/narrow/narrow_type/mod.rs
    • Introduced a check if union_types.is_empty() { return None; } after filtering union types. This ensures that if all types in a union are filtered out during narrowing, the result is None, indicating no valid type, rather than an an empty union. This applies to both LuaType::Union and LuaType::MultiLineUnion processing.
Activity
  • The pull request was created by lewis6991 to address issue bad type narrow with type() #696, which describes the type guard narrowing problem for union functions.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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 correctly fixes a bug in type narrowing for union types. Previously, when narrowing a union type where no member types matched the target, the operation would incorrectly result in a nil type instead of failing. This could lead to incorrect type inference downstream. The fix ensures that the narrowing operation returns None in such cases, which is the correct behavior. A new test case is added to verify the fix and prevent future regressions. The changes are logical and well-tested. I have added a couple of minor suggestions to improve code conciseness by using more idiomatic Rust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bad type narrow with type()

1 participant