Skip to main content

rustc_monomorphize/
lib.rs

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