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
6 use crate::infer::{DefiningAnchor, TyCtxtInferExt};
8 ImplSource, Obligation, ObligationCause, SelectionContext, TraitEngine, TraitEngineExt,
11 use rustc_middle::traits::CodegenObligationError;
12 use rustc_middle::ty::{self, TyCtxt};
14 /// Attempts to resolve an obligation to an `ImplSource`. The result is
15 /// a shallow `ImplSource` resolution, meaning that we do not
16 /// (necessarily) resolve all nested obligations on the impl. Note
17 /// that type check should guarantee to us that all nested
18 /// obligations *could be* resolved if we wanted to.
20 /// This also expects that `trait_ref` is fully normalized.
21 pub fn codegen_select_candidate<'tcx>(
23 (param_env, trait_ref): (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>),
24 ) -> Result<&'tcx ImplSource<'tcx, ()>, CodegenObligationError> {
25 // We expect the input to be fully normalized.
26 debug_assert_eq!(trait_ref, tcx.normalize_erasing_regions(param_env, trait_ref));
28 // Do the initial selection for the obligation. This yields the
29 // shallow result we are looking for -- that is, what specific impl.
30 let mut infcx_builder =
31 tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(DefiningAnchor::Bubble);
32 infcx_builder.enter(|infcx| {
33 //~^ HACK `Bubble` is required for
34 // this test to pass: type-alias-impl-trait/assoc-projection-ice.rs
35 let mut selcx = SelectionContext::new(&infcx);
37 let obligation_cause = ObligationCause::dummy();
39 Obligation::new(obligation_cause, param_env, trait_ref.to_poly_trait_predicate());
41 let selection = match selcx.select(&obligation) {
42 Ok(Some(selection)) => selection,
43 Ok(None) => return Err(CodegenObligationError::Ambiguity),
44 Err(Unimplemented) => return Err(CodegenObligationError::Unimplemented),
46 bug!("Encountered error `{:?}` selecting `{:?}` during codegen", e, trait_ref)
52 // Currently, we use a fulfillment context to completely resolve
53 // all nested obligations. This is because they can inform the
54 // inference of the impl's type parameters.
55 let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(tcx);
56 let impl_source = selection.map(|predicate| {
57 fulfill_cx.register_predicate_obligation(&infcx, predicate);
60 // In principle, we only need to do this so long as `impl_source`
61 // contains unbound type parameters. It could be a slight
62 // optimization to stop iterating early.
63 let errors = fulfill_cx.select_all_or_error(&infcx);
64 if !errors.is_empty() {
65 return Err(CodegenObligationError::FulfillmentError);
68 let impl_source = infcx.resolve_vars_if_possible(impl_source);
69 let impl_source = infcx.tcx.erase_regions(impl_source);
71 // Opaque types may have gotten their hidden types constrained, but we can ignore them safely
72 // as they will get constrained elsewhere, too.
73 // (ouz-a) This is required for `type-alias-impl-trait/assoc-projection-ice.rs` to pass
74 let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
76 Ok(&*tcx.arena.alloc(impl_source))