Skip to content

Commit ac804f2

Browse files
committed
Trying to detect autorefs to avoid unnecessary borrowed temporaries
This is all almost certainly wrong
1 parent 513a9c6 commit ac804f2

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

compiler/rustc_typeck/src/check/generator_interior/drop_ranges/record_consumed_borrow.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
102102
&mut self,
103103
place_with_id: &expr_use_visitor::PlaceWithHirId<'tcx>,
104104
diag_expr_id: HirId,
105-
_bk: rustc_middle::ty::BorrowKind,
105+
bk: rustc_middle::ty::BorrowKind,
106+
is_autoref: bool,
106107
) {
107-
debug!("borrow: place_with_id = {place_with_id:?}, diag_expr_id={diag_expr_id:?}");
108+
debug!(
109+
"borrow: place_with_id = {place_with_id:?}, diag_expr_id={diag_expr_id:?}, \
110+
borrow_kind={bk:?}, is_autoref={is_autoref}"
111+
);
108112

109113
self.places
110114
.borrowed
@@ -114,7 +118,7 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
114118
//
115119
// XXX -- we need to distinguish `&*E` where `E: &T` which is not creating a temporary
116120
// even though the place-base E is an rvalue
117-
if let PlaceBase::Rvalue = place_with_id.place.base {
121+
if let (false, PlaceBase::Rvalue) = (is_autoref, place_with_id.place.base) {
118122
self.places.borrowed_temporaries.insert(place_with_id.hir_id);
119123
}
120124
}

compiler/rustc_typeck/src/check/upvar.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,7 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
17921792
place_with_id: &PlaceWithHirId<'tcx>,
17931793
diag_expr_id: hir::HirId,
17941794
bk: ty::BorrowKind,
1795+
_is_autoref: bool,
17951796
) {
17961797
let PlaceBase::Upvar(upvar_id) = place_with_id.place.base else { return };
17971798
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> {
18261827

18271828
#[instrument(skip(self), level = "debug")]
18281829
fn mutate(&mut self, assignee_place: &PlaceWithHirId<'tcx>, diag_expr_id: hir::HirId) {
1829-
self.borrow(assignee_place, diag_expr_id, ty::BorrowKind::MutBorrow);
1830+
self.borrow(assignee_place, diag_expr_id, ty::BorrowKind::MutBorrow, false);
18301831
}
18311832
}
18321833

compiler/rustc_typeck/src/expr_use_visitor.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub trait Delegate<'tcx> {
4545
place_with_id: &PlaceWithHirId<'tcx>,
4646
diag_expr_id: hir::HirId,
4747
bk: ty::BorrowKind,
48+
is_autoref: bool,
4849
);
4950

5051
/// The path at `assignee_place` is being assigned to.
@@ -175,7 +176,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
175176
debug!("borrow_expr(expr={:?}, bk={:?})", expr, bk);
176177

177178
let place_with_id = return_if_err!(self.mc.cat_expr(expr));
178-
self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk);
179+
self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk, false);
179180

180181
self.walk_expr(expr)
181182
}
@@ -558,7 +559,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
558559
// this is an autoref of `x`.
559560
adjustment::Adjust::Deref(Some(ref deref)) => {
560561
let bk = ty::BorrowKind::from_mutbl(deref.mutbl);
561-
self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk);
562+
self.delegate.borrow(&place_with_id, place_with_id.hir_id, bk, true);
562563
}
563564

564565
adjustment::Adjust::Borrow(ref autoref) => {
@@ -590,13 +591,14 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
590591
base_place,
591592
base_place.hir_id,
592593
ty::BorrowKind::from_mutbl(m.into()),
594+
true,
593595
);
594596
}
595597

596598
adjustment::AutoBorrow::RawPtr(m) => {
597599
debug!("walk_autoref: expr.hir_id={} base_place={:?}", expr.hir_id, base_place);
598600

599-
self.delegate.borrow(base_place, base_place.hir_id, ty::BorrowKind::from_mutbl(m));
601+
self.delegate.borrow(base_place, base_place.hir_id, ty::BorrowKind::from_mutbl(m), true);
600602
}
601603
}
602604
}
@@ -669,7 +671,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
669671
match bm {
670672
ty::BindByReference(m) => {
671673
let bk = ty::BorrowKind::from_mutbl(m);
672-
delegate.borrow(place, discr_place.hir_id, bk);
674+
delegate.borrow(place, discr_place.hir_id, bk, false);
673675
}
674676
ty::BindByValue(..) => {
675677
debug!("walk_pat binding consuming pat");
@@ -799,6 +801,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
799801
&place_with_id,
800802
place_with_id.hir_id,
801803
upvar_borrow,
804+
false,
802805
);
803806
}
804807
}
@@ -837,7 +840,7 @@ fn delegate_consume<'a, 'tcx>(
837840
match mode {
838841
ConsumeMode::Move => delegate.consume(place_with_id, diag_expr_id),
839842
ConsumeMode::Copy => {
840-
delegate.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow)
843+
delegate.borrow(place_with_id, diag_expr_id, ty::BorrowKind::ImmBorrow, false)
841844
}
842845
}
843846
}

0 commit comments

Comments
 (0)