Skip to content

Commit 8121d2e

Browse files
committed
Fix up autoderef when performing mutable auto borrow
1 parent fb0793c commit 8121d2e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/librustc_typeck/check/method/confirm.rs

-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,6 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
119119

120120
// Create the final `MethodCallee`.
121121
let callee = MethodCallee { def_id: pick.item.def_id, substs: all_substs, sig: method_sig };
122-
123-
if let Some(hir::Mutability::Mut) = pick.autoref {
124-
self.convert_place_derefs_to_mutable(self.self_expr);
125-
}
126-
127122
ConfirmResult { callee, illegal_sized_bound }
128123
}
129124

src/librustc_typeck/check/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -3183,6 +3183,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31833183
return;
31843184
}
31853185

3186+
let autoborrow_mut = adj.iter().any(|adj| {
3187+
matches!(adj, &Adjustment {
3188+
kind: Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. })),
3189+
..
3190+
})
3191+
});
3192+
31863193
match self.tables.borrow_mut().adjustments_mut().entry(expr.hir_id) {
31873194
Entry::Vacant(entry) => {
31883195
entry.insert(adj);
@@ -3212,6 +3219,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32123219
*entry.get_mut() = adj;
32133220
}
32143221
}
3222+
3223+
// When there is an auto mutable borrow, it is equivalent to `&mut expr`,
3224+
// thus `expr` is ought to be typechecked with needs = [`Needs::MutPlace`].
3225+
// However in many cases it might not be checked this way originally, e.g.
3226+
// the receiver of a method call. We need to fix them up.
3227+
if autoborrow_mut {
3228+
self.convert_place_derefs_to_mutable(expr);
3229+
}
32153230
}
32163231

32173232
/// Basically whenever we are converting from a type scheme into

0 commit comments

Comments
 (0)