Skip to content

Commit aa97edb

Browse files
committed
Auto merge of #16497 - evertedsphere:swann/fix-inline-for-macro-generated-method, r=Veykril
Fix incorrect inlining of functions that come from MBE macros Partial fix for #16471. As a reminder, there are two issues there: 1. missing whitespace in parameter types (the first test) 2. the `self` parameter not being replaced by `this` in the function body (the second test) The first part is fixed in this PR. See [this comment](#16497 (comment)) for the second.
2 parents 7e92655 + 18be556 commit aa97edb

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
@@ -415,7 +415,24 @@ fn inline(
415415
let expr: &ast::Expr = expr;
416416

417417
let mut insert_let_stmt = || {
418-
let ty = sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty.clone());
418+
let param_ty = match param_ty {
419+
None => None,
420+
Some(param_ty) => {
421+
if sema.hir_file_for(param_ty.syntax()).is_macro() {
422+
if let Some(param_ty) =
423+
ast::Type::cast(insert_ws_into(param_ty.syntax().clone()))
424+
{
425+
Some(param_ty)
426+
} else {
427+
Some(param_ty.clone_for_update())
428+
}
429+
} else {
430+
Some(param_ty.clone_for_update())
431+
}
432+
}
433+
};
434+
let ty: Option<syntax::ast::Type> =
435+
sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty);
419436

420437
let is_self = param
421438
.name(sema.db)
@@ -1732,6 +1749,49 @@ pub fn main() {
17321749
this.0 += 1;
17331750
};
17341751
}
1752+
"#,
1753+
)
1754+
}
1755+
1756+
#[test]
1757+
fn inline_call_with_reference_in_macros() {
1758+
check_assist(
1759+
inline_call,
1760+
r#"
1761+
fn _write_u64(s: &mut u64, x: u64) {
1762+
*s += x;
1763+
}
1764+
macro_rules! impl_write {
1765+
($(($ty:ident, $meth:ident),)*) => {$(
1766+
fn _hash(inner_self_: &u64, state: &mut u64) {
1767+
$meth(state, *inner_self_)
1768+
}
1769+
)*}
1770+
}
1771+
impl_write! { (u64, _write_u64), }
1772+
fn _hash2(self_: &u64, state: &mut u64) {
1773+
$0_hash(&self_, state);
1774+
}
1775+
"#,
1776+
r#"
1777+
fn _write_u64(s: &mut u64, x: u64) {
1778+
*s += x;
1779+
}
1780+
macro_rules! impl_write {
1781+
($(($ty:ident, $meth:ident),)*) => {$(
1782+
fn _hash(inner_self_: &u64, state: &mut u64) {
1783+
$meth(state, *inner_self_)
1784+
}
1785+
)*}
1786+
}
1787+
impl_write! { (u64, _write_u64), }
1788+
fn _hash2(self_: &u64, state: &mut u64) {
1789+
{
1790+
let inner_self_: &u64 = &self_;
1791+
let state: &mut u64 = state;
1792+
_write_u64(state, *inner_self_)
1793+
};
1794+
}
17351795
"#,
17361796
)
17371797
}

0 commit comments

Comments
 (0)