-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix: Handle Self replacement contextually in inline assists #20049
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
phyBrackets
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
phyBrackets:fix/inline-self-replacement-context-aware
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.
Open
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -248,6 +248,47 @@ fn preorder_rev(item: &SyntaxNode) -> impl Iterator<Item = SyntaxNode> { | |
x.into_iter().rev() | ||
} | ||
|
||
fn is_path_in_expression_context(path: &ast::Path) -> bool { | ||
let mut current = path.syntax().parent(); | ||
while let Some(node) = current { | ||
// Expression contexts where we need bare type names | ||
if let Some(call_expr) = ast::CallExpr::cast(node.clone()) { | ||
if let Some(expr) = call_expr.expr() { | ||
if expr.syntax().text_range().contains_range(path.syntax().text_range()) { | ||
return true; | ||
} | ||
} | ||
} | ||
if ast::PathExpr::cast(node.clone()).is_some() { | ||
return true; | ||
} | ||
if let Some(record_expr) = ast::RecordExpr::cast(node.clone()) { | ||
if let Some(record_path) = record_expr.path() { | ||
if record_path.syntax().text_range().contains_range(path.syntax().text_range()) { | ||
return true; | ||
} | ||
} | ||
} | ||
// Stop at type/pattern boundaries | ||
if ast::Type::cast(node.clone()).is_some() | ||
|| ast::Pat::cast(node.clone()).is_some() | ||
|| ast::RetType::cast(node.clone()).is_some() | ||
{ | ||
return false; | ||
} | ||
current = node.parent(); | ||
} | ||
false | ||
} | ||
|
||
fn get_bare_type_name(ty_str: &str) -> String { | ||
if let Some(angle_pos) = ty_str.find('<') { | ||
ty_str[..angle_pos].to_owned() | ||
} else { | ||
ty_str.to_owned() | ||
} | ||
} | ||
|
||
impl Ctx<'_> { | ||
fn apply(&self, item: &SyntaxNode) { | ||
// `transform_path` may update a node's parent and that would break the | ||
|
@@ -413,10 +454,17 @@ impl Ctx<'_> { | |
true, | ||
) | ||
.ok()?; | ||
let ast_ty = make::ty(ty_str).clone_for_update(); | ||
|
||
// Context-aware replacement | ||
let replacement = if is_path_in_expression_context(&path) { | ||
let bare_name = get_bare_type_name(ty_str); | ||
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. I think the more correct approach would be to replace the |
||
make::ty(&bare_name).clone_for_update() | ||
} else { | ||
make::ty(ty_str).clone_for_update() | ||
}; | ||
|
||
if let Some(adt) = ty.as_adt() { | ||
if let ast::Type::PathType(path_ty) = &ast_ty { | ||
if let ast::Type::PathType(path_ty) = &replacement { | ||
let cfg = ImportPathConfig { | ||
prefer_no_std: false, | ||
prefer_prelude: true, | ||
|
@@ -439,7 +487,7 @@ impl Ctx<'_> { | |
} | ||
} | ||
|
||
ted::replace(path.syntax(), ast_ty.syntax()); | ||
ted::replace(path.syntax(), replacement.syntax()); | ||
} | ||
hir::PathResolution::Local(_) | ||
| hir::PathResolution::Def(_) | ||
|
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.
This can be simplified (and also made more robust by that)
I don't think we need to consider record expression constructors, as their path is actually in type namespace if I am not mistaken.
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.
Okay, my original concern was about struct constructor calls like
Foo { field: value }
where we might want Foo instead ofSelf::Foo
, but thinking about it more, record expressions are indeed type namespace references and even if we wanted bare names there,PathTransform
should handle the namespace correctly, so this complexity isn't justified indeed, thanks.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.
We can always add back heuristics later that can scan for cases where we can simplify the path, but for now that's not really necessary I think