-
Notifications
You must be signed in to change notification settings - Fork 57
fix(typecheck): prefer declared globals for index member ownership #953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
When setting a member owner for an index expression with a name prefix (e.g. `table.unpack`), prefer the declared global type cache over the inferred prefix type. This keeps stdlib members attached to their type defs and avoids losing them after casts (like `---@cast table unknown`). Skip the override when the name is shadowed by a local.
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 enhances the type-checking accuracy for Lua index expressions by adjusting how member ownership is resolved. It ensures that globally declared types are consistently used for name prefixes, thereby preserving type integrity for standard library functions even when type casts are applied, while also correctly handling cases where global names are shadowed by local variables. 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
|
|
Not sure if this is something we care about, buts it's something codex picked up. |
There was a problem hiding this 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 a well-reasoned fix to prefer declared global types for index member ownership, which is particularly useful for maintaining type information for standard library members after casting. The logic is sound, and the new test case effectively validates the change. I have one suggestion to refactor a portion of the new logic to improve its readability and make it more idiomatic.
| let mut explicit_type = None; | ||
| if let Some(name) = name_expr.get_name_text() { | ||
| // Avoid attaching members to stdlib globals when a local shadows the name. | ||
| let is_shadowed = analyzer | ||
| .db | ||
| .get_decl_index() | ||
| .get_decl_tree(&file_id) | ||
| .and_then(|tree| tree.find_local_decl(&name, name_expr.get_position())) | ||
| .map(|decl| decl.is_local() || decl.is_implicit_self()) | ||
| .unwrap_or(false); | ||
| if !is_shadowed | ||
| && let Some(decl_ids) = | ||
| analyzer.db.get_global_index().get_global_decl_ids(&name) | ||
| { | ||
| // Pick the first resolvable global type cache as the owner type. | ||
| for decl_id in decl_ids { | ||
| if let Some(type_cache) = analyzer | ||
| .db | ||
| .get_type_index() | ||
| .get_type_cache(&(*decl_id).into()) | ||
| { | ||
| explicit_type = Some(type_cache.as_type().clone()); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code for determining the explicit_type can be refactored to be more concise and idiomatic by using a more functional style with and_then and find_map. This change will reduce nesting, remove the need for a mutable variable, and improve the overall readability of the logic.
let explicit_type = name_expr.get_name_text().and_then(|name| {
// Avoid attaching members to stdlib globals when a local shadows the name.
let is_shadowed = analyzer
.db
.get_decl_index()
.get_decl_tree(&file_id)
.and_then(|tree| tree.find_local_decl(&name, name_expr.get_position()))
.map(|decl| decl.is_local() || decl.is_implicit_self())
.unwrap_or(false);
if is_shadowed {
None
} else {
analyzer
.db
.get_global_index()
.get_global_decl_ids(&name)
.and_then(|decl_ids| {
// Pick the first resolvable global type cache as the owner type.
decl_ids.iter().find_map(|decl_id| {
analyzer
.db
.get_type_index()
.get_type_cache(&(*decl_id).into())
.map(|type_cache| type_cache.as_type().clone())
})
})
}
});
When setting a member owner for an index expression with a name prefix
(e.g.
table.unpack), prefer the declared global type cache over theinferred prefix type. This keeps stdlib members attached to their type
defs and avoids losing them after casts (like
---@cast table unknown).Skip the override when the name is shadowed by a local.