-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: transform return into break when inlining functions
#21908
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
Open
Albab-Hasan
wants to merge
7
commits into
rust-lang:master
Choose a base branch
from
Albab-Hasan:inline-call-early-return
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+318
−3
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
332311c
transform return expressions into labeled breaks when inlining functions
Albab-Hasan 3c1fc4e
use HIR-based return point analysis for inline call assist
Albab-Hasan a63e18f
fix formatting
Albab-Hasan 17c71d1
address review feedback on return_points and inline call assist
Albab-Hasan dd5d4db
return Vec<ast::ReturnExpr> from return_points instead of Vec<TextRange>
Albab-Hasan 87ee6d7
return Vec<InFile<ast::ReturnExpr>> from return_points
Albab-Hasan a594047
use return_points() in inline call assist instead of separate tree walk
Albab-Hasan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2824,6 +2824,35 @@ impl Function { | |
| } | ||
| } | ||
|
|
||
| /// Returns the return expressions in this function's body that belong to the | ||
| /// function itself (not inside closures or async blocks which create their own | ||
| /// return scope). Uses HIR analysis, which handles returns inside macro expansions. | ||
| pub fn return_points(self, db: &dyn HirDatabase) -> Vec<InFile<AstPtr<ast::ReturnExpr>>> { | ||
| let func_id: FunctionId = match self.id { | ||
| AnyFunctionId::FunctionId(id) => id, | ||
| _ => return vec![], | ||
| }; | ||
| let (body, source_map) = Body::with_source_map(db, func_id.into()); | ||
| let mut returns = vec![]; | ||
| let mut to_visit = vec![body.root_expr()]; | ||
|
Contributor
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. A recursion is simpler IMO than an out-of-line traversal. |
||
| while let Some(expr_id) = to_visit.pop() { | ||
| let expr = &body[expr_id]; | ||
| // Closures and async blocks create their own return scope | ||
| match expr { | ||
| Expr::Closure { .. } | Expr::Async { .. } => continue, | ||
| _ => {} | ||
| } | ||
| if matches!(expr, Expr::Return { .. }) | ||
| && let Ok(source) = source_map.expr_syntax(expr_id) | ||
| && let Some(ptr) = source.value.cast::<ast::ReturnExpr>() | ||
| { | ||
| returns.push(InFile::new(source.file_id, ptr)); | ||
| } | ||
| body.walk_child_exprs(expr_id, |child| to_visit.push(child)); | ||
| } | ||
| returns | ||
| } | ||
|
|
||
| pub fn as_proc_macro(self, db: &dyn HirDatabase) -> Option<Macro> { | ||
| let AnyFunctionId::FunctionId(id) = self.id else { | ||
| return None; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
No need to say it's using HIR analysis, it's obvious for any function in this file.