Skip to main content

rustc_monomorphize/
lib.rs

1// tidy-alphabetical-start
2#![feature(file_buffered)]
3#![feature(if_let_guard)]
4#![feature(impl_trait_in_assoc_type)]
5#![feature(once_cell_get_mut)]
6// tidy-alphabetical-end
7
8use rustc_hir::lang_items::LangItem;
9use rustc_middle::query::TyCtxtAt;
10use rustc_middle::ty::adjustment::CustomCoerceUnsized;
11use rustc_middle::ty::{self, Ty};
12use rustc_middle::util::Providers;
13use rustc_middle::{bug, traits};
14use rustc_span::ErrorGuaranteed;
15
16mod collector;
17mod errors;
18mod graph_checks;
19mod mono_checks;
20mod partitioning;
21mod util;
22
23fn custom_coerce_unsize_info<'tcx>(
24    tcx: TyCtxtAt<'tcx>,
25    source_ty: Ty<'tcx>,
26    target_ty: Ty<'tcx>,
27) -> Result<CustomCoerceUnsized, ErrorGuaranteed> {
28    let trait_ref = ty::TraitRef::new(
29        tcx.tcx,
30        tcx.require_lang_item(LangItem::CoerceUnsized, tcx.span),
31        [source_ty, target_ty],
32    );
33
34    match tcx
35        .codegen_select_candidate(ty::TypingEnv::fully_monomorphized().as_query_input(trait_ref))
36    {
37        Ok(traits::ImplSource::UserDefined(traits::ImplSourceUserDefinedData {
38            impl_def_id,
39            ..
40        })) => Ok(tcx.coerce_unsized_info(impl_def_id)?.custom_kind.unwrap()),
41        impl_source => {
42            ::rustc_middle::util::bug::bug_fmt(format_args!("invalid `CoerceUnsized` from {1} to {2}: impl_source: {0:?}",
        impl_source, source_ty, target_ty));bug!(
43                "invalid `CoerceUnsized` from {source_ty} to {target_ty}: impl_source: {:?}",
44                impl_source
45            );
46        }
47    }
48}
49
50pub fn provide(providers: &mut Providers) {
51    partitioning::provide(providers);
52    mono_checks::provide(&mut providers.queries);
53}