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.
|
|
|
|
|
2022-12-08 05:16:39 +00:00
|
|
|
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
|
2022-09-19 20:45:00 -05:00
|
|
|
use rustc_infer::traits::FulfillmentErrorCode;
|
2022-05-01 11:03:14 +02:00
|
|
|
use rustc_middle::traits::CodegenObligationError;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2022-12-08 05:16:39 +00:00
|
|
|
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
|
|
|
|
use rustc_trait_selection::traits::{
|
|
|
|
ImplSource, Obligation, ObligationCause, SelectionContext, TraitEngine, TraitEngineExt,
|
|
|
|
Unimplemented,
|
|
|
|
};
|
2017-04-21 21:02:14 -04:00
|
|
|
|
2021-08-22 14:46:15 +02:00
|
|
|
/// Attempts to resolve an obligation to an `ImplSource`. The result is
|
2020-05-11 15:25:33 +00:00
|
|
|
/// a shallow `ImplSource` 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.
|
2020-10-04 16:40:06 +02:00
|
|
|
///
|
|
|
|
/// This also expects that `trait_ref` is fully normalized.
|
2022-09-09 13:36:27 +02:00
|
|
|
pub fn codegen_select_candidate<'tcx>(
|
2020-09-28 20:56:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
(param_env, trait_ref): (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>),
|
2022-05-01 11:03:14 +02:00
|
|
|
) -> Result<&'tcx ImplSource<'tcx, ()>, CodegenObligationError> {
|
2020-10-04 16:40:06 +02:00
|
|
|
// We expect the input to be fully normalized.
|
|
|
|
debug_assert_eq!(trait_ref, tcx.normalize_erasing_regions(param_env, trait_ref));
|
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.
|
2022-07-20 11:40:15 +02:00
|
|
|
let infcx = tcx
|
|
|
|
.infer_ctxt()
|
|
|
|
.ignoring_regions()
|
|
|
|
.with_opaque_type_inference(DefiningAnchor::Bubble)
|
|
|
|
.build();
|
2022-07-02 16:37:49 +03:00
|
|
|
//~^ HACK `Bubble` is required for
|
|
|
|
// this test to pass: type-alias-impl-trait/assoc-projection-ice.rs
|
2017-09-28 23:13:43 -04:00
|
|
|
let mut selcx = SelectionContext::new(&infcx);
|
|
|
|
|
|
|
|
let obligation_cause = ObligationCause::dummy();
|
2022-11-18 14:10:36 +00:00
|
|
|
let obligation = Obligation::new(tcx, obligation_cause, param_env, trait_ref);
|
2017-09-28 23:13:43 -04:00
|
|
|
|
|
|
|
let selection = match selcx.select(&obligation) {
|
|
|
|
Ok(Some(selection)) => selection,
|
2022-05-01 11:03:14 +02:00
|
|
|
Ok(None) => return Err(CodegenObligationError::Ambiguity),
|
|
|
|
Err(Unimplemented) => return Err(CodegenObligationError::Unimplemented),
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-30 13:17:46 +00:00
|
|
|
debug!(?selection);
|
2017-09-28 23:13:43 -04:00
|
|
|
|
|
|
|
// 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.
|
2022-07-26 04:43:04 +00:00
|
|
|
let mut fulfill_cx = <dyn TraitEngine<'tcx>>::new(tcx);
|
2020-05-11 15:25:33 +00:00
|
|
|
let impl_source = selection.map(|predicate| {
|
2017-09-28 23:13:43 -04:00
|
|
|
fulfill_cx.register_predicate_obligation(&infcx, predicate);
|
|
|
|
});
|
2022-05-01 11:03:14 +02:00
|
|
|
|
|
|
|
// In principle, we only need to do this so long as `impl_source`
|
|
|
|
// contains unbound type parameters. It could be a slight
|
|
|
|
// optimization to stop iterating early.
|
|
|
|
let errors = fulfill_cx.select_all_or_error(&infcx);
|
|
|
|
if !errors.is_empty() {
|
2022-09-19 20:45:00 -05:00
|
|
|
// `rustc_monomorphize::collector` assumes there are no type errors.
|
|
|
|
// Cycle errors are the only post-monomorphization errors possible; emit them now so
|
|
|
|
// `rustc_ty_utils::resolve_associated_item` doesn't return `None` post-monomorphization.
|
|
|
|
for err in errors {
|
|
|
|
if let FulfillmentErrorCode::CodeCycle(cycle) = err.code {
|
2022-12-06 00:19:42 +00:00
|
|
|
infcx.err_ctxt().report_overflow_obligation_cycle(&cycle);
|
2022-09-19 20:45:00 -05:00
|
|
|
}
|
2022-05-01 11:03:14 +02:00
|
|
|
}
|
|
|
|
return Err(CodegenObligationError::FulfillmentError);
|
2022-09-19 22:03:59 -05:00
|
|
|
}
|
2022-05-01 11:03:14 +02:00
|
|
|
|
|
|
|
let impl_source = infcx.resolve_vars_if_possible(impl_source);
|
|
|
|
let impl_source = infcx.tcx.erase_regions(impl_source);
|
2017-09-28 23:13:43 -04:00
|
|
|
|
2022-03-30 13:17:46 +00:00
|
|
|
// Opaque types may have gotten their hidden types constrained, but we can ignore them safely
|
|
|
|
// as they will get constrained elsewhere, too.
|
2022-07-02 16:37:49 +03:00
|
|
|
// (ouz-a) This is required for `type-alias-impl-trait/assoc-projection-ice.rs` to pass
|
2023-01-09 18:14:28 +00:00
|
|
|
let _ = infcx.take_opaque_types();
|
2022-02-14 16:10:22 +00:00
|
|
|
|
2022-01-31 19:55:34 +01:00
|
|
|
Ok(&*tcx.arena.alloc(impl_source))
|
2017-09-28 23:13:43 -04:00
|
|
|
}
|