-
Notifications
You must be signed in to change notification settings - Fork 118
feat(tyck): contextual typing for destructuring and tuple/array flows #2386
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?
feat(tyck): contextual typing for destructuring and tuple/array flows #2386
Conversation
Summary of ChangesHello @hongjr03, 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 advances the type checking capabilities by implementing contextual type propagation across various language constructs. It focuses on improving type inference for destructuring, tuple/array flows, and field access, which leads to more precise type suggestions and error detection. The changes also refine how paths are typed in 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. 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
|
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 significant enhancements to the type checking system, enabling contextual type propagation for destructuring assignments and flows involving tuples and arrays. Key changes include back-propagation of types for field access (e.g., through dict.at("key")), improved type inference for for loops, and constant folding for string concatenation. The changes are extensive and well-supported by new test cases.
I have identified a couple of areas for improvement. One is a potential bug in destructuring logic that could lead to loss of type information when spreads are used. The other is a suggestion to generalize the new for loop type inference to support more complex patterns beyond simple identifiers.
Overall, this is a great feature addition that will improve the accuracy of type analysis.
| if trailing_pos_after_spread { | ||
| has_pos = false; | ||
| pos.clear(); | ||
| } |
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 incorrectly clears all inferred positional pattern types if a spread is followed by another positional pattern (e.g., in (a, ..rest, b)). This results in losing the type information for prefix patterns like a. According to the comment on line 443, the intention is to keep the prefix mapping, so this block should be removed to prevent discarding valid type information.
| if matches!(for_loop.pattern.as_ref(), Pattern::Simple(..)) { | ||
| self.constrain(&iter, &Ty::Array(pattern.clone().into())); | ||
|
|
||
| match &iter { | ||
| Ty::Array(elem) => self.constrain(elem, &pattern), | ||
| Ty::Tuple(elems) => { | ||
| for elem in elems.iter() { | ||
| self.constrain(elem, &pattern); | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } |
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.
The type constraint logic for for loops is currently limited to Pattern::Simple. This prevents type propagation for more complex patterns like destructuring (k, v), which is a common use case. The logic for constraining the iterable and its elements seems general enough to be applied to all pattern types, which would make this feature more powerful and consistent.
Consider removing the if matches!(...) condition to apply the type constraints for all for loop patterns.
self.constrain(&iter, &Ty::Array(pattern.clone().into()));
match &iter {
Ty::Array(elem) => self.constrain(elem, &pattern),
Ty::Tuple(elems) => {
for elem in elems.iter() {
self.constrain(elem, &pattern);
}
}
_ => {}
}f9e0144 to
41a23d6
Compare
41a23d6 to
729f5fa
Compare
729f5fa to
5f625da
Compare
- Tighten dict.at("key") rewrite to 1 positional arg\n- Use shared const-string-key extractor for NamedRt/Args\n- Refactor keyed dict analysis to reduce nesting
5f625da to
73a8f7a
Compare
Propagates contextual types through destructuring and tuple/array constraints, including field-access backprop. Adds tuple destructuring and include for-loop fixtures/snapshots.
Depends on #2385.