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::attrs::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 diagnostics;
17mod graph_checks;
18mod mono_checks;
19mod offload;
20mod partitioning;
21mod util;
22
23// Exposed so `rustc_codegen_ssa::base::codegen_crate` can trigger the
24// host-metadata manifest write.
25pub use offload::manifest::write_host_metadata_offload_manifest;
26
27fn custom_coerce_unsize_info<'tcx>(
28    tcx: TyCtxtAt<'tcx>,
29    source_ty: Ty<'tcx>,
30    target_ty: Ty<'tcx>,
31) -> Result<CustomCoerceUnsized, ErrorGuaranteed> {
32    let trait_ref = ty::TraitRef::new(
33        tcx.tcx,
34        tcx.require_lang_item(LangItem::CoerceUnsized, tcx.span),
35        [source_ty, target_ty],
36    );
37
38    match tcx
39        .codegen_select_candidate(ty::TypingEnv::fully_monomorphized().as_query_input(trait_ref))
40    {
41        Ok(traits::ImplSource::UserDefined(traits::ImplSourceUserDefinedData {
42            impl_def_id,
43            ..
44        })) => Ok(tcx.coerce_unsized_info(*impl_def_id)?.custom_kind.unwrap()),
45        impl_source => {
46            ::rustc_middle::util::bug::bug_fmt(format_args!("invalid `CoerceUnsized` from {1} to {2}: impl_source: {0:?}",
        impl_source, source_ty, target_ty));bug!(
47                "invalid `CoerceUnsized` from {source_ty} to {target_ty}: impl_source: {:?}",
48                impl_source
49            );
50        }
51    }
52}
53
54pub fn provide(providers: &mut Providers) {
55    partitioning::provide(providers);
56    mono_checks::provide(&mut providers.queries);
57}