1
Fork 0

Remove some unnecessary clones

This commit is contained in:
Oli Scherer 2022-05-10 08:43:39 +00:00
parent 2ea2ced2be
commit 312d27d0a2
3 changed files with 31 additions and 26 deletions

View file

@ -143,10 +143,6 @@ impl<'tcx> ObligationCause<'tcx> {
ObligationCause { span, body_id: hir::CRATE_HIR_ID, code: None } ObligationCause { span, body_id: hir::CRATE_HIR_ID, code: None }
} }
pub fn make_mut_code(&mut self) -> &mut ObligationCauseCode<'tcx> {
Lrc::make_mut(self.code.get_or_insert_with(|| Lrc::new(MISC_OBLIGATION_CAUSE_CODE)))
}
pub fn span(&self, tcx: TyCtxt<'tcx>) -> Span { pub fn span(&self, tcx: TyCtxt<'tcx>) -> Span {
match *self.code() { match *self.code() {
ObligationCauseCode::CompareImplMethodObligation { .. } ObligationCauseCode::CompareImplMethodObligation { .. }
@ -173,6 +169,16 @@ impl<'tcx> ObligationCause<'tcx> {
None => Lrc::new(MISC_OBLIGATION_CAUSE_CODE), None => Lrc::new(MISC_OBLIGATION_CAUSE_CODE),
} }
} }
pub fn map_code(
&mut self,
f: impl FnOnce(Lrc<ObligationCauseCode<'tcx>>) -> Lrc<ObligationCauseCode<'tcx>>,
) {
self.code = Some(f(match self.code.take() {
Some(code) => code,
None => Lrc::new(MISC_OBLIGATION_CAUSE_CODE),
}));
}
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)] #[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]

View file

@ -294,30 +294,28 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
let obligations = self.nominal_obligations(trait_ref.def_id, trait_ref.substs); let obligations = self.nominal_obligations(trait_ref.def_id, trait_ref.substs);
debug!("compute_trait_ref obligations {:?}", obligations); debug!("compute_trait_ref obligations {:?}", obligations);
let cause = self.cause(traits::MiscObligation);
let param_env = self.param_env; let param_env = self.param_env;
let depth = self.recursion_depth; let depth = self.recursion_depth;
let item = self.item; let item = self.item;
let extend = |obligation: traits::PredicateObligation<'tcx>| { let extend = |traits::PredicateObligation { predicate, mut cause, .. }| {
let mut cause = cause.clone(); if let Some(parent_trait_pred) = predicate.to_opt_poly_trait_pred() {
if let Some(parent_trait_pred) = obligation.predicate.to_opt_poly_trait_pred() { cause.map_code(|parent_code| {
let derived_cause = traits::DerivedObligationCause { {
parent_trait_pred, traits::ObligationCauseCode::DerivedObligation(
parent_code: obligation.cause.clone_code(), traits::DerivedObligationCause { parent_trait_pred, parent_code },
}; )
*cause.make_mut_code() = }
traits::ObligationCauseCode::DerivedObligation(derived_cause); .into()
});
} else {
cause = traits::ObligationCause::misc(self.span, self.body_id);
} }
extend_cause_with_original_assoc_item_obligation( extend_cause_with_original_assoc_item_obligation(
tcx, tcx, trait_ref, item, &mut cause, predicate,
trait_ref,
item,
&mut cause,
obligation.predicate,
); );
traits::Obligation::with_depth(cause, depth, param_env, obligation.predicate) traits::Obligation::with_depth(cause, depth, param_env, predicate)
}; };
if let Elaborate::All = elaborate { if let Elaborate::All = elaborate {
@ -339,17 +337,17 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
}) })
.filter(|(_, arg)| !arg.has_escaping_bound_vars()) .filter(|(_, arg)| !arg.has_escaping_bound_vars())
.map(|(i, arg)| { .map(|(i, arg)| {
let mut new_cause = cause.clone(); let mut cause = traits::ObligationCause::misc(self.span, self.body_id);
// The first subst is the self ty - use the correct span for it. // The first subst is the self ty - use the correct span for it.
if i == 0 { if i == 0 {
if let Some(hir::ItemKind::Impl(hir::Impl { self_ty, .. })) = if let Some(hir::ItemKind::Impl(hir::Impl { self_ty, .. })) =
item.map(|i| &i.kind) item.map(|i| &i.kind)
{ {
new_cause.span = self_ty.span; cause.span = self_ty.span;
} }
} }
traits::Obligation::with_depth( traits::Obligation::with_depth(
new_cause, cause,
depth, depth,
param_env, param_env,
ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)).to_predicate(tcx), ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)).to_predicate(tcx),

View file

@ -1668,13 +1668,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// We make sure that only *one* argument matches the obligation failure // We make sure that only *one* argument matches the obligation failure
// and we assign the obligation's span to its expression's. // and we assign the obligation's span to its expression's.
error.obligation.cause.span = args[ref_in].span; error.obligation.cause.span = args[ref_in].span;
let parent_code = error.obligation.cause.clone_code(); error.obligation.cause.map_code(|parent_code| {
*error.obligation.cause.make_mut_code() =
ObligationCauseCode::FunctionArgumentObligation { ObligationCauseCode::FunctionArgumentObligation {
arg_hir_id: args[ref_in].hir_id, arg_hir_id: args[ref_in].hir_id,
call_hir_id: expr.hir_id, call_hir_id: expr.hir_id,
parent_code, parent_code,
}; }
.into()
});
} else if error.obligation.cause.span == call_sp { } else if error.obligation.cause.span == call_sp {
// Make function calls point at the callee, not the whole thing. // Make function calls point at the callee, not the whole thing.
if let hir::ExprKind::Call(callee, _) = expr.kind { if let hir::ExprKind::Call(callee, _) = expr.kind {