Skip to content

Commit

Permalink
Auto merge of #132301 - compiler-errors:adjust, r=lcnr
Browse files Browse the repository at this point in the history
Remove region from adjustments

It's not necessary to store this region, because it's only used in THIR and MemCat/ExprUse, both of which already basically only deal with erased regions anyways.
  • Loading branch information
bors committed Oct 31, 2024
2 parents 4d296ea + 599ffab commit 9ccfedf
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 64 deletions.
1 change: 0 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
matches!(
adj.kind,
ty::adjustment::Adjust::Borrow(ty::adjustment::AutoBorrow::Ref(
_,
ty::adjustment::AutoBorrowMutability::Not
| ty::adjustment::AutoBorrowMutability::Mut {
allow_two_phase_borrow: ty::adjustment::AllowTwoPhase::No
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_hir_typeck/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.try_overloaded_deref(autoderef.span(), source).and_then(
|InferOk { value: method, obligations: o }| {
obligations.extend(o);
if let ty::Ref(region, _, mutbl) = *method.sig.output().kind() {
Some(OverloadedDeref { region, mutbl, span: autoderef.span() })
// FIXME: we should assert the sig is &T here... there's no reason for this to be fallible.
if let ty::Ref(_, _, mutbl) = *method.sig.output().kind() {
Some(OverloadedDeref { mutbl, span: autoderef.span() })
} else {
None
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if borrow {
// Check for &self vs &mut self in the method signature. Since this is either
// the Fn or FnMut trait, it should be one of those.
let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].kind() else {
let ty::Ref(_, _, mutbl) = method.sig.inputs()[0].kind() else {
bug!("Expected `FnMut`/`Fn` to take receiver by-ref/by-mut")
};

Expand All @@ -317,7 +317,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::No);

autoref = Some(Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
target: method.sig.inputs()[0],
});
}
Expand Down
17 changes: 7 additions & 10 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn identity(_: Ty<'_>) -> Vec<Adjustment<'_>> {
vec![]
}

fn simple<'tcx>(kind: Adjust<'tcx>) -> impl FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>> {
fn simple<'tcx>(kind: Adjust) -> impl FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>> {
move |target| vec![Adjustment { kind, target }]
}

Expand Down Expand Up @@ -484,14 +484,11 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {

// Now apply the autoref. We have to extract the region out of
// the final ref type we got.
let ty::Ref(r_borrow, _, _) = ty.kind() else {
let ty::Ref(..) = ty.kind() else {
span_bug!(span, "expected a ref type, got {:?}", ty);
};
let mutbl = AutoBorrowMutability::new(mutbl_b, self.allow_two_phase);
adjustments.push(Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(*r_borrow, mutbl)),
target: ty,
});
adjustments.push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)), target: ty });

debug!("coerce_borrowed_pointer: succeeded ty={:?} adjustments={:?}", ty, adjustments);

Expand Down Expand Up @@ -547,7 +544,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
let mutbl = AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);

Some((Adjustment { kind: Adjust::Deref(None), target: ty_a }, Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(r_borrow, mutbl)),
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
target: Ty::new_ref(self.tcx, r_borrow, ty_a, mutbl_b),
}))
}
Expand Down Expand Up @@ -827,7 +824,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
};

let (pin, a_region, a_ty, mut_a) = extract_pin_mut(a)?;
let (_, b_region, _b_ty, mut_b) = extract_pin_mut(b)?;
let (_, _, _b_ty, mut_b) = extract_pin_mut(b)?;

coerce_mutbls(mut_a, mut_b)?;

Expand All @@ -841,7 +838,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
// To complete the reborrow, we need to make sure we can unify the inner types, and if so we
// add the adjustments.
self.unify_and(a, b, |_inner_ty| {
vec![Adjustment { kind: Adjust::ReborrowPin(b_region, mut_b), target: b }]
vec![Adjustment { kind: Adjust::ReborrowPin(mut_b), target: b }]
})
}

