Skip to content

Commit 787cd54

Browse files
committed
Make traits::util::* free functions.
1 parent 56a0aec commit 787cd54

File tree

10 files changed

+189
-174
lines changed

10 files changed

+189
-174
lines changed

src/librustc/traits/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind;
6464
pub use self::specialize::{specialization_graph, translate_substs, OverlapError};
6565
pub use self::util::{elaborate_predicates, elaborate_trait_ref, elaborate_trait_refs};
6666
pub use self::util::{expand_trait_aliases, TraitAliasExpander};
67+
pub use self::util::{
68+
get_vtable_index_of_object_method, impl_is_default, impl_item_is_final,
69+
predicate_for_trait_def, upcast_choices,
70+
};
6771
pub use self::util::{
6872
supertrait_def_ids, supertraits, transitive_bounds, SupertraitDefIds, Supertraits,
6973
};

src/librustc/traits/project.rs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
10571057
node_item.item.defaultness.has_value()
10581058
} else {
10591059
node_item.item.defaultness.is_default()
1060-
|| selcx.tcx().impl_is_default(node_item.node.def_id())
1060+
|| super::util::impl_is_default(selcx.tcx(), node_item.node.def_id())
10611061
};
10621062

10631063
// Only reveal a specializable default if we're past type-checking
@@ -1263,26 +1263,30 @@ fn confirm_generator_candidate<'cx, 'tcx>(
12631263

12641264
let gen_def_id = tcx.lang_items().gen_trait().unwrap();
12651265

1266-
let predicate = tcx
1267-
.generator_trait_ref_and_outputs(gen_def_id, obligation.predicate.self_ty(), gen_sig)
1268-
.map_bound(|(trait_ref, yield_ty, return_ty)| {
1269-
let name = tcx.associated_item(obligation.predicate.item_def_id).ident.name;
1270-
let ty = if name == sym::Return {
1271-
return_ty
1272-
} else if name == sym::Yield {
1273-
yield_ty
1274-
} else {
1275-
bug!()
1276-
};
1266+
let predicate = super::util::generator_trait_ref_and_outputs(
1267+
tcx,
1268+
gen_def_id,
1269+
obligation.predicate.self_ty(),
1270+
gen_sig,
1271+
)
1272+
.map_bound(|(trait_ref, yield_ty, return_ty)| {
1273+
let name = tcx.associated_item(obligation.predicate.item_def_id).ident.name;
1274+
let ty = if name == sym::Return {
1275+
return_ty
1276+
} else if name == sym::Yield {
1277+
yield_ty
1278+
} else {
1279+
bug!()
1280+
};
12771281

1278-
ty::ProjectionPredicate {
1279-
projection_ty: ty::ProjectionTy {
1280-
substs: trait_ref.substs,
1281-
item_def_id: obligation.predicate.item_def_id,
1282-
},
1283-
ty: ty,
1284-
}
1285-
});
1282+
ty::ProjectionPredicate {
1283+
projection_ty: ty::ProjectionTy {
1284+
substs: trait_ref.substs,
1285+
item_def_id: obligation.predicate.item_def_id,
1286+
},
1287+
ty: ty,
1288+
}
1289+
});
12861290

12871291
confirm_param_env_candidate(selcx, obligation, predicate)
12881292
.with_addl_obligations(vtable.nested)
@@ -1349,21 +1353,21 @@ fn confirm_callable_candidate<'cx, 'tcx>(
13491353
// the `Output` associated type is declared on `FnOnce`
13501354
let fn_once_def_id = tcx.lang_items().fn_once_trait().unwrap();
13511355

1352-
let predicate = tcx
1353-
.closure_trait_ref_and_return_type(
1354-
fn_once_def_id,
1355-
obligation.predicate.self_ty(),
1356-
fn_sig,
1357-
flag,
1358-
)
1359-
.map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
1360-
projection_ty: ty::ProjectionTy::from_ref_and_name(
1361-
tcx,
1362-
trait_ref,
1363-
Ident::with_dummy_span(FN_OUTPUT_NAME),
1364-
),
1365-
ty: ret_type,
1366-
});
1356+
let predicate = super::util::closure_trait_ref_and_return_type(
1357+
tcx,
1358+
fn_once_def_id,
1359+
obligation.predicate.self_ty(),
1360+
fn_sig,
1361+
flag,
1362+
)
1363+
.map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
1364+
projection_ty: ty::ProjectionTy::from_ref_and_name(
1365+
tcx,
1366+
trait_ref,
1367+
Ident::with_dummy_span(FN_OUTPUT_NAME),
1368+
),
1369+
ty: ret_type,
1370+
});
13671371

13681372
confirm_param_env_candidate(selcx, obligation, predicate)
13691373
}

