Skip to content

Commit 34ace10

Browse files
committed
Always reborrow mutable reference receiver in methods
1 parent ea22d24 commit 34ace10

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

crates/hir-ty/src/method_resolution.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,10 @@ fn iterate_method_candidates_with_autoref(
952952
)
953953
};
954954

955-
iterate_method_candidates_by_receiver(receiver_ty, first_adjustment.clone())?;
955+
if !matches!(receiver_ty.value.kind(Interner), TyKind::Ref(Mutability::Mut, _, _)) {
956+
// We should prefer reborrow in case of mutable references
957+
iterate_method_candidates_by_receiver(receiver_ty, first_adjustment.clone())?;
958+
}
956959

957960
let refed = Canonical {
958961
value: TyKind::Ref(Mutability::Not, static_lifetime(), receiver_ty.value.clone())

crates/hir-ty/src/tests/method_resolution.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,25 @@ mod bar_test {
388388
);
389389
}
390390

391+
#[test]
392+
fn infer_trait_method_multiple_mutable_reference() {
393+
check_types(
394+
r#"
395+
trait Trait {
396+
fn method(&mut self) -> i32 { 5 }
397+
}
398+
struct S;
399+
impl Trait for &mut &mut S {}
400+
fn test() {
401+
let s = &mut &mut &mut S;
402+
s.method();
403+
//^^^^^^^^^^ i32
404+
}
405+
"#,
406+
);
407+
}
408+
409+
391410
#[test]
392411
fn infer_trait_method_generic_1() {
393412
// the trait implementation is intentionally incomplete -- it shouldn't matter

crates/ide/src/inlay_hints/adjustment.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ fn main() {
322322
//^^^^^^^^^^^&
323323
//^^^^^^^^^^^*
324324
(&mut Struct).by_ref_mut();
325+
//^^^^^^^^^^^&mut $
326+
//^^^^^^^^^^^*
325327
326328
// Check that block-like expressions don't duplicate hints
327329
let _: &mut [u32] = (&mut []);
@@ -425,6 +427,10 @@ fn main() {
425427
//^^^^^^^^^^^.*
426428
//^^^^^^^^^^^.&
427429
(&mut Struct).by_ref_mut();
430+
//^^^^^^^^^^^(
431+
//^^^^^^^^^^^)
432+
//^^^^^^^^^^^.*
433+
//^^^^^^^^^^^.&mut
428434
429435
// Check that block-like expressions don't duplicate hints
430436
let _: &mut [u32] = (&mut []);

0 commit comments

Comments
 (0)