Auto merge of #132301 - compiler-errors:adjust, r=lcnr

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.
This commit is contained in:
bors 2024-10-31 10:17:49 +00:00
commit 9ccfedf186
18 changed files with 65 additions and 64 deletions

View File

@ -1525,7 +1525,6 @@ pub(crate) fn report_move_out_while_borrowed(
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

View File

@ -50,8 +50,9 @@ pub(crate) fn adjust_steps_as_infer_ok(
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
}

View File

@ -307,7 +307,7 @@ fn try_overloaded_call_traits(
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")
};
@ -317,7 +317,7 @@ fn try_overloaded_call_traits(
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],
});
}

View File

@ -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 }]
}
@ -484,14 +484,11 @@ fn coerce_borrowed_pointer(
// 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);
@ -547,7 +544,7 @@ fn coerce_unsized(&self, mut source: Ty<'tcx>, mut target: Ty<'tcx>) -> CoerceRe
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),
}))
}
@ -827,7 +824,7 @@ fn coerce_pin(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'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)?;
@ -841,7 +838,7 @@ fn coerce_pin(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'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 }]
})
}
@ -1321,7 +1318,7 @@ fn try_find_coercion_lub<E>(
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) => {

View File

@ -781,7 +781,7 @@ fn walk_adjustment(&self, expr: &hir::Expr<'_>) -> Result<(), Cx::Error> {
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 {
@ -804,7 +804,7 @@ fn walk_autoref(
&self,
expr: &hir::Expr<'_>,
base_place: &PlaceWithHirId<'tcx>,
autoref: &adjustment::AutoBorrow<'tcx>,
autoref: &adjustment::AutoBorrow,
) {
debug!(
"walk_autoref(expr.hir_id={} base_place={:?} autoref={:?})",
@ -812,7 +812,7 @@ fn walk_autoref(
);
match *autoref {
adjustment::AutoBorrow::Ref(_, m) => {
adjustment::AutoBorrow::Ref(m) => {
self.delegate.borrow_mut().borrow(
base_place,
base_place.hir_id,
@ -1283,7 +1283,12 @@ fn cat_expr_adjusted_with<F>(
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()?

View File

@ -264,7 +264,7 @@ pub(crate) fn apply_adjustments(&self, expr: &hir::Expr<'_>, adj: Vec<Adjustment
let autoborrow_mut = adj.iter().any(|adj| {
matches!(adj, &Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. })),
kind: Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Mut { .. })),
..
})
});

View File

@ -200,10 +200,8 @@ fn adjust_self_ty(
// 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() {
@ -250,7 +248,7 @@ fn adjust_self_ty(
_ => 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 => {}
}

View File

@ -256,23 +256,23 @@ fn check_overloaded_binop(
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

View File

@ -29,9 +29,9 @@ pub(super) fn lookup_derefing(
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 {
@ -158,7 +158,7 @@ fn try_index_step(
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 {
@ -289,9 +289,13 @@ pub(crate) fn convert_place_derefs_to_mutable(&self, expr: &hir::Expr<'_>) {
)
{
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
@ -390,7 +394,7 @@ fn convert_place_op_to_mutable(
// 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;

View File

@ -1610,7 +1610,7 @@ fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
}
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);

View File

@ -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>,
}
@ -93,20 +93,20 @@ pub fn is_region_borrow(&self) -> bool {
}
#[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)`
@ -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),
@ -187,9 +186,9 @@ fn from(m: AutoBorrowMutability) -> Self {
#[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),

View File

@ -140,7 +140,7 @@ fn apply_adjustment(
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(),
@ -152,14 +152,14 @@ fn apply_adjustment(
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 }`
@ -197,7 +197,8 @@ fn apply_adjustment(
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,

View File

@ -551,10 +551,7 @@ pub fn emit_inference_failure_err(
] => "",
[
..,
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mut_)),
target: _,
},
Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mut_)), target: _ },
] => hir::Mutability::from(*mut_).ref_prefix_str(),
_ => "",
};

View File

@ -418,7 +418,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
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 { .. })

View File

@ -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),
@ -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,
},
] => {

View File

@ -199,7 +199,7 @@ fn is_ref_iterable<'tcx>(
kind: Adjust::Deref(_), ..
},
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
target,
},
..,
@ -236,7 +236,7 @@ fn is_ref_iterable<'tcx>(
},
&[
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
target,
},
..,

View File

@ -137,7 +137,7 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
_ if matches!(
typeck.expr_adjustments(prev_expr).first(),
Some(Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Not))
kind: Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Not))
| Adjust::Deref(_),
..
})
@ -230,7 +230,7 @@ fn check_use<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> (UseKind<'tcx>,
if use_cx
.adjustments
.first()
.is_some_and(|a| matches!(a.kind, Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Not)))) =>
.is_some_and(|a| matches!(a.kind, Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Not)))) =>
{
UseKind::AutoBorrowed
},

View File

@ -110,7 +110,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
) || cx.typeck_results().expr_adjustments(expr).first().map_or(false, |a| {
matches!(
a.kind,
Adjust::Borrow(AutoBorrow::Ref(_, AutoBorrowMutability::Mut { .. }))
Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Mut { .. }))
)
}) || (matches!(
cx.typeck_results().expr_ty(indexed).ref_mutability(),