From 57a454233c57e297450f9f9990b1cad8ad8c845c Mon Sep 17 00:00:00 2001 From: Soham Chowdhury Date: Fri, 2 Feb 2024 15:29:12 +0100 Subject: [PATCH 1/2] inline_call: ensure correct whitespace in parameter types --- .../ide-assists/src/handlers/inline_call.rs | 148 +++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-) diff --git a/crates/ide-assists/src/handlers/inline_call.rs b/crates/ide-assists/src/handlers/inline_call.rs index 4ba33ada48c7..0af4248eb624 100644 --- a/crates/ide-assists/src/handlers/inline_call.rs +++ b/crates/ide-assists/src/handlers/inline_call.rs @@ -415,7 +415,24 @@ fn inline( let expr: &ast::Expr = expr; let mut insert_let_stmt = || { - let ty = sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty.clone()); + let param_ty = match param_ty { + None => None, + Some(param_ty) => { + if sema.hir_file_for(param_ty.syntax()).is_macro() { + if let Some(param_ty) = + ast::Type::cast(insert_ws_into(param_ty.syntax().clone())) + { + Some(param_ty) + } else { + Some(param_ty.clone_for_update()) + } + } else { + Some(param_ty.clone_for_update()) + } + } + }; + let ty: Option = + sema.type_of_expr(expr).filter(TypeInfo::has_adjustment).and(param_ty); let is_self = param .name(sema.db) @@ -1732,6 +1749,135 @@ pub fn main() { this.0 += 1; }; } +"#, + ) + } + + #[test] + fn inline_call_with_reference_in_macros() { + check_assist( + inline_call, + r#" +fn _write_u64(s: &mut u64, x: u64) { + *s += x; +} +macro_rules! impl_write { + ($(($ty:ident, $meth:ident),)*) => {$( + fn _hash(inner_self_: &u64, state: &mut u64) { + $meth(state, *inner_self_) + } + )*} +} +impl_write! { (u64, _write_u64), } +fn _hash2(self_: &u64, state: &mut u64) { + $0_hash(&self_, state); +} +"#, + r#" +fn _write_u64(s: &mut u64, x: u64) { + *s += x; +} +macro_rules! impl_write { + ($(($ty:ident, $meth:ident),)*) => {$( + fn _hash(inner_self_: &u64, state: &mut u64) { + $meth(state, *inner_self_) + } + )*} +} +impl_write! { (u64, _write_u64), } +fn _hash2(self_: &u64, state: &mut u64) { + { + let inner_self_: &u64 = &self_; + let state: &mut u64 = state; + _write_u64(state, *inner_self_) + }; +} +"#, + ) + } + + #[test] + fn inline_call_with_reference_in_macro_generated_trait_impl() { + check_assist( + inline_call, + r#" +trait Hash2 { + fn hash2(&self, state: &mut H); +} + +trait Hasher2 { + fn write2_u64(&mut self, x: u64); +} +impl Hasher2 for u64 { + fn write2_u64(&mut self, x: u64) { + *self += x; + } +} + +macro_rules! impl_write { + ($(($ty:ident, $meth:ident),)*) => {$( + impl Hash2 for $ty { + #[inline] + fn hash2(&self, state: &mut H) { + state.$meth(*self) + } + } + )*} +} + +impl_write! { (u64, write2_u64), } + +pub struct MyStruct { + value: u64, +} + +impl Hash2 for MyStruct { + fn hash2(&self, state: &mut H) { + self.value.$0hash2(state) + } +} +"#, + // + r#" +trait Hash2 { + fn hash2(&self, state: &mut H); +} + +trait Hasher2 { + fn write2_u64(&mut self, x: u64); +} +impl Hasher2 for u64 { + fn write2_u64(&mut self, x: u64) { + *self += x; + } +} + +macro_rules! impl_write { + ($(($ty:ident, $meth:ident),)*) => {$( + impl Hash2 for $ty { + #[inline] + fn hash2(&self, state: &mut H) { + state.$meth(*self) + } + } + )*} +} + +impl_write! { (u64, write2_u64), } + +pub struct MyStruct { + value: u64, +} + +impl Hash2 for MyStruct { + fn hash2(&self, state: &mut H) { + { + let this = &self.value; + let state: &mut H = state; + state.write2_u64(*this) + } + } +} "#, ) } From 18be556b37fa89bea50a40bd980c392346df838a Mon Sep 17 00:00:00 2001 From: Soham Chowdhury Date: Fri, 9 Feb 2024 22:01:34 +0100 Subject: [PATCH 2/2] inline_call: remove macro self->this test To be added back later once we have a fix. See #16471 and https://github.com/rust-lang/rust-analyzer/pull/16497#issuecomment-1934243409. --- .../ide-assists/src/handlers/inline_call.rs | 86 ------------------- 1 file changed, 86 deletions(-) diff --git a/crates/ide-assists/src/handlers/inline_call.rs b/crates/ide-assists/src/handlers/inline_call.rs index 0af4248eb624..11b22b65205b 100644 --- a/crates/ide-assists/src/handlers/inline_call.rs +++ b/crates/ide-assists/src/handlers/inline_call.rs @@ -1792,92 +1792,6 @@ fn _hash2(self_: &u64, state: &mut u64) { _write_u64(state, *inner_self_) }; } -"#, - ) - } - - #[test] - fn inline_call_with_reference_in_macro_generated_trait_impl() { - check_assist( - inline_call, - r#" -trait Hash2 { - fn hash2(&self, state: &mut H); -} - -trait Hasher2 { - fn write2_u64(&mut self, x: u64); -} -impl Hasher2 for u64 { - fn write2_u64(&mut self, x: u64) { - *self += x; - } -} - -macro_rules! impl_write { - ($(($ty:ident, $meth:ident),)*) => {$( - impl Hash2 for $ty { - #[inline] - fn hash2(&self, state: &mut H) { - state.$meth(*self) - } - } - )*} -} - -impl_write! { (u64, write2_u64), } - -pub struct MyStruct { - value: u64, -} - -impl Hash2 for MyStruct { - fn hash2(&self, state: &mut H) { - self.value.$0hash2(state) - } -} -"#, - // - r#" -trait Hash2 { - fn hash2(&self, state: &mut H); -} - -trait Hasher2 { - fn write2_u64(&mut self, x: u64); -} -impl Hasher2 for u64 { - fn write2_u64(&mut self, x: u64) { - *self += x; - } -} - -macro_rules! impl_write { - ($(($ty:ident, $meth:ident),)*) => {$( - impl Hash2 for $ty { - #[inline] - fn hash2(&self, state: &mut H) { - state.$meth(*self) - } - } - )*} -} - -impl_write! { (u64, write2_u64), } - -pub struct MyStruct { - value: u64, -} - -impl Hash2 for MyStruct { - fn hash2(&self, state: &mut H) { - { - let this = &self.value; - let state: &mut H = state; - state.write2_u64(*this) - } - } -} "#, ) }