Skip to content

Commit 312d27d

Browse files
committed
Remove some unnecessary clones
1 parent 2ea2ced commit 312d27d

File tree

3 files changed

+31
-26
lines changed
  • compiler
    • rustc_middle/src/traits
    • rustc_trait_selection/src/traits
    • rustc_typeck/src/check/fn_ctxt

3 files changed

+31
-26
lines changed

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@ impl<'tcx> ObligationCause<'tcx> {
143143
ObligationCause { span, body_id: hir::CRATE_HIR_ID, code: None }
144144
}
145145

146-
pub fn make_mut_code(&mut self) -> &mut ObligationCauseCode<'tcx> {
147-
Lrc::make_mut(self.code.get_or_insert_with(|| Lrc::new(MISC_OBLIGATION_CAUSE_CODE)))
148-
}
149-
150146
pub fn span(&self, tcx: TyCtxt<'tcx>) -> Span {
151147
match *self.code() {
152148
ObligationCauseCode::CompareImplMethodObligation { .. }
@@ -173,6 +169,16 @@ impl<'tcx> ObligationCause<'tcx> {
173169
None => Lrc::new(MISC_OBLIGATION_CAUSE_CODE),
174170
}
175171
}
172+
173+
pub fn map_code(
174+
&mut self,
175+
f: impl FnOnce(Lrc<ObligationCauseCode<'tcx>>) -> Lrc<ObligationCauseCode<'tcx>>,
176+
) {
177+
self.code = Some(f(match self.code.take() {
178+
Some(code) => code,
179+
None => Lrc::new(MISC_OBLIGATION_CAUSE_CODE),
180+
}));
181+
}
176182
}
177183

178184
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift)]

compiler/rustc_trait_selection/src/traits/wf.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -294,30 +294,28 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
294294
let obligations = self.nominal_obligations(trait_ref.def_id, trait_ref.substs);
295295

296296
debug!("compute_trait_ref obligations {:?}", obligations);
297-
let cause = self.cause(traits::MiscObligation);
298297
let param_env = self.param_env;
299298
let depth = self.recursion_depth;
300299

301300
let item = self.item;
302301

303-
let extend = |obligation: traits::PredicateObligation<'tcx>| {
304-
let mut cause = cause.clone();
305-
if let Some(parent_trait_pred) = obligation.predicate.to_opt_poly_trait_pred() {
306-
let derived_cause = traits::DerivedObligationCause {
307-
parent_trait_pred,
308-
parent_code: obligation.cause.clone_code(),
309-
};
310-
*cause.make_mut_code() =
311-
traits::ObligationCauseCode::DerivedObligation(derived_cause);
302+
let extend = |traits::PredicateObligation { predicate, mut cause, .. }| {
303+
if let Some(parent_trait_pred) = predicate.to_opt_poly_trait_pred() {
304+
cause.map_code(|parent_code| {
305+
{
306+
traits::ObligationCauseCode::DerivedObligation(
307+
traits::DerivedObligationCause { parent_trait_pred, parent_code },
308+
)
309+
}
310+
.into()
311+
});
312+
} else {
313+
cause = traits::ObligationCause::misc(self.span, self.body_id);
312314
}
313315
extend_cause_with_original_assoc_item_obligation(
314-
tcx,
315-
trait_ref,
316-
item,
317-
&mut cause,
318-
obligation.predicate,
316+
tcx, trait_ref, item, &mut cause, predicate,
319317
);
320-
traits::Obligation::with_depth(cause, depth, param_env, obligation.predicate)
318+
traits::Obligation::with_depth(cause, depth, param_env, predicate)
321319
};
322320

323321
if let Elaborate::All = elaborate {
@@ -339,17 +337,17 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
339337
})
340338
.filter(|(_, arg)| !arg.has_escaping_bound_vars())
341339
.map(|(i, arg)| {
342-
let mut new_cause = cause.clone();
340+
let mut cause = traits::ObligationCause::misc(self.span, self.body_id);
343341
// The first subst is the self ty - use the correct span for it.
344342
if i == 0 {
345343
if let Some(hir::ItemKind::Impl(hir::Impl { self_ty, .. })) =
346344
item.map(|i| &i.kind)
347345
{
348-
new_cause.span = self_ty.span;
346+
cause.span = self_ty.span;
349347
}
350348
}
351349
traits::Obligation::with_depth(
352-
new_cause,
350+
cause,
353351
depth,
354352
param_env,
355353
ty::Binder::dummy(ty::PredicateKind::WellFormed(arg)).to_predicate(tcx),

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,13 +1668,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16681668
// We make sure that only *one* argument matches the obligation failure
16691669
// and we assign the obligation's span to its expression's.
16701670
error.obligation.cause.span = args[ref_in].span;
1671-
let parent_code = error.obligation.cause.clone_code();
1672-
*error.obligation.cause.make_mut_code() =
1671+
error.obligation.cause.map_code(|parent_code| {
16731672
ObligationCauseCode::FunctionArgumentObligation {
16741673
arg_hir_id: args[ref_in].hir_id,
16751674
call_hir_id: expr.hir_id,
16761675
parent_code,
1677-
};
1676+
}
1677+
.into()
1678+
});
16781679
} else if error.obligation.cause.span == call_sp {
16791680
// Make function calls point at the callee, not the whole thing.
16801681
if let hir::ExprKind::Call(callee, _) = expr.kind {

0 commit comments

Comments
 (0)