Skip to content

Commit 38b5dcb

Browse files
committed
fix: self type replacement with macros
1 parent 3449222 commit 38b5dcb

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ fn inline(
373373
// We should place the following code after last usage of `usages_for_locals`
374374
// because `ted::replace` will change the offset in syntax tree, which makes
375375
// `FileReference` incorrect
376-
if let Some(imp) = body.syntax().ancestors().find_map(ast::Impl::cast) {
376+
if let Some(imp) =
377+
sema.ancestors_with_macros(fn_body.syntax().clone()).find_map(ast::Impl::cast)
378+
{
377379
if !node.syntax().ancestors().any(|anc| &anc == imp.syntax()) {
378380
if let Some(t) = imp.self_ty() {
379381
while let Some(self_tok) = body
@@ -1559,6 +1561,64 @@ fn a() -> bool {
15591561
this == &Enum::A || this == &Enum::B
15601562
}
15611563
}
1564+
"#,
1565+
)
1566+
}
1567+
1568+
#[test]
1569+
fn inline_call_with_self_type_in_macros() {
1570+
check_assist(
1571+
inline_call,
1572+
r#"
1573+
trait Trait<T1> {
1574+
fn f(a: T1) -> Self;
1575+
}
1576+
1577+
macro_rules! impl_from {
1578+
($t: ty) => {
1579+
impl Trait<$t> for $t {
1580+
fn f(a: $t) -> Self {
1581+
a as Self
1582+
}
1583+
}
1584+
};
1585+
}
1586+
1587+
struct A {}
1588+
1589+
impl_from!(A);
1590+
1591+
fn main() {
1592+
let a: A = A{};
1593+
let b = <A as Trait<A>>::$0f(a);
1594+
}
1595+
"#,
1596+
r#"
1597+
trait Trait<T1> {
1598+
fn f(a: T1) -> Self;
1599+
}
1600+
1601+
macro_rules! impl_from {
1602+
($t: ty) => {
1603+
impl Trait<$t> for $t {
1604+
fn f(a: $t) -> Self {
1605+
a as Self
1606+
}
1607+
}
1608+
};
1609+
}
1610+
1611+
struct A {}
1612+
1613+
impl_from!(A);
1614+
1615+
fn main() {
1616+
let a: A = A{};
1617+
let b = {
1618+
let a = a;
1619+
a as A
1620+
};
1621+
}
15621622
"#,
15631623
)
15641624
}

0 commit comments

Comments
 (0)