fix(generic): stabilize higher-order callable overload inference#990
fix(generic): stabilize higher-order callable overload inference#990lewis6991 wants to merge 2 commits intoEmmyLuaLs:mainfrom
Conversation
Summary of ChangesHello, 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 refactors the type inference logic for higher-order callable types, particularly when they involve unions or intersections. The system now employs a more robust overload resolution strategy, where argument types are used to select the most appropriate callable signature from a set of candidates, thereby yielding a more precise return type. This change improves the accuracy of type analysis for complex function definitions and calls. 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. Footnotes
|
|
Warning Gemini is experiencing higher than usual traffic and was unable to create the review. Please try again in a few hours by commenting |
4705986 to
be9caeb
Compare
|
please fix conflicts |
be9caeb to
963c3ce
Compare
7b80f2a to
9cf7e55
Compare
75166d3 to
3b763c6
Compare
|
please fix conflicts |
Collect callable overload groups through aliases, unions, intersections, and signatures. Expose the argument-shape matcher so generic return inference and call inference can resolve candidates through the same path. Assisted-by: Codex
3b763c6 to
0c65700
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request significantly improves callable return type inference, particularly for complex types involving unions, intersections, and generic overloads. Key enhancements include the introduction of collect_callable_overload_groups to flatten nested callable structures and filter_callable_overloads_by_call_args to more accurately narrow down applicable overloads based on argument shapes. The logic for matching template patterns was also refined to handle unknown arguments more gracefully. Feedback was provided regarding a performance optimization in infer_union, where argument types are currently inferred multiple times within a loop instead of being cached.
| for ty in union.into_vec() { | ||
| match ty { | ||
| LuaType::Signature(signature_id) => { | ||
| if let Some(signature) = db.get_signature_index().get(&signature_id) { | ||
| // 处理 overloads | ||
| let overloads = if signature.is_generic() { | ||
| signature | ||
| .overloads | ||
| .iter() | ||
| .map(|func| { | ||
| Ok(Arc::new(instantiate_func_generic( | ||
| db, | ||
| cache, | ||
| func, | ||
| call_expr.clone(), | ||
| )?)) | ||
| }) | ||
| .collect::<Result<Vec<_>, _>>()? | ||
| } else { | ||
| signature.overloads.clone() | ||
| }; | ||
| all_overloads.extend(overloads); | ||
|
|
||
| // 处理 signature 本身的函数类型 | ||
| let mut fake_doc_function = LuaFunctionType::new( | ||
| signature.async_state, | ||
| signature.is_colon_define, | ||
| signature.is_vararg, | ||
| signature.get_type_params(), | ||
| signature.get_return_type(), | ||
| ); | ||
| if signature.is_generic() { | ||
| fake_doc_function = instantiate_func_generic( | ||
| db, | ||
| cache, | ||
| &fake_doc_function, | ||
| call_expr.clone(), | ||
| )?; | ||
| let mut overload_groups = Vec::new(); | ||
| collect_callable_overload_groups(db, &ty, &mut overload_groups)?; | ||
| for overloads in overload_groups { | ||
| let compatible_overloads = filter_callable_overloads_by_call_args( | ||
| db, | ||
| cache, | ||
| overloads.clone(), | ||
| &call_expr, | ||
| args_count, | ||
| true, | ||
| )?; | ||
| if compatible_overloads.is_empty() { | ||
| fallback_overloads.extend(overloads); | ||
| continue; | ||
| } | ||
|
|
||
| let contains_tpl = compatible_overloads.iter().any(|func| func.contain_tpl()); | ||
| match resolve_signature( | ||
| db, | ||
| cache, | ||
| compatible_overloads, | ||
| call_expr.clone(), | ||
| contains_tpl, | ||
| args_count, | ||
| ) { | ||
| Ok(func) => { | ||
| returns.push(func.get_ret().clone()); | ||
| if first_func.is_none() { | ||
| first_func = Some(func); | ||
| } | ||
| base_signatures.push(Arc::new(fake_doc_function)); | ||
| } | ||
| Err(InferFailReason::RecursiveInfer) => { | ||
| return Err(InferFailReason::RecursiveInfer); | ||
| } | ||
| Err(reason) if reason.is_need_resolve() => { | ||
| if need_resolve.is_none() { | ||
| need_resolve = Some(reason); | ||
| } | ||
| } | ||
| Err(_) => {} | ||
| } | ||
| LuaType::DocFunction(func) => { | ||
| let func_to_push = if func.contain_tpl() { | ||
| Arc::new(instantiate_func_generic( | ||
| db, | ||
| cache, | ||
| &func, | ||
| call_expr.clone(), | ||
| )?) | ||
| } else { | ||
| func.clone() | ||
| }; | ||
| base_signatures.push(func_to_push); | ||
| } | ||
| _ => {} | ||
| } | ||
| } |
There was a problem hiding this comment.
In infer_union, argument types are inferred multiple times because filter_callable_overloads_by_call_args and resolve_signature are called within a loop, and both perform their own argument inference. For large unions or complex argument lists, this redundancy can impact performance. It would be more efficient to infer the argument types once at the start of infer_union and pass them to the helper functions.
Infer higher-order callable returns from compatible overload members instead of a single order-dependent branch. Keep unknown returns when the argument shape is unknown, treat function as an erased callable fallback, and union compatible callable-union returns. Assisted-by: Codex
0c65700 to
538cec3
Compare
Improve overload inference for callable values.
The main rule is: match callable candidates by the actual argument shape before using their return types. That makes higher-order callback inference and direct callable-union calls less order-dependent.
This also clarifies the broad callable cases:
unknownreturns stay unknown instead of falling through to a concrete overload.functionis treated as an erased callable fallback.Regression tests cover
apply-style callbacks, direct callable unions, andpcallreturn overloads.