2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::traits;
|
|
|
|
use rustc_middle::ty::adjustment::CustomCoerceUnsized;
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2017-10-25 14:14:51 +00:00
|
|
|
|
2020-08-18 10:47:27 +00:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2020-05-14 21:06:44 +00:00
|
|
|
|
2017-10-25 13:39:54 +00:00
|
|
|
pub mod collector;
|
2017-11-23 15:02:02 +00:00
|
|
|
pub mod partitioning;
|
2020-06-22 12:57:03 +00:00
|
|
|
pub mod polymorphize;
|
2017-10-25 14:14:51 +00:00
|
|
|
|
2019-06-11 21:11:55 +00:00
|
|
|
pub fn custom_coerce_unsize_info<'tcx>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 21:11:55 +00:00
|
|
|
source_ty: Ty<'tcx>,
|
|
|
|
target_ty: Ty<'tcx>,
|
|
|
|
) -> CustomCoerceUnsized {
|
2020-08-18 10:47:27 +00:00
|
|
|
let def_id = tcx.require_lang_item(LangItem::CoerceUnsized, None);
|
2017-10-25 14:14:51 +00:00
|
|
|
|
2018-04-25 02:45:49 +00:00
|
|
|
let trait_ref = ty::Binder::bind(ty::TraitRef {
|
2020-03-06 18:28:44 +00:00
|
|
|
def_id,
|
2019-12-22 22:42:04 +00:00
|
|
|
substs: tcx.mk_substs_trait(source_ty, &[target_ty.into()]),
|
2017-10-25 14:14:51 +00:00
|
|
|
});
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
match tcx.codegen_fulfill_obligation((ty::ParamEnv::reveal_all(), trait_ref)) {
|
2020-06-02 15:54:24 +00:00
|
|
|
Ok(traits::ImplSourceUserDefined(traits::ImplSourceUserDefinedData {
|
|
|
|
impl_def_id,
|
|
|
|
..
|
|
|
|
})) => tcx.coerce_unsized_info(impl_def_id).custom_kind.unwrap(),
|
2020-05-11 15:25:33 +00:00
|
|
|
impl_source => {
|
|
|
|
bug!("invalid `CoerceUnsized` impl_source: {:?}", impl_source);
|
2017-10-25 14:14:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|