• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 rustc_infer::infer::TyCtxtInferExt;
7 use rustc_infer::traits::{FulfillmentErrorCode, TraitEngineExt as _};
8 use rustc_middle::traits::CodegenObligationError;
9 use rustc_middle::ty::{self, TyCtxt};
10 use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
11 use rustc_trait_selection::traits::{
12     ImplSource, Obligation, ObligationCause, SelectionContext, TraitEngine, TraitEngineExt,
13     Unimplemented,
14 };
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.
codegen_select_candidate<'tcx>( tcx: TyCtxt<'tcx>, (param_env, trait_ref): (ty::ParamEnv<'tcx>, ty::TraitRef<'tcx>), ) -> Result<&'tcx ImplSource<'tcx, ()>, CodegenObligationError>23 pub fn codegen_select_candidate<'tcx>(
24     tcx: TyCtxt<'tcx>,
25     (param_env, trait_ref): (ty::ParamEnv<'tcx>, ty::TraitRef<'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.infer_ctxt().ignoring_regions().build();
33     let mut selcx = SelectionContext::new(&infcx);
34 
35     let obligation_cause = ObligationCause::dummy();
36     let obligation = Obligation::new(tcx, obligation_cause, param_env, trait_ref);
37 
38     let selection = match selcx.select(&obligation) {
39         Ok(Some(selection)) => selection,
40         Ok(None) => return Err(CodegenObligationError::Ambiguity),
41         Err(Unimplemented) => return Err(CodegenObligationError::Unimplemented),
42         Err(e) => {
43             bug!("Encountered error `{:?}` selecting `{:?}` during codegen", e, trait_ref)
44         }
45     };
46 
47     debug!(?selection);
48 
49     // Currently, we use a fulfillment context to completely resolve
50     // all nested obligations. This is because they can inform the
51     // inference of the impl's type parameters.
52     let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(&infcx);
53     let impl_source = selection.map(|predicate| {
54         fulfill_cx.register_predicate_obligation(&infcx, predicate);
55     });
56 
57     // In principle, we only need to do this so long as `impl_source`
58     // contains unbound type parameters. It could be a slight
59     // optimization to stop iterating early.
60     let errors = fulfill_cx.select_all_or_error(&infcx);
61     if !errors.is_empty() {
62         // `rustc_monomorphize::collector` assumes there are no type errors.
63         // Cycle errors are the only post-monomorphization errors possible; emit them now so
64         // `rustc_ty_utils::resolve_associated_item` doesn't return `None` post-monomorphization.
65         for err in errors {
66             if let FulfillmentErrorCode::CodeCycle(cycle) = err.code {
67                 infcx.err_ctxt().report_overflow_obligation_cycle(&cycle);
68             }
69         }
70         return Err(CodegenObligationError::FulfillmentError);
71     }
72 
73     let impl_source = infcx.resolve_vars_if_possible(impl_source);
74     let impl_source = infcx.tcx.erase_regions(impl_source);
75 
76     Ok(&*tcx.arena.alloc(impl_source))
77 }
78