Skip to content

Fix up autoderef when reborrowing #72280

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 7 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix up autoderef when performing mutable auto borrow
  • Loading branch information
nbdd0121 committed Jun 15, 2020
commit 8121d2e0576e74b23f0019e857b1088197ef8c04
5 changes: 0 additions & 5 deletions src/librustc_typeck/check/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {

// Create the final `MethodCallee`.
let callee = MethodCallee { def_id: pick.item.def_id, substs: all_substs, sig: method_sig };

if let Some(hir::Mutability::Mut) = pick.autoref {
self.convert_place_derefs_to_mutable(self.self_expr);
}

ConfirmResult { callee, illegal_sized_bound }
}

Expand Down
15 changes: 15 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3183,6 +3183,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return;
}

let autoborrow_mut = adj.iter().any(|adj| {
matches!(adj, &Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. })),
..
})
});

match self.tables.borrow_mut().adjustments_mut().entry(expr.hir_id) {
Entry::Vacant(entry) => {
entry.insert(adj);
Expand Down Expand Up @@ -3212,6 +3219,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
*entry.get_mut() = adj;
}
}

// When there is an auto mutable borrow, it is equivalent to `&mut expr`,
// thus `expr` is ought to be typechecked with needs = [`Needs::MutPlace`].
// However in many cases it might not be checked this way originally, e.g.
// the receiver of a method call. We need to fix them up.
if autoborrow_mut {
self.convert_place_derefs_to_mutable(expr);
}
}

/// Basically whenever we are converting from a type scheme into
Expand Down