src/librustc/traits/select.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use super::coherence::{self, Conflict};
1111
use super::project;
1212
use super::project::{normalize_with_depth, Normalized, ProjectionCacheKey};
1313
use super::util;
14+
use super::util::{closure_trait_ref_and_return_type, predicate_for_trait_def};
1415
use super::DerivedObligationCause;
1516
use super::Selection;
1617
use super::SelectionResult;
@@ -2651,7 +2652,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
26512652
recursion_depth,
26522653
&skol_ty,
26532654
);
2654-
let skol_obligation = self.tcx().predicate_for_trait_def(
2655+
let skol_obligation = predicate_for_trait_def(
2656+
self.tcx(),
26552657
param_env,
26562658
cause.clone(),
26572659
trait_def_id,
@@ -2988,7 +2990,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
29882990
// we pass over, we sum up the set of number of vtable
29892991
// entries, so that we can compute the offset for the selected
29902992
// trait.
2991-
vtable_base = nonmatching.map(|t| tcx.count_own_vtable_entries(t)).sum();
2993+
vtable_base = nonmatching.map(|t| super::util::count_own_vtable_entries(tcx, t)).sum();
29922994
}
29932995

29942996
VtableObjectData { upcast_trait_ref: upcast_trait_ref.unwrap(), vtable_base, nested }
@@ -3003,15 +3005,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
30033005
// Okay to skip binder; it is reintroduced below.
30043006
let self_ty = self.infcx.shallow_resolve(*obligation.self_ty().skip_binder());
30053007
let sig = self_ty.fn_sig(self.tcx());
3006-
let trait_ref = self
3007-
.tcx()
3008-
.closure_trait_ref_and_return_type(
3009-
obligation.predicate.def_id(),
3010-
self_ty,
3011-
sig,
3012-
util::TupleArgumentsFlag::Yes,
3013-
)
3014-
.map_bound(|(trait_ref, _)| trait_ref);
3008+
let trait_ref = closure_trait_ref_and_return_type(
3009+
self.tcx(),
3010+
obligation.predicate.def_id(),
3011+
self_ty,
3012+
sig,
3013+
util::TupleArgumentsFlag::Yes,
3014+
)
3015+
.map_bound(|(trait_ref, _)| trait_ref);
30153016

30163017
let Normalized { value: trait_ref, obligations } = project::normalize_with_depth(
30173018
self,
@@ -3381,7 +3382,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
33813382
nested.extend(obligations);
33823383

33833384
// Construct the nested `Field<T>: Unsize<Field<U>>` predicate.
3384-
nested.push(tcx.predicate_for_trait_def(
3385+
nested.push(predicate_for_trait_def(
3386+
tcx,
33853387
obligation.param_env,
33863388
obligation.cause.clone(),
33873389
obligation.predicate.def_id(),
@@ -3416,7 +3418,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
34163418
nested.extend(obligations);
34173419

34183420
// Construct the nested `T: Unsize<U>` predicate.
3419-
nested.push(tcx.predicate_for_trait_def(
3421+
nested.push(predicate_for_trait_def(
3422+
tcx,
34203423
obligation.param_env,
34213424
obligation.cause.clone(),
34223425
obligation.predicate.def_id(),
@@ -3627,14 +3630,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
36273630
// in fact unparameterized (or at least does not reference any
36283631
// regions bound in the obligation). Still probably some
36293632
// refactoring could make this nicer.
3630-
self.tcx()
3631-
.closure_trait_ref_and_return_type(
3632-
obligation.predicate.def_id(),
3633-
obligation.predicate.skip_binder().self_ty(), // (1)
3634-
closure_type,
3635-
util::TupleArgumentsFlag::No,
3636-
)
3637-
.map_bound(|(trait_ref, _)| trait_ref)
3633+
closure_trait_ref_and_return_type(
3634+
self.tcx(),
3635+
obligation.predicate.def_id(),
3636+
obligation.predicate.skip_binder().self_ty(), // (1)
3637+
closure_type,
3638+
util::TupleArgumentsFlag::No,
3639+
)
3640+
.map_bound(|(trait_ref, _)| trait_ref)
36383641
}
36393642

36403643
fn generator_trait_ref_unnormalized(
@@ -3651,13 +3654,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
36513654
// regions bound in the obligation). Still probably some
36523655
// refactoring could make this nicer.
36533656

3654-
self.tcx()
3655-
.generator_trait_ref_and_outputs(
3656-
obligation.predicate.def_id(),
3657-
obligation.predicate.skip_binder().self_ty(), // (1)
3658-
gen_sig,
3659-
)
3660-
.map_bound(|(trait_ref, ..)| trait_ref)
3657+
super::util::generator_trait_ref_and_outputs(
3658+
self.tcx(),
3659+
obligation.predicate.def_id(),
3660+
obligation.predicate.skip_binder().self_ty(), // (1)
3661+
gen_sig,
3662+
)
3663+
.map_bound(|(trait_ref, ..)| trait_ref)
36613664
}
36623665

36633666
/// Returns the obligations that are implied by instantiating an

0 commit comments

Comments
 (0)