2018-05-08 16:10:16 +03:00
|
|
|
// This file contains various trait resolution methods used by codegen.
|
2017-04-21 21:02:14 -04:00
|
|
|
// They all assume regions can be erased and monomorphic types. It
|
|
|
|
// seems likely that they should eventually be merged into more
|
|
|
|
// general routines.
|
|
|
|
|
2020-01-06 20:13:24 +01:00
|
|
|
use crate::infer::{InferCtxt, TyCtxtInferExt};
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::traits::{
|
|
|
|
FulfillmentContext, Obligation, ObligationCause, SelectionContext, TraitEngine, Vtable,
|
|
|
|
};
|
2020-01-07 22:07:22 +01:00
|
|
|
use rustc::ty::fold::TypeFoldable;
|
|
|
|
use rustc::ty::{self, TyCtxt};
|
2017-04-21 21:02:14 -04:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Attempts to resolve an obligation to a vtable. The result is
|
|
|
|
/// a shallow vtable resolution, meaning that we do not
|
2017-09-28 23:13:43 -04:00
|
|
|
/// (necessarily) resolve all nested obligations on the impl. Note
|
|
|
|
/// that type check should guarantee to us that all nested
|
|
|
|
/// obligations *could be* resolved if we wanted to.
|
|
|
|
/// Assumes that this is run after the entire crate has been successfully type-checked.
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn codegen_fulfill_obligation<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
ty: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
(param_env, trait_ref): (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>),
|
|
|
|
) -> Vtable<'tcx, ()> {
|
2017-09-28 23:13:43 -04:00
|
|
|
// Remove any references to regions; this helps improve caching.
|
|
|
|
let trait_ref = ty.erase_regions(&trait_ref);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!(
|
|
|
|
"codegen_fulfill_obligation(trait_ref={:?}, def_id={:?})",
|
|
|
|
(param_env, trait_ref),
|
|
|
|
trait_ref.def_id()
|
|
|
|
);
|
2017-09-28 23:13:43 -04:00
|
|
|
|
|
|
|
// Do the initial selection for the obligation. This yields the
|
|
|
|
// shallow result we are looking for -- that is, what specific impl.
|
|
|
|
ty.infer_ctxt().enter(|infcx| {
|
|
|
|
let mut selcx = SelectionContext::new(&infcx);
|
|
|
|
|
|
|
|
let obligation_cause = ObligationCause::dummy();
|
2019-12-22 17:42:04 -05:00
|
|
|
let obligation =
|
|
|
|
Obligation::new(obligation_cause, param_env, trait_ref.to_poly_trait_predicate());
|
2017-09-28 23:13:43 -04:00
|
|
|
|
|
|
|
let selection = match selcx.select(&obligation) {
|
|
|
|
Ok(Some(selection)) => selection,
|
|
|
|
Ok(None) => {
|
|
|
|
// Ambiguity can happen when monomorphizing during trans
|
|
|
|
// expands to some humongo type that never occurred
|
|
|
|
// statically -- this humongo type can then overflow,
|
|
|
|
// leading to an ambiguous result. So report this as an
|
|
|
|
// overflow bug, since I believe this is the only case
|
|
|
|
// where ambiguity can result.
|
2019-12-22 17:42:04 -05:00
|
|
|
bug!(
|
|
|
|
"Encountered ambiguity selecting `{:?}` during codegen, \
|
2018-09-12 16:57:19 +02:00
|
|
|
presuming due to overflow",
|
2019-12-22 17:42:04 -05:00
|
|
|
trait_ref
|
|
|
|
)
|
2017-09-28 23:13:43 -04:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2018-09-12 16:57:19 +02:00
|
|
|
bug!("Encountered error `{:?}` selecting `{:?}` during codegen", e, trait_ref)
|
2017-09-28 23:13:43 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!("fulfill_obligation: selection={:?}", selection);
|
|
|
|
|
|
|
|
// Currently, we use a fulfillment context to completely resolve
|
|
|
|
// all nested obligations. This is because they can inform the
|
|
|
|
// inference of the impl's type parameters.
|
|
|
|
let mut fulfill_cx = FulfillmentContext::new();
|
|
|
|
let vtable = selection.map(|predicate| {
|
|
|
|
debug!("fulfill_obligation: register_predicate_obligation {:?}", predicate);
|
|
|
|
fulfill_cx.register_predicate_obligation(&infcx, predicate);
|
|
|
|
});
|
2019-04-19 14:53:34 -07:00
|
|
|
let vtable = infcx.drain_fulfillment_cx_or_panic(&mut fulfill_cx, &vtable);
|
2017-09-28 23:13:43 -04:00
|
|
|
|
|
|
|
info!("Cache miss: {:?} => {:?}", trait_ref, vtable);
|
|
|
|
vtable
|
|
|
|
})
|
|
|
|
}
|
2017-04-21 21:02:14 -04:00
|
|
|
|
2017-04-17 12:35:53 -04:00
|
|
|
// # Global Cache
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
2018-02-13 09:15:01 -05:00
|
|
|
/// Finishes processes any obligations that remain in the
|
|
|
|
/// fulfillment context, and then returns the result with all type
|
|
|
|
/// variables removed and regions erased. Because this is intended
|
|
|
|
/// for use after type-check has completed, if any errors occur,
|
|
|
|
/// it will panic. It is used during normalization and other cases
|
|
|
|
/// where processing the obligations in `fulfill_cx` may cause
|
|
|
|
/// type inference variables that appear in `result` to be
|
|
|
|
/// unified, and hence we need to process those obligations to get
|
|
|
|
/// the complete picture of the type.
|
2019-06-14 01:32:15 +03:00
|
|
|
fn drain_fulfillment_cx_or_panic<T>(
|
|
|
|
&self,
|
|
|
|
fulfill_cx: &mut FulfillmentContext<'tcx>,
|
|
|
|
result: &T,
|
2019-06-14 18:09:57 +02:00
|
|
|
) -> T
|
2019-06-14 01:32:15 +03:00
|
|
|
where
|
2019-06-14 18:09:57 +02:00
|
|
|
T: TypeFoldable<'tcx>,
|
2018-02-13 09:15:01 -05:00
|
|
|
{
|
|
|
|
debug!("drain_fulfillment_cx_or_panic()");
|
|
|
|
|
|
|
|
// In principle, we only need to do this so long as `result`
|
|
|
|
// contains unbound type parameters. It could be a slight
|
|
|
|
// optimization to stop iterating early.
|
2018-09-12 16:57:19 +02:00
|
|
|
if let Err(errors) = fulfill_cx.select_all_or_error(self) {
|
2019-04-19 14:53:34 -07:00
|
|
|
bug!("Encountered errors `{:?}` resolving bounds after type-checking", errors);
|
2018-02-13 09:15:01 -05:00
|
|
|
}
|
|
|
|
|
2019-05-11 19:08:26 +01:00
|
|
|
let result = self.resolve_vars_if_possible(result);
|
2019-06-14 18:09:57 +02:00
|
|
|
self.tcx.erase_regions(&result)
|
2018-02-13 09:15:01 -05:00
|
|
|
}
|
|
|
|
}
|