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 #[instrument(level = "debug", skip(tcx))]
22 pub fn codegen_fulfill_obligation<'tcx>(
24 (param_env, trait_ref): (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>),
25 ) -> Result<&'tcx ImplSource<'tcx, ()>, CodegenObligationError> {
26 // We expect the input to be fully normalized.
27 debug_assert_eq!(trait_ref, tcx.normalize_erasing_regions(param_env, trait_ref));
29 // Do the initial selection for the obligation. This yields the
30 // shallow result we are looking for -- that is, what specific impl.
31 let mut infcx_builder =
32 tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(DefiningAnchor::Bubble);
33 infcx_builder.enter(|infcx| {
34 //~^ HACK `Bubble` is required for
35 // this test to pass: type-alias-impl-trait/assoc-projection-ice.rs
36 let mut selcx = SelectionContext::new(&infcx);
38 let obligation_cause = ObligationCause::dummy();
40 Obligation::new(obligation_cause, param_env, trait_ref.to_poly_trait_predicate());
42 let selection = match selcx.select(&obligation) {
43 Ok(Some(selection)) => selection,
44 Ok(None) => return Err(CodegenObligationError::Ambiguity),
45 Err(Unimplemented) => return Err(CodegenObligationError::Unimplemented),
47 bug!("Encountered error `{:?}` selecting `{:?}` during codegen", e, trait_ref)
53 // Currently, we use a fulfillment context to completely resolve
54 // all nested obligations. This is because they can inform the
55 // inference of the impl's type parameters.
56 let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(tcx);
57 let impl_source = selection.map(|predicate| {
58 fulfill_cx.register_predicate_obligation(&infcx, predicate);
61 // In principle, we only need to do this so long as `impl_source`
62 // contains unbound type parameters. It could be a slight
63 // optimization to stop iterating early.
64 let errors = fulfill_cx.select_all_or_error(&infcx);
65 if !errors.is_empty() {
66 return Err(CodegenObligationError::FulfillmentError);
69 let impl_source = infcx.resolve_vars_if_possible(impl_source);
70 let impl_source = infcx.tcx.erase_regions(impl_source);
72 // Opaque types may have gotten their hidden types constrained, but we can ignore them safely
73 // as they will get constrained elsewhere, too.
74 // (ouz-a) This is required for `type-alias-impl-trait/assoc-projection-ice.rs` to pass
75 let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
77 debug!("Cache miss: {trait_ref:?} => {impl_source:?}");
78 Ok(&*tcx.arena.alloc(impl_source))