Skip to content

Commit 8ad536f

Browse files
committed
Make path start with a QualifiedPathType
1 parent 609bdbc commit 8ad536f

File tree

1 file changed

+75
-9
lines changed

1 file changed

+75
-9
lines changed

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

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,24 @@ pub(crate) fn into_to_qualified_from(acc: &mut Assists, ctx: &AssistContext<'_>)
5252
== FamousDefs(sema, scope.krate()).core_convert_Into()?
5353
{
5454
let type_call = sema.type_of_expr(&method_call.clone().into())?;
55-
let type_call_disp =
56-
type_call.adjusted().display_source_code(db, scope.module().into(), true).ok()?;
55+
let adjusted_tc = type_call.adjusted();
56+
57+
if adjusted_tc.is_unknown() && adjusted_tc.contains_unknown() {
58+
return None;
59+
}
60+
61+
let qualified_from = format!(
62+
"<{}>::from({})",
63+
adjusted_tc.display_source_code(db, scope.module().into(), true).ok()?,
64+
receiver
65+
);
5766

5867
acc.add(
5968
AssistId("into_to_qualified_from", AssistKind::Generate),
6069
"Convert `into` to fully qualified `from`",
6170
nameref.syntax().text_range(),
6271
|edit| {
63-
edit.replace(
64-
method_call.syntax().text_range(),
65-
format!("{}::from({})", type_call_disp, receiver),
66-
);
72+
edit.replace(method_call.syntax().text_range(), qualified_from);
6773
},
6874
);
6975
}
@@ -106,7 +112,7 @@ impl From<A> for B {
106112
107113
fn main() -> () {
108114
let a: A = A;
109-
let b: B = B::from(a);
115+
let b: B = <B>::from(a);
110116
}"#,
111117
)
112118
}
@@ -154,7 +160,7 @@ mod C {
154160
155161
fn main() -> () {
156162
let a: A = A;
157-
let b: B = B::from(a);
163+
let b: B = <B>::from(a);
158164
}"#,
159165
)
160166
}
@@ -198,7 +204,67 @@ mod C {
198204
199205
fn main() -> () {
200206
let a: A = A;
201-
let b: C::B = C::B::from(a);
207+
let b: C::B = <C::B>::from(a);
208+
}"#,
209+
)
210+
}
211+
212+
#[test]
213+
fn preceding_type_qualifier() {
214+
check_assist(
215+
into_to_qualified_from,
216+
r#"
217+
//- minicore: from
218+
impl From<(i32,i32)> for [i32;2] {
219+
fn from(value: (i32,i32)) -> Self {
220+
[value.0, value.1]
221+
}
222+
}
223+
224+
fn tuple_to_array() -> [i32; 2] {
225+
(0,1).in$0to()
226+
}"#,
227+
r#"
228+
impl From<(i32,i32)> for [i32;2] {
229+
fn from(value: (i32,i32)) -> Self {
230+
[value.0, value.1]
231+
}
232+
}
233+
234+
fn tuple_to_array() -> [i32; 2] {
235+
<[i32; 2]>::from((0,1))
236+
}"#,
237+
)
238+
}
239+
240+
#[test]
241+
fn type_with_gens() {
242+
check_assist(
243+
into_to_qualified_from,
244+
r#"
245+
//- minicore: from
246+
struct StructA<Gen>(Gen);
247+
248+
impl From<i32> for StructA<i32> {
249+
fn from(value: i32) -> Self {
250+
StructA(value + 1)
251+
}
252+
}
253+
254+
fn main() -> () {
255+
let a: StructA<i32> = 3.in$0to();
256+
}"#,
257+
r#"
258+
struct StructA<Gen>(Gen);
259+
260+
impl From<i32> for StructA<i32> {
261+
fn from(value: i32) -> Self {
262+
StructA(value + 1)
263+
}
264+
}
265+
266+
fn main() -> () {
267+
let a: StructA<i32> = <StructA<i32>>::from(3);
202268
}"#,
203269
)
204270
}

0 commit comments

Comments
 (0)