Skip to content

fix: select(n, func()) correctly narrows type when func returns multiple values - #962

Merged
CppCXY merged 3 commits into
EmmyLuaLs:mainfrom
nickvonkaenel:fix/select-multi-return-type
Feb 26, 2026
Merged

CppCXY merged 3 commits into
EmmyLuaLs:mainfrom
nickvonkaenel:fix/select-multi-return-type

Conversation

@nickvonkaenel

Copy link
Copy Markdown
Contributor

Summary

  • select(2, func()) where func() returns (integer, string) was resolving to integer|string instead of string
  • Root cause: when a multi-return function call was the sole variadic argument, variadic_tpl_pattern_match bound the template T to the entire Variadic(Multi([integer, string])) wrapper rather than expanding it
  • This caused instantiate_variadic_type to produce a doubly-nested Variadic(Base(Variadic(Multi(...)))), which bypassed the index-slicing logic in instantiate_select_call

Fix

In variadic_tpl_pattern_match (tpl_pattern/mod.rs), when matching a TplRef variadic template against a single argument that is itself a Variadic, expand it instead of storing it as-is:

  • Variadic(Multi([T1, T2, ...])) → insert_multi_types([T1, T2, ...]) so T... instantiates to Variadic(Multi([T1, T2, ...]))
  • Variadic(Base(T)) → insert_multi_base(T) preserving unbounded variadic semantics

Test plan

  • Added test case select(n, multi_ret()) where multi_ret() returns (integer, string), asserting result is string
  • All existing test_select_type cases still pass (variadic ..., literal args, select('#', ...))
  • Full test suite passes (570+ tests)

🤖 Generated with Claude Code

…ple values

When a multi-return function call was the sole variadic argument to
select(), the T... template was binding T to the whole Variadic(Multi(...))
wrapper instead of expanding it. This caused instantiate_variadic_type to
produce a doubly-nested Variadic(Base(Variadic(Multi(...)))) which bypassed
the index-slicing logic in instantiate_select_call, returning the wrong type.

Fix: in variadic_tpl_pattern_match, when the single argument is itself a
Variadic(Multi([T1, T2, ...])), expand it via insert_multi_types so the
template receives all return values rather than the Variadic wrapper.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @nickvonkaenel, 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 critical type inference bug within the select function when it interacts with functions returning multiple values. The fix ensures that the type system correctly narrows down the return type based on the select index, preventing incorrect union types and improving the accuracy of static analysis for Lua code.

Highlights

  • Type Narrowing Fix: Resolved an issue where select(2, func()) with a multi-return function (func() returning (integer, string)) incorrectly resolved to integer|string instead of string.
  • Root Cause Identified: The problem stemmed from variadic_tpl_pattern_match binding the template T to the entire Variadic(Multi([integer, string])) wrapper when a multi-return function call was the sole variadic argument, instead of expanding it.
  • Template Matching Logic Update: Modified variadic_tpl_pattern_match to correctly expand Variadic types when matching a TplRef variadic template against a single argument that is itself a Variadic, ensuring proper type expansion and instantiation.

🧠 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/semantic/generic/test.rs
    • Added a new test case to validate the correct type narrowing for select(n, func()) when func() returns multiple values.
  • crates/emmylua_code_analysis/src/semantic/generic/tpl_pattern/mod.rs
    • Implemented logic within variadic_tpl_pattern_match to expand Variadic types when a single variadic argument is a multi-return function call, ensuring proper template instantiation.
Activity
  • A new test case select(n, multi_ret()) was added to verify the fix for multi-return functions.
  • All existing test_select_type cases, including those for variadic ..., literal arguments, and select('#', ...), were confirmed to still pass.
  • The full test suite, comprising over 570 tests, passed successfully after the changes.
  • The pull request was generated using Claude Code.
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. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 effectively addresses a type narrowing issue with select(n, func()) when func returns multiple values. The added test case is well-crafted and specifically targets the fixed scenario. The implementation changes in variadic_tpl_pattern_match are logical and correctly handle the unpacking of variadic arguments. Overall, this is a solid fix. I have one minor suggestion to improve code consistency.

…mod.rs

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@CppCXY

CppCXY commented Feb 25, 2026

Copy link
Copy Markdown
Member

please fix the check

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@nickvonkaenel

Copy link
Copy Markdown
Contributor Author

Should be fixed now

@CppCXY
CppCXY merged commit beabf3f into EmmyLuaLs:main Feb 26, 2026
22 checks passed
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.

2 participants