Trying to detect autorefs to avoid unnecessary borrowed temporaries

This is all almost certainly wrong
This commit is contained in:
Eric Holk 2022-02-22 16:10:17 -08:00
parent 513a9c67a5
commit ac804f27a8
3 changed files with 17 additions and 9 deletions

View File

@ -102,9 +102,13 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
&mut self,
place_with_id: &expr_use_visitor::PlaceWithHirId<'tcx>,
diag_expr_id: HirId,
_bk: rustc_middle::ty::BorrowKind,
bk: rustc_middle::ty::BorrowKind,
is_autoref: bool,
) {
debug!("borrow: place_with_id = {place_with_id:?}, diag_expr_id={diag_expr_id:?}");
debug!(
"borrow: place_with_id = {place_with_id:?}, diag_expr_id={diag_expr_id:?}, \
borrow_kind={bk:?}, is_autoref={is_autoref}"
);
self.places
.borrowed
@ -114,7 +118,7 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
//
// XXX -- we need to distinguish `&*E` where `E: &T` which is not creating a temporary
// even though the place-base E is an rvalue
if let PlaceBase::Rvalue = place_with_id.place.base {
if let (false, PlaceBase::Rvalue) = (is_autoref, place_with_id.place.base) {
self.places.borrowed_temporaries.insert(place_with_id.hir_id);
}
}

View File

@ -1792,6 +1792,7 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
place_with_id: &PlaceWithHirId<'tcx>,
diag_expr_id: hir::HirId,
bk: ty::BorrowKind,
_is_autoref: bool,
) {
let PlaceBase::Upvar(upvar_id) = place_with_id.place.base else { return };
assert_eq!(self.closure_def_id, upvar_id.closure_expr_id);
@ -1826,7 +1827,7 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
#[instrument(skip(self), level = "debug")]
fn mutate(&mut self, assignee_place: &PlaceWithHirId<'tcx>, diag_expr_id: hir::HirId) {
self.borrow(assignee_place, diag_expr_id, ty::BorrowKind::MutBorrow);
self.borrow(assignee_place, diag_expr_id, ty::BorrowKind::MutBorrow, false);
}
}

View File

@ -45,6 +45,7 @@ pub trait Delegate<'tcx> {
place_with_id: &PlaceWithHirId<'tcx>,
diag_expr_id: hir::HirId,
bk: ty::BorrowKind,
is_autoref: bool,
);
/// The path at `assignee_place` is being assigned to.
@ -175,7 +176,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
debug!("borrow_expr(expr={:?}, bk={:?})", expr, bk);
let place_with_id = return_if_err!(self.mc.cat_expr(expr));
self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk);
self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk, false);
self.walk_expr(expr)
}
@ -558,7 +559,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
// this is an autoref of `x`.
adjustment::Adjust::Deref(Some(ref deref)) => {
let bk = ty::BorrowKind::from_mutbl(deref.mutbl);
self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk);
self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk, true);
}
adjustment::Adjust::Borrow(ref autoref) => {
@ -590,13 +591,14 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
base_place,
base_place.hir_id,
ty::BorrowKind::from_mutbl(m.into()),
true,
);
}
adjustment::AutoBorrow::RawPtr(m) => {
debug!("walk_autoref: expr.hir_id={} base_place={:?}", expr.hir_id, base_place);
self.delegate.borrow(base_place, base_place.hir_id, ty::BorrowKind::from_mutbl(m));
self.delegate.borrow(base_place, base_place.hir_id, ty::BorrowKind::from_mutbl(m), true);
}
}
}
@ -669,7 +671,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
match bm {
ty::BindByReference(m) => {
let bk = ty::BorrowKind::from_mutbl(m);
delegate.borrow(place, discr_place.hir_id, bk);
delegate.borrow(place, discr_place.hir_id, bk, false);
}
ty::BindByValue(..) => {
debug!("walk_pat binding consuming pat");
@ -799,6 +801,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
&place_with_id,
place_with_id.hir_id,
upvar_borrow,
false,
);
}
}
@ -837,7 +840,7 @@ fn delegate_consume<'a, 'tcx>(
match mode {
ConsumeMode::Move => delegate.consume(place_with_id, diag_expr_id),
ConsumeMode::Copy => {
delegate.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow)
delegate.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow, false)
}
}
}