Expand Down Expand Up @@ -1321,7 +1318,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let noop = match self.typeck_results.borrow().expr_adjustments(expr) {
&[
Adjustment { kind: Adjust::Deref(_), .. },
Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl_adj)), .. },
Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mutbl_adj)), .. },
] => {
match *self.node_ty(expr.hir_id).kind() {
ty::Ref(_, _, mt_orig) => {
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_hir_typeck/src/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
self.walk_autoref(expr, &place_with_id, autoref);
}

adjustment::Adjust::ReborrowPin(_, mutbl) => {
adjustment::Adjust::ReborrowPin(mutbl) => {
// Reborrowing a Pin is like a combinations of a deref and a borrow, so we do
// both.
let bk = match mutbl {
Expand All @@ -804,15 +804,15 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
&self,
expr: &hir::Expr<'_>,
base_place: &PlaceWithHirId<'tcx>,
autoref: &adjustment::AutoBorrow<'tcx>,
autoref: &adjustment::AutoBorrow,
) {
debug!(
"walk_autoref(expr.hir_id={} base_place={:?} autoref={:?})",
expr.hir_id, base_place, autoref
);

match *autoref {
adjustment::AutoBorrow::Ref(_, m) => {
adjustment::AutoBorrow::Ref(m) => {
self.delegate.borrow_mut().borrow(
base_place,
base_place.hir_id,
Expand Down Expand Up @@ -1283,7 +1283,12 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
adjustment::Adjust::Deref(overloaded) => {
// Equivalent to *expr or something similar.
let base = if let Some(deref) = overloaded {
let ref_ty = Ty::new_ref(self.cx.tcx(), deref.region, target, deref.mutbl);
let ref_ty = Ty::new_ref(
self.cx.tcx(),
self.cx.tcx().lifetimes.re_erased,
target,
deref.mutbl,
);
self.cat_rvalue(expr.hir_id, ref_ty)
} else {
previous()?
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let autoborrow_mut = adj.iter().any(|adj| {
matches!(adj, &Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. })),
kind: Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Mut { .. })),
..
})
});
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,8 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
// for two-phase borrows.
let mutbl = AutoBorrowMutability::new(mutbl, AllowTwoPhase::Yes);

adjustments.push(Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
target,
});
adjustments
.push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)), target });

