Skip to content

fix: expr-key member registration for table inference#951

Open
lewis6991 wants to merge 1 commit intoEmmyLuaLs:mainfrom
lewis6991:codex/enum-pairs-key
Open

fix: expr-key member registration for table inference#951
lewis6991 wants to merge 1 commit intoEmmyLuaLs:mainfrom
lewis6991:codex/enum-pairs-key

Conversation

@lewis6991
Copy link
Collaborator

Register expression-key table members early so pairs() and generic table
inference can see them even without explicit table generics.

Extend tpl pattern matching to handle expr-key member types.

Fixes #642

Register expression-key table members early so pairs() and generic table
inference can see them even without explicit table generics.

Extend tpl pattern matching to handle expr-key member types.

Fixes EmmyLuaLs#642
@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 significantly enhances the type inference capabilities for Lua tables, particularly those utilizing expression keys. By registering these members earlier and improving template pattern matching, the system can now more accurately infer types for table iterations and member access, addressing a known limitation and improving the overall robustness of the type analysis.

Highlights

  • Early Registration of Expression-Key Members: Expression-key table members are now registered earlier in the analysis process. This ensures that type inference mechanisms, such as pairs() and generic table inference, can correctly identify and utilize these members even when explicit table generics are not provided.
  • Enhanced Template Pattern Matching: The template pattern matching logic has been extended to properly handle and recognize expr-key member types. This improves the accuracy of type resolution for tables using dynamic keys.
  • Improved Type Inference for Tables: New test cases demonstrate that the system now correctly infers types for pairs() iterations over tables with enum-based or string-expression keys, and accurately resolves member access for tables using expression keys.

🧠 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/compilation/analyzer/lua/stats.rs
    • Imported necessary types: LuaIndexKey, LuaTableExpr, LuaMemberKey, LuaMember, LuaMemberFeature.
    • Added a new private function register_expr_key_member to proactively register members defined with expression keys.
    • Integrated the register_expr_key_member call into the analyze_table_field function to ensure early registration of these members.
  • crates/emmylua_code_analysis/src/compilation/test/for_range_var_infer_test.rs
    • Added use std::collections::HashSet; for new test cases.
    • Introduced test_enum_key_pairs to verify correct type inference for pairs() when iterating over tables with enum-based expression keys.
    • Added test_pairs_expr_key_type to confirm accurate type inference for pairs() with string-expression keys.
  • crates/emmylua_code_analysis/src/compilation/test/member_infer_test.rs
    • Added test_table_expr_key_string to validate type inference for table member access using string expression keys.
    • Included test_table_expr_key_doc_const to ensure correct type inference for table member access with documented constant string expression keys.
  • crates/emmylua_code_analysis/src/semantic/generic/tpl_pattern/mod.rs
    • Modified the table_generic_tpl_pattern_member_owner_match function to explicitly handle LuaMemberKey::ExprType(typ), extending its pattern matching capabilities for expression-key types.
Activity
  • No human activity has been recorded on this pull request since its creation.
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 provides a solid fix for inferring expression-key table members. The new register_expr_key_member function is well-designed, registering these members early to make them available for pairs() and generic table inference. The extension of template pattern matching to handle ExprType member keys is a necessary and correct addition. The new tests are comprehensive and effectively validate the fix across different scenarios. The code is clean, the logic is sound, and the changes significantly improve type inference capabilities. I have no issues to report.

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.

cannot pairs on a table that uses enum values for keys

1 participant