]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/codegen.rs
:arrow_up: rust-analyzer
[rust.git] / compiler / rustc_trait_selection / src / traits / codegen.rs
1 // This file contains various trait resolution methods used by codegen.
2 // They all assume regions can be erased and monomorphic types.  It
3 // seems likely that they should eventually be merged into more
4 // general routines.
5
6 use crate::infer::{DefiningAnchor, TyCtxtInferExt};
7 use crate::traits::error_reporting::TypeErrCtxtExt;
8 use crate::traits::{
9     ImplSource, Obligation, ObligationCause, SelectionContext, TraitEngine, TraitEngineExt,
10     Unimplemented,
11 };
12 use rustc_infer::traits::FulfillmentErrorCode;
13 use rustc_middle::traits::CodegenObligationError;
14 use rustc_middle::ty::{self, TyCtxt};
15
16 /// Attempts to resolve an obligation to an `ImplSource`. The result is
17 /// a shallow `ImplSource` resolution, meaning that we do not
18 /// (necessarily) resolve all nested obligations on the impl. Note
19 /// that type check should guarantee to us that all nested
20 /// obligations *could be* resolved if we wanted to.
21 ///
22 /// This also expects that `trait_ref` is fully normalized.
23 pub fn codegen_select_candidate<'tcx>(
24     tcx: TyCtxt<'tcx>,
25     (param_env, trait_ref): (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>),
26 ) -> Result<&'tcx ImplSource<'tcx, ()>, CodegenObligationError> {
27     // We expect the input to be fully normalized.
28     debug_assert_eq!(trait_ref, tcx.normalize_erasing_regions(param_env, trait_ref));
29
30     // Do the initial selection for the obligation. This yields the
31     // shallow result we are looking for -- that is, what specific impl.
32     let infcx = tcx
33         .infer_ctxt()
34         .ignoring_regions()
35         .with_opaque_type_inference(DefiningAnchor::Bubble)
36         .build();
37     //~^ HACK `Bubble` is required for
38     // this test to pass: type-alias-impl-trait/assoc-projection-ice.rs
39     let mut selcx = SelectionContext::new(&infcx);
40
41     let obligation_cause = ObligationCause::dummy();
42     let obligation =
43         Obligation::new(tcx, obligation_cause, param_env, trait_ref.to_poly_trait_predicate());
44
45     let selection = match selcx.select(&obligation) {
46         Ok(Some(selection)) => selection,
47         Ok(None) => return Err(CodegenObligationError::Ambiguity),
48         Err(Unimplemented) => return Err(CodegenObligationError::Unimplemented),
49         Err(e) => {
50             bug!("Encountered error `{:?}` selecting `{:?}` during codegen", e, trait_ref)
51         }
52     };
53
54     debug!(?selection);
55
56     // Currently, we use a fulfillment context to completely resolve
57     // all nested obligations. This is because they can inform the
58     // inference of the impl's type parameters.
59     let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(tcx);
60     let impl_source = selection.map(|predicate| {
61         fulfill_cx.register_predicate_obligation(&infcx, predicate);
62     });
63
64     // In principle, we only need to do this so long as `impl_source`
65     // contains unbound type parameters. It could be a slight
66     // optimization to stop iterating early.
67     let errors = fulfill_cx.select_all_or_error(&infcx);
68     if !errors.is_empty() {
69         // `rustc_monomorphize::collector` assumes there are no type errors.
70         // Cycle errors are the only post-monomorphization errors possible; emit them now so
71         // `rustc_ty_utils::resolve_associated_item` doesn't return `None` post-monomorphization.
72         for err in errors {
73             if let FulfillmentErrorCode::CodeCycle(cycle) = err.code {
74                 infcx.err_ctxt().report_overflow_error_cycle(&cycle);
75             }
76         }
77         return Err(CodegenObligationError::FulfillmentError);
78     }
79
80     let impl_source = infcx.resolve_vars_if_possible(impl_source);
81     let impl_source = infcx.tcx.erase_regions(impl_source);
82
83     // Opaque types may have gotten their hidden types constrained, but we can ignore them safely
84     // as they will get constrained elsewhere, too.
85     // (ouz-a) This is required for `type-alias-impl-trait/assoc-projection-ice.rs` to pass
86     let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
87
88     Ok(&*tcx.arena.alloc(impl_source))
89 }