Skip to content

Conversation

@xuhuanzy
Copy link
Member

No description provided.

@gemini-code-assist
Copy link

Summary of Changes

Hello @xuhuanzy, 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 refines the handling of generic type constraints within the EmmyLua code analysis engine. By integrating type constraints directly into generic type representations and updating the type inference and checking mechanisms, the system can now more accurately understand and validate generic type usage, leading to more robust code analysis. It also introduces new fundamental types for better type system expressiveness and includes a new test case to ensure the correct application of these enhancements.

Highlights

  • Enhanced Generic Type Constraints: The type system now explicitly stores and utilizes generic type constraints (Option<LuaType>) within GenericTpl and LuaStringTplType structures, allowing for more precise type definitions and checks.
  • Improved Type Inference and Checking: Type inference logic and generic constraint mismatch checks have been updated to correctly propagate and apply these constraints, prioritizing explicit constraints during type validation and when no substitution is found.
  • New Built-in Types: The builtin.lua resource now includes definitions for ---@class unknown and ---@class never, expanding the expressiveness of the type system.
  • Refactored Generic Parameter Storage: The TagGenericParams struct now stores the full GenericParam object alongside its index, enabling better access to generic parameter details, including constraints.
  • Hashing Optimization for Arc Types: The Hash implementation for LuaType::TplRef, LuaType::StrTplRef, and LuaType::ConstTplRef has been adjusted to hash the pointer of the Arc for consistency and potential performance benefits.
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 introduces support for generic constraints, allowing them to be used as default types when a generic argument is not specified. This is achieved by adding a constraint field to GenericTpl and related types, and updating the type inference and instantiation logic accordingly. The changes are logical and implement a valuable feature.

My review has identified a high-severity correctness issue regarding the Hash implementation for LuaType, which violates the Hash contract and could lead to incorrect behavior. Additionally, I've pointed out a couple of medium-severity maintainability issues where comments were written in Chinese within a predominantly English codebase.

Comment on lines +180 to +187
LuaType::TplRef(a) => {
let ptr = Arc::as_ptr(a);
(32, ptr).hash(state)
}
LuaType::StrTplRef(a) => {
let ptr = Arc::as_ptr(a);
(33, ptr).hash(state)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The Hash implementation for Arc-wrapped types like TplRef and StrTplRef uses pointer hashing (Arc::as_ptr). However, the PartialEq implementation for LuaType compares the contained values, not the pointers. This violates the Hash contract, which requires that if a == b, then hash(a) == hash(b).

This can lead to subtle bugs. For example, LuaType::from_vec uses a HashSet<LuaType> to deduplicate types. With this Hash implementation, two equal LuaType values that are allocated separately will have different hashes and will not be considered duplicates by the HashSet. This can result in unions with duplicate types (e.g., string | string).

This seems to be a pre-existing issue in the codebase that is being extended in this PR. A proper fix would be complex, likely involving a recursive Hash implementation with cycle detection or interning all Arc-wrapped types. Given the potential for incorrect behavior, this is a significant issue to be aware of.

let result_ty = ws.expr_ty("result");
assert_eq!(result_ty, ws.ty("number"));
}
// 类的默认泛型约束暂时不支持

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This comment is in Chinese. For consistency with the rest of the codebase, which is primarily in English, please write comments in English.

Suggested change
// 类的默认泛型约束暂时不支持
// Default generic constraints for classes are not yet supported.

@CppCXY CppCXY merged commit a3a27a6 into EmmyLuaLs:main Nov 21, 2025
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