1 use std::cell::RefCell;
3 use super::TraitEngine;
4 use super::{ChalkFulfillmentContext, FulfillmentContext};
5 use crate::infer::InferCtxtExt;
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_hir::def_id::{DefId, LocalDefId};
8 use rustc_infer::infer::{InferCtxt, InferOk};
9 use rustc_infer::traits::{
10 FulfillmentError, Obligation, ObligationCause, PredicateObligation, TraitEngineExt as _,
12 use rustc_middle::ty::error::TypeError;
13 use rustc_middle::ty::ToPredicate;
14 use rustc_middle::ty::TypeFoldable;
15 use rustc_middle::ty::{self, Ty, TyCtxt};
18 pub trait TraitEngineExt<'tcx> {
19 fn new(tcx: TyCtxt<'tcx>) -> Box<Self>;
20 fn new_in_snapshot(tcx: TyCtxt<'tcx>) -> Box<Self>;
23 impl<'tcx> TraitEngineExt<'tcx> for dyn TraitEngine<'tcx> {
24 fn new(tcx: TyCtxt<'tcx>) -> Box<Self> {
25 if tcx.sess.opts.unstable_opts.chalk {
26 Box::new(ChalkFulfillmentContext::new())
28 Box::new(FulfillmentContext::new())
32 fn new_in_snapshot(tcx: TyCtxt<'tcx>) -> Box<Self> {
33 if tcx.sess.opts.unstable_opts.chalk {
34 Box::new(ChalkFulfillmentContext::new())
36 Box::new(FulfillmentContext::new_in_snapshot())
41 /// Used if you want to have pleasant experience when dealing
42 /// with obligations outside of hir or mir typeck.
43 pub struct ObligationCtxt<'a, 'tcx> {
44 pub infcx: &'a InferCtxt<'tcx>,
45 engine: RefCell<Box<dyn TraitEngine<'tcx>>>,
48 impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
49 pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
50 Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new(infcx.tcx)) }
53 pub fn new_in_snapshot(infcx: &'a InferCtxt<'tcx>) -> Self {
54 Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new_in_snapshot(infcx.tcx)) }
57 pub fn register_obligation(&self, obligation: PredicateObligation<'tcx>) {
58 self.engine.borrow_mut().register_predicate_obligation(self.infcx, obligation);
61 pub fn register_obligations(
63 obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
65 // Can't use `register_predicate_obligations` because the iterator
66 // may also use this `ObligationCtxt`.
67 for obligation in obligations {
68 self.engine.borrow_mut().register_predicate_obligation(self.infcx, obligation)
72 pub fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
73 let InferOk { value, obligations } = infer_ok;
74 self.engine.borrow_mut().register_predicate_obligations(self.infcx, obligations);
78 /// Requires that `ty` must implement the trait with `def_id` in
79 /// the given environment. This trait must not have any type
80 /// parameters (except for `Self`).
81 pub fn register_bound(
83 cause: ObligationCause<'tcx>,
84 param_env: ty::ParamEnv<'tcx>,
88 let tcx = self.infcx.tcx;
89 let trait_ref = ty::TraitRef { def_id, substs: tcx.mk_substs_trait(ty, &[]) };
90 self.register_obligation(Obligation {
94 predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(tcx),
98 pub fn normalize<T: TypeFoldable<'tcx>>(
100 cause: ObligationCause<'tcx>,
101 param_env: ty::ParamEnv<'tcx>,
104 let infer_ok = self.infcx.partially_normalize_associated_types_in(cause, param_env, value);
105 self.register_infer_ok_obligations(infer_ok)
110 cause: &ObligationCause<'tcx>,
111 param_env: ty::ParamEnv<'tcx>,
114 ) -> Result<(), TypeError<'tcx>> {
115 match self.infcx.at(cause, param_env).eq(expected, actual) {
116 Ok(InferOk { obligations, value: () }) => {
117 self.register_obligations(obligations);
124 pub fn select_all_or_error(&self) -> Vec<FulfillmentError<'tcx>> {
125 self.engine.borrow_mut().select_all_or_error(self.infcx)
128 pub fn assumed_wf_types(
130 param_env: ty::ParamEnv<'tcx>,
133 ) -> FxHashSet<Ty<'tcx>> {
134 let tcx = self.infcx.tcx;
135 let assumed_wf_types = tcx.assumed_wf_types(def_id);
136 let mut implied_bounds = FxHashSet::default();
137 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
138 let cause = ObligationCause::misc(span, hir_id);
139 for ty in assumed_wf_types {
140 // FIXME(@lcnr): rustc currently does not check wf for types
141 // pre-normalization, meaning that implied bounds are sometimes
142 // incorrect. See #100910 for more details.
144 // Not adding the unnormalized types here mostly fixes that, except
145 // that there are projections which are still ambiguous in the item definition
146 // but do normalize successfully when using the item, see #98543.
148 // Anyways, I will hopefully soon change implied bounds to make all of this
149 // sound and then uncomment this line again.
151 // implied_bounds.insert(ty);
152 let normalized = self.normalize(cause.clone(), param_env, ty);
153 implied_bounds.insert(normalized);