]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/codegen.rs
Rollup merge of #98665 - ChrisDenton:deprecated-suggestion, r=compiler-errors
[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::TyCtxtInferExt;
7 use crate::traits::{
8     FulfillmentContext, ImplSource, Obligation, ObligationCause, SelectionContext, TraitEngine,
9     Unimplemented,
10 };
11 use rustc_middle::traits::CodegenObligationError;
12 use rustc_middle::ty::{self, TyCtxt};
13
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.
19 ///
20 /// This also expects that `trait_ref` is fully normalized.
21 #[instrument(level = "debug", skip(tcx))]
22 pub fn codegen_fulfill_obligation<'tcx>(
23     tcx: TyCtxt<'tcx>,
24     (param_env, trait_ref): (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>),
25 ) -> Result<&'tcx ImplSource<'tcx, ()>, CodegenObligationError> {
26     // Remove any references to regions; this helps improve caching.
27     let trait_ref = tcx.erase_regions(trait_ref);
28     // We expect the input to be fully normalized.
29     debug_assert_eq!(trait_ref, tcx.normalize_erasing_regions(param_env, trait_ref));
30
31     // Do the initial selection for the obligation. This yields the
32     // shallow result we are looking for -- that is, what specific impl.
33     tcx.infer_ctxt().enter(|infcx| {
34         let mut selcx = SelectionContext::new(&infcx);
35
36         let obligation_cause = ObligationCause::dummy();
37         let obligation =
38             Obligation::new(obligation_cause, param_env, trait_ref.to_poly_trait_predicate());
39
40         let selection = match selcx.select(&obligation) {
41             Ok(Some(selection)) => selection,
42             Ok(None) => return Err(CodegenObligationError::Ambiguity),
43             Err(Unimplemented) => return Err(CodegenObligationError::Unimplemented),
44             Err(e) => {
45                 bug!("Encountered error `{:?}` selecting `{:?}` during codegen", e, trait_ref)
46             }
47         };
48
49         debug!(?selection);
50
51         // Currently, we use a fulfillment context to completely resolve
52         // all nested obligations. This is because they can inform the
53         // inference of the impl's type parameters.
54         let mut fulfill_cx = FulfillmentContext::new();
55         let impl_source = selection.map(|predicate| {
56             fulfill_cx.register_predicate_obligation(&infcx, predicate);
57         });
58
59         // In principle, we only need to do this so long as `impl_source`
60         // contains unbound type parameters. It could be a slight
61         // optimization to stop iterating early.
62         let errors = fulfill_cx.select_all_or_error(&infcx);
63         if !errors.is_empty() {
64             return Err(CodegenObligationError::FulfillmentError);
65         }
66
67         let impl_source = infcx.resolve_vars_if_possible(impl_source);
68         let impl_source = infcx.tcx.erase_regions(impl_source);
69
70         // Opaque types may have gotten their hidden types constrained, but we can ignore them safely
71         // as they will get constrained elsewhere, too.
72         let _opaque_types = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
73
74         debug!("Cache miss: {trait_ref:?} => {impl_source:?}");
75         Ok(&*tcx.arena.alloc(impl_source))
76     })
77 }