if unsize {
let unsized_ty = if let ty::Array(elem_ty, _) = base_ty.kind() {
Expand Down Expand Up @@ -250,7 +248,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
_ => bug!("Cannot adjust receiver type for reborrowing pin of {target:?}"),
};

adjustments.push(Adjustment { kind: Adjust::ReborrowPin(region, mutbl), target });
adjustments.push(Adjustment { kind: Adjust::ReborrowPin(mutbl), target });
}
None => {}
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_hir_typeck/src/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,23 +256,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Ok(method) => {
let by_ref_binop = !op.node.is_by_value();
if is_assign == IsAssign::Yes || by_ref_binop {
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].kind() {
if let ty::Ref(_, _, mutbl) = method.sig.inputs()[0].kind() {
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::Yes);
let autoref = Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
target: method.sig.inputs()[0],
};
self.apply_adjustments(lhs_expr, vec![autoref]);
}
}
if by_ref_binop {
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[1].kind() {
if let ty::Ref(_, _, mutbl) = method.sig.inputs()[1].kind() {
// Allow two-phase borrows for binops in initial deployment
// since they desugar to methods
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::Yes);

let autoref = Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
target: method.sig.inputs()[1],
};
// HACK(eddyb) Bypass checks due to reborrows being in
Expand Down
18 changes: 11 additions & 7 deletions compiler/rustc_hir_typeck/src/place_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let ok = self.try_overloaded_deref(expr.span, oprnd_ty)?;
let method = self.register_infer_ok_obligations(ok);
if let ty::Ref(region, _, hir::Mutability::Not) = method.sig.inputs()[0].kind() {
if let ty::Ref(_, _, hir::Mutability::Not) = method.sig.inputs()[0].kind() {
self.apply_adjustments(oprnd_expr, vec![Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(*region, AutoBorrowMutability::Not)),
kind: Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Not)),
target: method.sig.inputs()[0],
}]);
} else {
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut adjustments = self.adjust_steps(autoderef);
if let ty::Ref(region, _, hir::Mutability::Not) = method.sig.inputs()[0].kind() {
adjustments.push(Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(*region, AutoBorrowMutability::Not)),
kind: Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Not)),
target: Ty::new_imm_ref(self.tcx, *region, adjusted_ty),
});
} else {
Expand Down Expand Up @@ -289,9 +289,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
{
let method = self.register_infer_ok_obligations(ok);
if let ty::Ref(region, _, mutbl) = *method.sig.output().kind() {
*deref = OverloadedDeref { region, mutbl, span: deref.span };
}
let ty::Ref(_, _, mutbl) = *method.sig.output().kind() else {
span_bug!(
self.tcx.def_span(method.def_id),
"expected DerefMut to return a &mut"
);
};
*deref = OverloadedDeref { mutbl, span: deref.span };
// If this is a union field, also throw an error for `DerefMut` of `ManuallyDrop` (see RFC 2514).
// This helps avoid accidental drops.
if inside_union
Expand Down Expand Up @@ -390,7 +394,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// not the case today.
allow_two_phase_borrow: AllowTwoPhase::No,
};
adjustment.kind = Adjust::Borrow(AutoBorrow::Ref(*region, mutbl));
adjustment.kind = Adjust::Borrow(AutoBorrow::Ref(mutbl));
adjustment.target = Ty::new_ref(self.tcx, *region, source, mutbl.into());
}
source = adjustment.target;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAllocation {
}

