-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add AsRef and Borrow for generate_mut_trait_impl #19917
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
A4-Tacks
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
A4-Tacks:ext-generate-mut
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,36 +48,42 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_> | |
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update(); | ||
let indent = impl_def.indent_level(); | ||
|
||
let (apply_trait, new_apply_trait) = impl_def | ||
.syntax() | ||
.descendants() | ||
.filter_map(ast::NameRef::cast) | ||
.find_map(process_trait_name)?; | ||
|
||
let trait_ = impl_def.trait_()?; | ||
if let ast::Type::PathType(trait_path) = trait_ { | ||
let trait_type = ctx.sema.resolve_trait(&trait_path.path()?)?; | ||
let scope = ctx.sema.scope(trait_path.syntax())?; | ||
if trait_type != FamousDefs(&ctx.sema, scope.krate()).core_convert_Index()? { | ||
let famous_defs = FamousDefs(&ctx.sema, scope.krate()); | ||
if trait_type != get_famous(&apply_trait.text(), famous_defs)? { | ||
return None; | ||
} | ||
} | ||
|
||
// Index -> IndexMut | ||
let index_trait = impl_def | ||
.syntax() | ||
.descendants() | ||
.filter_map(ast::NameRef::cast) | ||
.find(|it| it.text() == "Index")?; | ||
ted::replace( | ||
index_trait.syntax(), | ||
make::path_segment(make::name_ref("IndexMut")).clone_for_update().syntax(), | ||
apply_trait.syntax(), | ||
make::path_segment(make::name_ref(new_apply_trait)).clone_for_update().syntax(), | ||
); | ||
|
||
// index -> index_mut | ||
let trait_method_name = impl_def | ||
let (trait_method_name, new_trait_method_name) = impl_def | ||
.syntax() | ||
.descendants() | ||
.filter_map(ast::Name::cast) | ||
.find(|it| it.text() == "index")?; | ||
ted::replace(trait_method_name.syntax(), make::name("index_mut").clone_for_update().syntax()); | ||
.find_map(process_method_name)?; | ||
ted::replace( | ||
trait_method_name.syntax(), | ||
make::name(new_trait_method_name).clone_for_update().syntax(), | ||
); | ||
|
||
let type_alias = impl_def.syntax().descendants().find_map(ast::TypeAlias::cast)?; | ||
ted::remove(type_alias.syntax()); | ||
if let Some(type_alias) = impl_def.syntax().descendants().find_map(ast::TypeAlias::cast) { | ||
ted::remove(type_alias.syntax()); | ||
} | ||
|
||
// &self -> &mut self | ||
let mut_self_param = make::mut_self_param(); | ||
|
@@ -87,10 +93,8 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_> | |
|
||
// &Self::Output -> &mut Self::Output | ||
let ret_type = impl_def.syntax().descendants().find_map(ast::RetType::cast)?; | ||
ted::replace( | ||
ret_type.syntax(), | ||
make::ret_type(make::ty("&mut Self::Output")).clone_for_update().syntax(), | ||
); | ||
let new_ret_type = process_ret_type(&ret_type)?; | ||
ted::replace(ret_type.syntax(), make::ret_type(new_ret_type).clone_for_update().syntax()); | ||
|
||
let fn_ = impl_def.assoc_item_list()?.assoc_items().find_map(|it| match it { | ||
ast::AssocItem::Fn(f) => Some(f), | ||
|
@@ -104,14 +108,51 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_> | |
let target = impl_def.syntax().text_range(); | ||
acc.add( | ||
AssistId::generate("generate_mut_trait_impl"), | ||
"Generate `IndexMut` impl from this `Index` trait", | ||
format!("Generate `{new_apply_trait}` impl from this `{apply_trait}` trait"), | ||
target, | ||
|edit| { | ||
edit.insert(target.start(), format!("$0{impl_def}\n\n{indent}")); | ||
}, | ||
) | ||
} | ||
|
||
fn get_famous(apply_trait: &str, famous: FamousDefs<'_, '_>) -> Option<hir::Trait> { | ||
match apply_trait { | ||
"Index" => famous.core_convert_Index(), | ||
"AsRef" => famous.core_convert_AsRef(), | ||
"Borrow" => famous.core_borrow_Borrow(), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn process_trait_name(name: ast::NameRef) -> Option<(ast::NameRef, &'static str)> { | ||
let new_name = match &*name.text() { | ||
"Index" => "IndexMut", | ||
"AsRef" => "AsMut", | ||
"Borrow" => "BorrowMut", | ||
_ => return None, | ||
}; | ||
Some((name, new_name)) | ||
} | ||
|
||
fn process_method_name(name: ast::Name) -> Option<(ast::Name, &'static str)> { | ||
let new_name = match &*name.text() { | ||
"index" => "index_mut", | ||
"as_ref" => "as_mut", | ||
"borrow" => "borrow_mut", | ||
_ => return None, | ||
}; | ||
Some((name, new_name)) | ||
} | ||
|
||
fn process_ret_type(ref_ty: &ast::RetType) -> Option<ast::Type> { | ||
let ty = ref_ty.ty()?; | ||
let ast::Type::RefType(ref_type) = ty else { | ||
return None; | ||
}; | ||
Some(make::ty_ref(ref_type.ty()?, true)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::tests::{check_assist, check_assist_not_applicable}; | ||
|
@@ -186,6 +227,35 @@ impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy { | |
var_name | ||
} | ||
} | ||
"#, | ||
); | ||
|
||
check_assist( | ||
generate_mut_trait_impl, | ||
r#" | ||
//- minicore: as_ref | ||
struct Foo(i32); | ||
|
||
impl<T> core::convert::AsRef$0<i32> for Foo { | ||
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. Those tests have redundant type parameter |
||
fn as_ref(&self) -> &i32 { | ||
&self.0 | ||
} | ||
} | ||
"#, | ||
r#" | ||
struct Foo(i32); | ||
|
||
$0impl<T> core::convert::AsMut<i32> for Foo { | ||
fn as_mut(&mut self) -> &mut i32 { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T> core::convert::AsRef<i32> for Foo { | ||
fn as_ref(&self) -> &i32 { | ||
&self.0 | ||
} | ||
} | ||
"#, | ||
); | ||
} | ||
|
@@ -285,6 +355,14 @@ mod foo { | |
pub trait Index<Idx: ?Sized> {} | ||
|
||
impl<T> Index$0<i32> for [T; 3] {} | ||
"#, | ||
); | ||
check_assist_not_applicable( | ||
generate_mut_trait_impl, | ||
r#" | ||
pub trait AsRef<T: ?Sized> {} | ||
|
||
impl<T> AsRef$0<i32> for [T; 3] {} | ||
"#, | ||
); | ||
} | ||
|
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
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.
I think this should be receive
hir::Trait
though it's currently using&str
before this PR.Like here:
rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs
Lines 35 to 44 in 5b852da