Skip to content

Commit d29eb4c

Browse files
committed
Use hir instead of trait name
1 parent 0097bd8 commit d29eb4c

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

crates/ide-assists/src/handlers/generate_asref_impl_from_borrow.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use ide_db::{famous_defs::FamousDefs, traits::resolve_target_trait};
12
use syntax::{
23
ast::{self, AstNode, HasName, edit_in_place::Indent, make},
34
syntax_editor::Position,
@@ -10,6 +11,8 @@ use crate::{AssistContext, AssistId, Assists};
1011
// Generate `AsRef` implement from `Borrow`.
1112
//
1213
// ```
14+
// //- minicore: borrow, as_ref
15+
// use core::borrow::Borrow;
1316
// struct Foo<T>(T);
1417
//
1518
// impl<T> $0Borrow<T> for Foo<T> {
@@ -20,6 +23,7 @@ use crate::{AssistContext, AssistId, Assists};
2023
// ```
2124
// ->
2225
// ```
26+
// use core::borrow::Borrow;
2327
// struct Foo<T>(T);
2428
//
2529
// $0impl<T> AsRef<T> for Foo<T> {
@@ -39,6 +43,8 @@ use crate::{AssistContext, AssistId, Assists};
3943
// Generate `AsMut` implement from `BorrowMut`.
4044
//
4145
// ```
46+
// //- minicore: borrow_mut, as_mut
47+
// use core::borrow::BorrowMut;
4248
// struct Foo<T>(T);
4349
//
4450
// impl<T> $0BorrowMut<T> for Foo<T> {
@@ -49,6 +55,7 @@ use crate::{AssistContext, AssistId, Assists};
4955
// ```
5056
// ->
5157
// ```
58+
// use core::borrow::BorrowMut;
5259
// struct Foo<T>(T);
5360
//
5461
// $0impl<T> AsMut<T> for Foo<T> {
@@ -73,12 +80,11 @@ pub(crate) fn generate_asref_impl_from_borrow(
7380
let indent = impl_.indent_level();
7481

7582
let name = path.path()?.segment()?.name_ref()?;
83+
let scope = ctx.sema.scope(path.syntax())?;
84+
let famous = FamousDefs(&ctx.sema, scope.krate());
85+
let trait_ = resolve_target_trait(&ctx.sema, &impl_)?;
7686

77-
let (target_name, target_method_name) = match &*name.text() {
78-
"Borrow" => ("AsRef", "as_ref"),
79-
"BorrowMut" => ("AsMut", "as_mut"),
80-
_ => return None,
81-
};
87+
let (target_name, target_method_name) = out_trait(famous, trait_)?;
8288

8389
let method = impl_.assoc_item_list()?.assoc_items().find_map(|it| match it {
8490
ast::AssocItem::Fn(f) => Some(f),
@@ -118,6 +124,19 @@ pub(crate) fn generate_asref_impl_from_borrow(
118124
)
119125
}
120126

127+
fn out_trait(
128+
famous: FamousDefs<'_, '_>,
129+
trait_: hir::Trait,
130+
) -> Option<(&'static str, &'static str)> {
131+
if trait_ == famous.core_borrow_Borrow()? {
132+
Some(("AsRef", "as_ref"))
133+
} else if trait_ == famous.core_borrow_BorrowMut()? {
134+
Some(("AsMut", "as_mut"))
135+
} else {
136+
None
137+
}
138+
}
139+
121140
#[cfg(test)]
122141
mod tests {
123142
use crate::tests::check_assist;
@@ -129,6 +148,8 @@ mod tests {
129148
check_assist(
130149
generate_asref_impl_from_borrow,
131150
r#"
151+
//- minicore: borrow, as_ref
152+
use core::borrow::Borrow;
132153
struct Foo<T>(T);
133154
134155
impl<T> $0Borrow<T> for Foo<T> {
@@ -138,6 +159,7 @@ impl<T> $0Borrow<T> for Foo<T> {
138159
}
139160
"#,
140161
r#"
162+
use core::borrow::Borrow;
141163
struct Foo<T>(T);
142164
143165
$0impl<T> AsRef<T> for Foo<T> {
@@ -160,6 +182,8 @@ impl<T> Borrow<T> for Foo<T> {
160182
check_assist(
161183
generate_asref_impl_from_borrow,
162184
r#"
185+
//- minicore: borrow_mut, as_mut
186+
use core::borrow::BorrowMut;
163187
struct Foo<T>(T);
164188
165189
impl<T> $0BorrowMut<T> for Foo<T> {
@@ -169,6 +193,7 @@ impl<T> $0BorrowMut<T> for Foo<T> {
169193
}
170194
"#,
171195
r#"
196+
use core::borrow::BorrowMut;
172197
struct Foo<T>(T);
173198
174199
$0impl<T> AsMut<T> for Foo<T> {
@@ -191,6 +216,8 @@ impl<T> BorrowMut<T> for Foo<T> {
191216
check_assist(
192217
generate_asref_impl_from_borrow,
193218
r#"
219+
//- minicore: borrow, as_ref
220+
use core::borrow::Borrow;
194221
struct Foo<T>(T);
195222
196223
#[cfg(feature = "foo")]
@@ -202,6 +229,7 @@ impl<T> $0Borrow<T> for Foo<T> {
202229
}
203230
"#,
204231
r#"
232+
use core::borrow::Borrow;
205233
struct Foo<T>(T);
206234
207235
$0#[cfg(feature = "foo")]
@@ -228,8 +256,10 @@ impl<T> Borrow<T> for Foo<T> {
228256
check_assist(
229257
generate_asref_impl_from_borrow,
230258
r#"
259+
//- minicore: borrow, as_ref
231260
mod foo {
232261
mod bar {
262+
use core::borrow::Borrow;
233263
struct Foo<T>(T);
234264
235265
impl<T> $0Borrow<T> for Foo<T> {
@@ -243,6 +273,7 @@ mod foo {
243273
r#"
244274
mod foo {
245275
mod bar {
276+
use core::borrow::Borrow;
246277
struct Foo<T>(T);
247278
248279
$0impl<T> AsRef<T> for Foo<T> {

crates/ide-assists/src/tests/generated.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,8 @@ fn doctest_generate_asref_impl_from_borrow() {
13161316
check_doc_test(
13171317
"generate_asref_impl_from_borrow",
13181318
r#####"
1319+
//- minicore: borrow, as_ref
1320+
use core::borrow::Borrow;
13191321
struct Foo<T>(T);
13201322
13211323
impl<T> $0Borrow<T> for Foo<T> {
@@ -1325,6 +1327,7 @@ impl<T> $0Borrow<T> for Foo<T> {
13251327
}
13261328
"#####,
13271329
r#####"
1330+
use core::borrow::Borrow;
13281331
struct Foo<T>(T);
13291332
13301333
$0impl<T> AsRef<T> for Foo<T> {
@@ -1347,6 +1350,8 @@ fn doctest_generate_asref_impl_from_borrow_1() {
13471350
check_doc_test(
13481351
"generate_asref_impl_from_borrow",
13491352
r#####"
1353+
//- minicore: borrow_mut, as_mut
1354+
use core::borrow::BorrowMut;
13501355
struct Foo<T>(T);
13511356
13521357
impl<T> $0BorrowMut<T> for Foo<T> {
@@ -1356,6 +1361,7 @@ impl<T> $0BorrowMut<T> for Foo<T> {
13561361
}
13571362
"#####,
13581363
r#####"
1364+
use core::borrow::BorrowMut;
13591365
struct Foo<T>(T);
13601366
13611367
$0impl<T> AsMut<T> for Foo<T> {

0 commit comments

Comments
 (0)