2022-07-07 13:12:32 +00:00
|
|
|
use std::cell::RefCell;
|
2018-03-11 02:29:22 +00:00
|
|
|
|
2020-02-22 10:44:18 +00:00
|
|
|
use super::TraitEngine;
|
2020-03-03 16:25:03 +00:00
|
|
|
use super::{ChalkFulfillmentContext, FulfillmentContext};
|
2022-07-07 13:12:32 +00:00
|
|
|
use crate::infer::InferCtxtExt;
|
2022-08-17 16:14:25 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2022-07-07 13:12:32 +00:00
|
|
|
use rustc_infer::infer::{InferCtxt, InferOk};
|
|
|
|
use rustc_infer::traits::{
|
|
|
|
FulfillmentError, Obligation, ObligationCause, PredicateObligation, TraitEngineExt as _,
|
|
|
|
};
|
|
|
|
use rustc_middle::ty::error::TypeError;
|
|
|
|
use rustc_middle::ty::ToPredicate;
|
|
|
|
use rustc_middle::ty::TypeFoldable;
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2022-08-17 16:14:25 +00:00
|
|
|
use rustc_span::Span;
|
2018-03-11 02:29:22 +00:00
|
|
|
|
2018-06-27 20:04:32 +00:00
|
|
|
pub trait TraitEngineExt<'tcx> {
|
2020-02-22 10:44:18 +00:00
|
|
|
fn new(tcx: TyCtxt<'tcx>) -> Box<Self>;
|
2018-03-11 02:29:22 +00:00
|
|
|
}
|
2018-06-27 20:04:32 +00:00
|
|
|
|
2020-02-22 10:44:18 +00:00
|
|
|
impl<'tcx> TraitEngineExt<'tcx> for dyn TraitEngine<'tcx> {
|
2020-03-03 16:25:03 +00:00
|
|
|
fn new(tcx: TyCtxt<'tcx>) -> Box<Self> {
|
2022-07-06 12:44:47 +00:00
|
|
|
if tcx.sess.opts.unstable_opts.chalk {
|
2020-03-03 16:25:03 +00:00
|
|
|
Box::new(ChalkFulfillmentContext::new())
|
|
|
|
} else {
|
|
|
|
Box::new(FulfillmentContext::new())
|
|
|
|
}
|
2018-06-27 20:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-07 13:12:32 +00:00
|
|
|
|
|
|
|
/// Used if you want to have pleasant experience when dealing
|
|
|
|
/// with obligations outside of hir or mir typeck.
|
|
|
|
pub struct ObligationCtxt<'a, 'tcx> {
|
|
|
|
pub infcx: &'a InferCtxt<'a, 'tcx>,
|
|
|
|
engine: RefCell<Box<dyn TraitEngine<'tcx>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
|
|
|
|
pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
|
|
|
|
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new(infcx.tcx)) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_obligation(&self, obligation: PredicateObligation<'tcx>) {
|
|
|
|
self.engine.borrow_mut().register_predicate_obligation(self.infcx, obligation);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_obligations(
|
|
|
|
&self,
|
|
|
|
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
|
|
|
|
) {
|
|
|
|
// Can't use `register_predicate_obligations` because the iterator
|
|
|
|
// may also use this `ObligationCtxt`.
|
|
|
|
for obligation in obligations {
|
|
|
|
self.engine.borrow_mut().register_predicate_obligation(self.infcx, obligation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
|
|
|
|
let InferOk { value, obligations } = infer_ok;
|
|
|
|
self.engine.borrow_mut().register_predicate_obligations(self.infcx, obligations);
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Requires that `ty` must implement the trait with `def_id` in
|
|
|
|
/// the given environment. This trait must not have any type
|
|
|
|
/// parameters (except for `Self`).
|
|
|
|
pub fn register_bound(
|
|
|
|
&self,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
) {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
let trait_ref = ty::TraitRef { def_id, substs: tcx.mk_substs_trait(ty, &[]) };
|
|
|
|
self.register_obligation(Obligation {
|
|
|
|
cause,
|
|
|
|
recursion_depth: 0,
|
|
|
|
param_env,
|
|
|
|
predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(tcx),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn normalize<T: TypeFoldable<'tcx>>(
|
|
|
|
&self,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
value: T,
|
|
|
|
) -> T {
|
|
|
|
let infer_ok = self.infcx.partially_normalize_associated_types_in(cause, param_env, value);
|
|
|
|
self.register_infer_ok_obligations(infer_ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn equate_types(
|
|
|
|
&self,
|
|
|
|
cause: &ObligationCause<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
expected: Ty<'tcx>,
|
|
|
|
actual: Ty<'tcx>,
|
|
|
|
) -> Result<(), TypeError<'tcx>> {
|
|
|
|
match self.infcx.at(cause, param_env).eq(expected, actual) {
|
|
|
|
Ok(InferOk { obligations, value: () }) => {
|
|
|
|
self.register_obligations(obligations);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn select_all_or_error(&self) -> Vec<FulfillmentError<'tcx>> {
|
|
|
|
self.engine.borrow_mut().select_all_or_error(self.infcx)
|
|
|
|
}
|
2022-08-17 16:14:25 +00:00
|
|
|
|
|
|
|
pub fn assumed_wf_types(
|
|
|
|
&self,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
def_id: LocalDefId,
|
|
|
|
) -> FxHashSet<Ty<'tcx>> {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
let assumed_wf_types = tcx.assumed_wf_types(def_id);
|
|
|
|
let mut implied_bounds = FxHashSet::default();
|
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
|
|
|
let cause = ObligationCause::misc(span, hir_id);
|
|
|
|
for ty in assumed_wf_types {
|
|
|
|
implied_bounds.insert(ty);
|
|
|
|
let normalized = self.normalize(cause.clone(), param_env, ty);
|
|
|
|
implied_bounds.insert(normalized);
|
|
|
|
}
|
|
|
|
implied_bounds
|
|
|
|
}
|
2022-07-07 13:12:32 +00:00
|
|
|
}
|