for adj in cx.typeck_results().expr_adjustments(e) {
if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(_, m)) = adj.kind {
if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(m)) = adj.kind {
match m {
adjustment::AutoBorrowMutability::Not => {
cx.emit_span_lint(UNUSED_ALLOCATION, e.span, UnusedAllocationDiag);
Expand Down
21 changes: 10 additions & 11 deletions compiler/rustc_middle/src/ty/adjustment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub enum PointerCoercion {
/// `Box<[i32]>` is an `Adjust::Unsize` with the target `Box<[i32]>`.
#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
pub struct Adjustment<'tcx> {
pub kind: Adjust<'tcx>,
pub kind: Adjust,
pub target: Ty<'tcx>,
}

Expand All @@ -93,20 +93,20 @@ impl<'tcx> Adjustment<'tcx> {
}

#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
pub enum Adjust<'tcx> {
pub enum Adjust {
/// Go from ! to any type.
NeverToAny,

/// Dereference once, producing a place.
Deref(Option<OverloadedDeref<'tcx>>),
Deref(Option<OverloadedDeref>),

/// Take the address and produce either a `&` or `*` pointer.
Borrow(AutoBorrow<'tcx>),
Borrow(AutoBorrow),

Pointer(PointerCoercion),

/// Take a pinned reference and reborrow as a `Pin<&mut T>` or `Pin<&T>`.
ReborrowPin(ty::Region<'tcx>, hir::Mutability),
ReborrowPin(hir::Mutability),
}

/// An overloaded autoderef step, representing a `Deref(Mut)::deref(_mut)`
Expand All @@ -115,17 +115,16 @@ pub enum Adjust<'tcx> {
/// being those shared by both the receiver and the returned reference.
#[derive(Copy, Clone, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
#[derive(TypeFoldable, TypeVisitable)]
pub struct OverloadedDeref<'tcx> {
pub region: ty::Region<'tcx>,
pub struct OverloadedDeref {
pub mutbl: hir::Mutability,
/// The `Span` associated with the field access or method call
/// that triggered this overloaded deref.
pub span: Span,
}

impl<'tcx> OverloadedDeref<'tcx> {
impl OverloadedDeref {
/// Get the zst function item type for this method call.
pub fn method_call(&self, tcx: TyCtxt<'tcx>, source: Ty<'tcx>) -> Ty<'tcx> {
pub fn method_call<'tcx>(&self, tcx: TyCtxt<'tcx>, source: Ty<'tcx>) -> Ty<'tcx> {
let trait_def_id = match self.mutbl {
hir::Mutability::Not => tcx.require_lang_item(LangItem::Deref, None),
hir::Mutability::Mut => tcx.require_lang_item(LangItem::DerefMut, None),
Expand Down Expand Up @@ -187,9 +186,9 @@ impl From<AutoBorrowMutability> for hir::Mutability {

#[derive(Copy, Clone, PartialEq, Debug, TyEncodable, TyDecodable, HashStable)]
#[derive(TypeFoldable, TypeVisitable)]
pub enum AutoBorrow<'tcx> {
pub enum AutoBorrow {
/// Converts from T to &T.
Ref(ty::Region<'tcx>, AutoBorrowMutability),
Ref(AutoBorrowMutability),

/// Converts from T to *T.
RawPtr(hir::Mutability),
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<'tcx> Cx<'tcx> {

expr = Expr {
temp_lifetime,
ty: Ty::new_ref(self.tcx, deref.region, expr.ty, deref.mutbl),
ty: Ty::new_ref(self.tcx, self.tcx.lifetimes.re_erased, expr.ty, deref.mutbl),
span,
kind: ExprKind::Borrow {
borrow_kind: deref.mutbl.to_borrow_kind(),
Expand All @@ -152,14 +152,14 @@ impl<'tcx> Cx<'tcx> {

self.overloaded_place(hir_expr, adjustment.target, Some(call), expr, deref.span)
}
Adjust::Borrow(AutoBorrow::Ref(_, m)) => ExprKind::Borrow {
Adjust::Borrow(AutoBorrow::Ref(m)) => ExprKind::Borrow {
borrow_kind: m.to_borrow_kind(),
arg: self.thir.exprs.push(expr),
},
Adjust::Borrow(AutoBorrow::RawPtr(mutability)) => {
ExprKind::RawBorrow { mutability, arg: self.thir.exprs.push(expr) }
}
Adjust::ReborrowPin(region, mutbl) => {
Adjust::ReborrowPin(mutbl) => {
debug!("apply ReborrowPin adjustment");
// Rewrite `$expr` as `Pin { __pointer: &(mut)? *($expr).__pointer }`

Expand Down Expand Up @@ -197,7 +197,8 @@ impl<'tcx> Cx<'tcx> {
hir::Mutability::Mut => BorrowKind::Mut { kind: mir::MutBorrowKind::Default },
hir::Mutability::Not => BorrowKind::Shared,
};
let new_pin_target = Ty::new_ref(self.tcx, region, ptr_target_ty, mutbl);
let new_pin_target =
Ty::new_ref(self.tcx, self.tcx.lifetimes.re_erased, ptr_target_ty, mutbl);
let expr = self.thir.exprs.push(Expr {
temp_lifetime,
ty: new_pin_target,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
] => "",
[
..,
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mut_)),
target: _,
},
Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mut_)), target: _ },
] => hir::Mutability::from(*mut_).ref_prefix_str(),
_ => "",
};
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
let (required_refs, msg) = if can_auto_borrow {
(1, if deref_count == 1 { borrow_msg } else { deref_msg })
} else if let Some(&Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutability)),
kind: Adjust::Borrow(AutoBorrow::Ref(mutability)),
..
}) = next_adjust
&& matches!(mutability, AutoBorrowMutability::Mut { .. })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<
[] => AdjustKind::None,
&[
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
..
},
] => AdjustKind::borrow(mutbl),
Expand All @@ -62,7 +62,7 @@ pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<
kind: Adjust::Deref(_), ..
},
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
target,
},
] => {
Expand Down
Loading

0 comments on commit 9ccfedf

Please sign in to comment.