Skip to content

Always reborrow mutable reference receiver in methods #14511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/hir-ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,14 @@ fn iterate_method_candidates_with_autoref(
)
};

iterate_method_candidates_by_receiver(receiver_ty, first_adjustment.clone())?;
let mut maybe_reborrowed = first_adjustment.clone();
if let Some((_, _, m)) = receiver_ty.value.as_reference() {
// Prefer reborrow of references to move
maybe_reborrowed.autoref = Some(m);
maybe_reborrowed.autoderefs += 1;
}

iterate_method_candidates_by_receiver(receiver_ty, maybe_reborrowed)?;

let refed = Canonical {
value: TyKind::Ref(Mutability::Not, static_lifetime(), receiver_ty.value.clone())
Expand Down
20 changes: 19 additions & 1 deletion crates/hir-ty/src/tests/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ mod bar_test {
);
}

#[test]
fn infer_trait_method_multiple_mutable_reference() {
check_types(
r#"
trait Trait {
fn method(&mut self) -> i32 { 5 }
}
struct S;
impl Trait for &mut &mut S {}
fn test() {
let s = &mut &mut &mut S;
s.method();
//^^^^^^^^^^ i32
}
"#,
);
}

#[test]
fn infer_trait_method_generic_1() {
// the trait implementation is intentionally incomplete -- it shouldn't matter
Expand Down Expand Up @@ -1722,7 +1740,7 @@ fn test() {
Foo.foo();
//^^^ adjustments: Borrow(Ref(Not))
(&Foo).foo();
// ^^^^ adjustments: ,
// ^^^^ adjustments: Deref(None), Borrow(Ref(Not))
}
"#,
);
Expand Down
12 changes: 12 additions & 0 deletions crates/ide/src/inlay_hints/adjustment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,17 @@ fn main() {
(&Struct).consume();
//^^^^^^^*
(&Struct).by_ref();
//^^^^^^^&
//^^^^^^^*

(&mut Struct).consume();
//^^^^^^^^^^^*
(&mut Struct).by_ref();
//^^^^^^^^^^^&
//^^^^^^^^^^^*
(&mut Struct).by_ref_mut();
//^^^^^^^^^^^&mut $
//^^^^^^^^^^^*

// Check that block-like expressions don't duplicate hints
let _: &mut [u32] = (&mut []);
Expand Down Expand Up @@ -414,6 +418,10 @@ fn main() {
//^^^^^^^)
//^^^^^^^.*
(&Struct).by_ref();
//^^^^^^^(
//^^^^^^^)
//^^^^^^^.*
//^^^^^^^.&

(&mut Struct).consume();
//^^^^^^^^^^^(
Expand All @@ -425,6 +433,10 @@ fn main() {
//^^^^^^^^^^^.*
//^^^^^^^^^^^.&
(&mut Struct).by_ref_mut();
//^^^^^^^^^^^(
//^^^^^^^^^^^)
//^^^^^^^^^^^.*
//^^^^^^^^^^^.&mut

// Check that block-like expressions don't duplicate hints
let _: &mut [u32] = (&mut []);
Expand Down