Skip to content

Commit 704bbe5

Browse files
committed
Move an extension trait method onto the type directly and reuse it
1 parent 7e2e3d4 commit 704bbe5

File tree

5 files changed

+36
-46
lines changed

5 files changed

+36
-46
lines changed

compiler/rustc_infer/src/traits/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,21 @@ impl<'tcx> PredicateObligation<'tcx> {
6969
}
7070
}
7171

72-
impl TraitObligation<'_> {
72+
impl<'tcx> TraitObligation<'tcx> {
7373
/// Returns `true` if the trait predicate is considered `const` in its ParamEnv.
7474
pub fn is_const(&self) -> bool {
7575
match (self.predicate.skip_binder().constness, self.param_env.constness()) {
7676
(ty::BoundConstness::ConstIfConst, hir::Constness::Const) => true,
7777
_ => false,
7878
}
7979
}
80+
81+
pub fn derived_cause(
82+
&self,
83+
variant: fn(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>,
84+
) -> ObligationCause<'tcx> {
85+
self.cause.clone().derived_cause(self.predicate, variant)
86+
}
8087
}
8188

8289
// `PredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.

compiler/rustc_middle/src/traits/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,30 @@ impl<'tcx> ObligationCause<'tcx> {
179179
None => Lrc::new(MISC_OBLIGATION_CAUSE_CODE),
180180
}));
181181
}
182+
183+
pub fn derived_cause(
184+
mut self,
185+
parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
186+
variant: fn(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>,
187+
) -> ObligationCause<'tcx> {
188+
/*!
189+
* Creates a cause for obligations that are derived from
190+
* `obligation` by a recursive search (e.g., for a builtin
191+
* bound, or eventually a `auto trait Foo`). If `obligation`
192+
* is itself a derived obligation, this is just a clone, but
193+
* otherwise we create a "derived obligation" cause so as to
194+
* keep track of the original root obligation for error
195+
* reporting.
196+
*/
197+
198+
// NOTE(flaper87): As of now, it keeps track of the whole error
199+
// chain. Ideally, we should have a way to configure this either
200+
// by using -Z verbose or just a CLI argument.
201+
self.map_code(|parent_code| {
202+
variant(DerivedObligationCause { parent_trait_pred, parent_code }).into()
203+
});
204+
self
205+
}
182206
}
183207

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

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_middle::ty::{ToPolyTraitRef, ToPredicate};
1717
use rustc_span::def_id::DefId;
1818

1919
use crate::traits::project::{normalize_with_depth, normalize_with_depth_to};
20-
use crate::traits::select::TraitObligationExt;
2120
use crate::traits::util::{self, closure_trait_ref_and_return_type, predicate_for_trait_def};
2221
use crate::traits::{
2322
BuiltinDerivedObligation, DerivedObligationCause, ImplDerivedObligation,

compiler/rustc_trait_selection/src/traits/select/mod.rs

-36
Original file line numberDiff line numberDiff line change
@@ -2342,42 +2342,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
23422342
}
23432343
}
23442344

2345-
trait TraitObligationExt<'tcx> {
2346-
fn derived_cause(
2347-
&self,
2348-
variant: fn(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>,
2349-
) -> ObligationCause<'tcx>;
2350-
}
2351-
2352-
impl<'tcx> TraitObligationExt<'tcx> for TraitObligation<'tcx> {
2353-
fn derived_cause(
2354-
&self,
2355-
variant: fn(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>,
2356-
) -> ObligationCause<'tcx> {
2357-
/*!
2358-
* Creates a cause for obligations that are derived from
2359-
* `obligation` by a recursive search (e.g., for a builtin
2360-
* bound, or eventually a `auto trait Foo`). If `obligation`
2361-
* is itself a derived obligation, this is just a clone, but
2362-
* otherwise we create a "derived obligation" cause so as to
2363-
* keep track of the original root obligation for error
2364-
* reporting.
2365-
*/
2366-
2367-
let obligation = self;
2368-
2369-
// NOTE(flaper87): As of now, it keeps track of the whole error
2370-
// chain. Ideally, we should have a way to configure this either
2371-
// by using -Z verbose or just a CLI argument.
2372-
let derived_cause = DerivedObligationCause {
2373-
parent_trait_pred: obligation.predicate,
2374-
parent_code: obligation.cause.clone_code(),
2375-
};
2376-
let derived_code = variant(derived_cause);
2377-
ObligationCause::new(obligation.cause.span, obligation.cause.body_id, derived_code)
2378-
}
2379-
}
2380-
23812345
impl<'o, 'tcx> TraitObligationStack<'o, 'tcx> {
23822346
fn list(&'o self) -> TraitObligationStackList<'o, 'tcx> {
23832347
TraitObligationStackList::with(self)

compiler/rustc_trait_selection/src/traits/wf.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,10 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
301301

302302
let extend = |traits::PredicateObligation { predicate, mut cause, .. }| {
303303
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-
});
304+
cause = cause.derived_cause(
305+
parent_trait_pred,
306+
traits::ObligationCauseCode::DerivedObligation,
307+
);
312308
}
313309
extend_cause_with_original_assoc_item_obligation(
314310
tcx, trait_ref, item, &mut cause, predicate,

0 commit comments

Comments
 (0)