-
Notifications
You must be signed in to change notification settings - Fork 54
New mechanism for ---@return_cast #810
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -228,17 +228,50 @@ fn get_type_at_call_expr_by_signature_self( | |
| }; | ||
|
|
||
| let signature_root = syntax_tree.get_chunk_node(); | ||
| let Some(cast_op_type) = signature_cast.cast.to_node(&signature_root) else { | ||
| return Ok(ResultTypeOrContinue::Continue); | ||
|
|
||
| // Choose the appropriate cast based on condition_flow and whether fallback exists | ||
| let result_type = match condition_flow { | ||
| InferConditionFlow::TrueCondition => { | ||
| let Some(cast_op_type) = signature_cast.cast.to_node(&signature_root) else { | ||
| return Ok(ResultTypeOrContinue::Continue); | ||
| }; | ||
| cast_type( | ||
| db, | ||
| signature_id.get_file_id(), | ||
| cast_op_type, | ||
| antecedent_type, | ||
| condition_flow, | ||
| )? | ||
| } | ||
| InferConditionFlow::FalseCondition => { | ||
| // Use fallback_cast if available, otherwise use the default behavior | ||
| if let Some(fallback_cast_ptr) = &signature_cast.fallback_cast { | ||
| let Some(fallback_op_type) = fallback_cast_ptr.to_node(&signature_root) else { | ||
| return Ok(ResultTypeOrContinue::Continue); | ||
| }; | ||
| cast_type( | ||
| db, | ||
| signature_id.get_file_id(), | ||
| fallback_op_type, | ||
| antecedent_type.clone(), | ||
| InferConditionFlow::TrueCondition, // Apply fallback as force cast | ||
| )? | ||
| } else { | ||
| // Original behavior: remove the true type from antecedent | ||
| let Some(cast_op_type) = signature_cast.cast.to_node(&signature_root) else { | ||
| return Ok(ResultTypeOrContinue::Continue); | ||
| }; | ||
| cast_type( | ||
| db, | ||
| signature_id.get_file_id(), | ||
| cast_op_type, | ||
| antecedent_type, | ||
| condition_flow, | ||
| )? | ||
| } | ||
| } | ||
| }; | ||
|
Comment on lines
+232
to
273
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of logic for choosing and applying the type cast is duplicated in |
||
|
|
||
| let result_type = cast_type( | ||
| db, | ||
| signature_id.get_file_id(), | ||
| cast_op_type, | ||
| antecedent_type, | ||
| condition_flow, | ||
| )?; | ||
| Ok(ResultTypeOrContinue::Result(result_type)) | ||
| } | ||
|
|
||
|
|
@@ -304,17 +337,50 @@ fn get_type_at_call_expr_by_signature_param_name( | |
| }; | ||
|
|
||
| let signature_root = syntax_tree.get_chunk_node(); | ||
| let Some(cast_op_type) = signature_cast.cast.to_node(&signature_root) else { | ||
| return Ok(ResultTypeOrContinue::Continue); | ||
|
|
||
| // Choose the appropriate cast based on condition_flow and whether fallback exists | ||
| let result_type = match condition_flow { | ||
| InferConditionFlow::TrueCondition => { | ||
| let Some(cast_op_type) = signature_cast.cast.to_node(&signature_root) else { | ||
| return Ok(ResultTypeOrContinue::Continue); | ||
| }; | ||
| cast_type( | ||
| db, | ||
| signature_id.get_file_id(), | ||
| cast_op_type, | ||
| antecedent_type, | ||
| condition_flow, | ||
| )? | ||
| } | ||
| InferConditionFlow::FalseCondition => { | ||
| // Use fallback_cast if available, otherwise use the default behavior | ||
| if let Some(fallback_cast_ptr) = &signature_cast.fallback_cast { | ||
| let Some(fallback_op_type) = fallback_cast_ptr.to_node(&signature_root) else { | ||
| return Ok(ResultTypeOrContinue::Continue); | ||
| }; | ||
| cast_type( | ||
| db, | ||
| signature_id.get_file_id(), | ||
| fallback_op_type, | ||
| antecedent_type.clone(), | ||
| InferConditionFlow::TrueCondition, // Apply fallback as force cast | ||
| )? | ||
| } else { | ||
| // Original behavior: remove the true type from antecedent | ||
| let Some(cast_op_type) = signature_cast.cast.to_node(&signature_root) else { | ||
| return Ok(ResultTypeOrContinue::Continue); | ||
| }; | ||
| cast_type( | ||
| db, | ||
| signature_id.get_file_id(), | ||
| cast_op_type, | ||
| antecedent_type, | ||
| condition_flow, | ||
| )? | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let result_type = cast_type( | ||
| db, | ||
| signature_id.get_file_id(), | ||
| cast_op_type, | ||
| antecedent_type, | ||
| condition_flow, | ||
| )?; | ||
| Ok(ResultTypeOrContinue::Result(result_type)) | ||
| } | ||
|
|
||
|
|
||
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.
There's some code duplication when binding the types for the
trueandfalseconditions. This can be refactored into a local closure to improve readability and maintainability. Usingop_types.get(1).map(...)is also more idiomatic than checking length and then indexing.