rustc_trait_selection/solve/
delegate.rs

1use std::ops::Deref;
2
3use rustc_data_structures::fx::FxHashSet;
4use rustc_hir::LangItem;
5use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
6use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
7use rustc_infer::infer::canonical::{
8    Canonical, CanonicalExt as _, CanonicalQueryInput, CanonicalVarKind, CanonicalVarValues,
9};
10use rustc_infer::infer::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TyCtxtInferExt};
11use rustc_infer::traits::solve::Goal;
12use rustc_middle::traits::query::NoSolution;
13use rustc_middle::traits::solve::Certainty;
14use rustc_middle::ty::{
15    self, Ty, TyCtxt, TypeFlags, TypeFoldable, TypeVisitableExt as _, TypingMode,
16};
17use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
18
19use crate::traits::{EvaluateConstErr, ObligationCause, sizedness_fast_path, specialization_graph};
20
21#[repr(transparent)]
22pub struct SolverDelegate<'tcx>(InferCtxt<'tcx>);
23
24impl<'a, 'tcx> From<&'a InferCtxt<'tcx>> for &'a SolverDelegate<'tcx> {
25    fn from(infcx: &'a InferCtxt<'tcx>) -> Self {
26        // SAFETY: `repr(transparent)`
27        unsafe { std::mem::transmute(infcx) }
28    }
29}
30
31impl<'tcx> Deref for SolverDelegate<'tcx> {
32    type Target = InferCtxt<'tcx>;
33
34    fn deref(&self) -> &Self::Target {
35        &self.0
36    }
37}
38
39impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<'tcx> {
40    type Infcx = InferCtxt<'tcx>;
41    type Interner = TyCtxt<'tcx>;
42
43    fn cx(&self) -> TyCtxt<'tcx> {
44        self.0.tcx
45    }
46
47    fn build_with_canonical<V>(
48        interner: TyCtxt<'tcx>,
49        canonical: &CanonicalQueryInput<'tcx, V>,
50    ) -> (Self, V, CanonicalVarValues<'tcx>)
51    where
52        V: TypeFoldable<TyCtxt<'tcx>>,
53    {
54        let (infcx, value, vars) = interner
55            .infer_ctxt()
56            .with_next_trait_solver(true)
57            .build_with_canonical(DUMMY_SP, canonical);
58        (SolverDelegate(infcx), value, vars)
59    }
60
61    fn compute_goal_fast_path(
62        &self,
63        goal: Goal<'tcx, ty::Predicate<'tcx>>,
64        span: Span,
65    ) -> Option<Certainty> {
66        if let Some(trait_pred) = goal.predicate.as_trait_clause() {
67            if self.shallow_resolve(trait_pred.self_ty().skip_binder()).is_ty_var()
68                // We don't do this fast path when opaques are defined since we may
69                // eventually use opaques to incompletely guide inference via ty var
70                // self types.
71                // FIXME: Properly consider opaques here.
72                && self.inner.borrow_mut().opaque_types().is_empty()
73            {
74                return Some(Certainty::AMBIGUOUS);
75            }
76
77            if trait_pred.polarity() == ty::PredicatePolarity::Positive {
78                match self.0.tcx.as_lang_item(trait_pred.def_id()) {
79                    Some(LangItem::Sized) | Some(LangItem::MetaSized) => {
80                        let predicate = self.resolve_vars_if_possible(goal.predicate);
81                        if sizedness_fast_path(self.tcx, predicate, goal.param_env) {
82                            return Some(Certainty::Yes);
83                        }
84                    }
85                    Some(LangItem::Copy | LangItem::Clone) => {
86                        let self_ty =
87                            self.resolve_vars_if_possible(trait_pred.self_ty().skip_binder());
88                        // Unlike `Sized` traits, which always prefer the built-in impl,
89                        // `Copy`/`Clone` may be shadowed by a param-env candidate which
90                        // could force a lifetime error or guide inference. While that's
91                        // not generally desirable, it is observable, so for now let's
92                        // ignore this fast path for types that have regions or infer.
93                        if !self_ty
94                            .has_type_flags(TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_INFER)
95                            && self_ty.is_trivially_pure_clone_copy()
96                        {
97                            return Some(Certainty::Yes);
98                        }
99                    }
100                    _ => {}
101                }
102            }
103        }
104
105        let pred = goal.predicate.kind();
106        match pred.no_bound_vars()? {
107            ty::PredicateKind::DynCompatible(def_id) if self.0.tcx.is_dyn_compatible(def_id) => {
108                Some(Certainty::Yes)
109            }
110            ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(outlives)) => {
111                self.0.sub_regions(
112                    SubregionOrigin::RelateRegionParamBound(span, None),
113                    outlives.1,
114                    outlives.0,
115                );
116                Some(Certainty::Yes)
117            }
118            ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(outlives)) => {
119                self.0.register_type_outlives_constraint(
120                    outlives.0,
121                    outlives.1,
122                    &ObligationCause::dummy_with_span(span),
123                );
124
125                Some(Certainty::Yes)
126            }
127            ty::PredicateKind::Subtype(ty::SubtypePredicate { a, b, .. })
128            | ty::PredicateKind::Coerce(ty::CoercePredicate { a, b }) => {
129                match (self.shallow_resolve(a).kind(), self.shallow_resolve(b).kind()) {
130                    (&ty::Infer(ty::TyVar(a_vid)), &ty::Infer(ty::TyVar(b_vid))) => {
131                        self.sub_unify_ty_vids_raw(a_vid, b_vid);
132                        Some(Certainty::AMBIGUOUS)
133                    }
134                    _ => None,
135                }
136            }
137            ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, _)) => {
138                if self.shallow_resolve_const(ct).is_ct_infer() {
139                    Some(Certainty::AMBIGUOUS)
140                } else {
141                    None
142                }
143            }
144            ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
145                let arg = self.shallow_resolve_term(arg);
146                if arg.is_trivially_wf(self.tcx) {
147                    Some(Certainty::Yes)
148                } else if arg.is_infer() {
149                    Some(Certainty::AMBIGUOUS)
150                } else {
151                    None
152                }
153            }
154            _ => None,
155        }
156    }
157
158    fn fresh_var_for_kind_with_span(
159        &self,
160        arg: ty::GenericArg<'tcx>,
161        span: Span,
162    ) -> ty::GenericArg<'tcx> {
163        match arg.kind() {
164            ty::GenericArgKind::Lifetime(_) => {
165                self.next_region_var(RegionVariableOrigin::Misc(span)).into()
166            }
167            ty::GenericArgKind::Type(_) => self.next_ty_var(span).into(),
168            ty::GenericArgKind::Const(_) => self.next_const_var(span).into(),
169        }
170    }
171
172    fn leak_check(&self, max_input_universe: ty::UniverseIndex) -> Result<(), NoSolution> {
173        self.0.leak_check(max_input_universe, None).map_err(|_| NoSolution)
174    }
175
176    fn evaluate_const(
177        &self,
178        param_env: ty::ParamEnv<'tcx>,
179        uv: ty::UnevaluatedConst<'tcx>,
180    ) -> Option<ty::Const<'tcx>> {
181        let ct = ty::Const::new_unevaluated(self.tcx, uv);
182
183        match crate::traits::try_evaluate_const(&self.0, ct, param_env) {
184            Ok(ct) => Some(ct),
185            Err(EvaluateConstErr::EvaluationFailure(e)) => Some(ty::Const::new_error(self.tcx, e)),
186            Err(
187                EvaluateConstErr::InvalidConstParamTy(_) | EvaluateConstErr::HasGenericsOrInfers,
188            ) => None,
189        }
190    }
191
192    fn well_formed_goals(
193        &self,
194        param_env: ty::ParamEnv<'tcx>,
195        term: ty::Term<'tcx>,
196    ) -> Option<Vec<Goal<'tcx, ty::Predicate<'tcx>>>> {
197        crate::traits::wf::unnormalized_obligations(
198            &self.0,
199            param_env,
200            term,
201            DUMMY_SP,
202            CRATE_DEF_ID,
203        )
204        .map(|obligations| obligations.into_iter().map(|obligation| obligation.as_goal()).collect())
205    }
206
207    fn make_deduplicated_outlives_constraints(&self) -> Vec<ty::ArgOutlivesPredicate<'tcx>> {
208        // Cannot use `take_registered_region_obligations` as we may compute the response
209        // inside of a `probe` whenever we have multiple choices inside of the solver.
210        let region_obligations = self.0.inner.borrow().region_obligations().to_owned();
211        let region_assumptions = self.0.inner.borrow().region_assumptions().to_owned();
212        let region_constraints = self.0.with_region_constraints(|region_constraints| {
213            make_query_region_constraints(
214                region_obligations,
215                region_constraints,
216                region_assumptions,
217            )
218        });
219
220        let mut seen = FxHashSet::default();
221        region_constraints
222            .outlives
223            .into_iter()
224            .filter(|&(outlives, _)| seen.insert(outlives))
225            .map(|(outlives, _)| outlives)
226            .collect()
227    }
228
229    fn instantiate_canonical<V>(
230        &self,
231        canonical: Canonical<'tcx, V>,
232        values: CanonicalVarValues<'tcx>,
233    ) -> V
234    where
235        V: TypeFoldable<TyCtxt<'tcx>>,
236    {
237        canonical.instantiate(self.tcx, &values)
238    }
239
240    fn instantiate_canonical_var(
241        &self,
242        kind: CanonicalVarKind<'tcx>,
243        span: Span,
244        var_values: &[ty::GenericArg<'tcx>],
245        universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex,
246    ) -> ty::GenericArg<'tcx> {
247        self.0.instantiate_canonical_var(span, kind, var_values, universe_map)
248    }
249
250    fn add_item_bounds_for_hidden_type(
251        &self,
252        def_id: DefId,
253        args: ty::GenericArgsRef<'tcx>,
254        param_env: ty::ParamEnv<'tcx>,
255        hidden_ty: Ty<'tcx>,
256        goals: &mut Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
257    ) {
258        self.0.add_item_bounds_for_hidden_type(def_id, args, param_env, hidden_ty, goals);
259    }
260
261    fn fetch_eligible_assoc_item(
262        &self,
263        goal_trait_ref: ty::TraitRef<'tcx>,
264        trait_assoc_def_id: DefId,
265        impl_def_id: DefId,
266    ) -> Result<Option<DefId>, ErrorGuaranteed> {
267        let node_item = specialization_graph::assoc_def(self.tcx, impl_def_id, trait_assoc_def_id)?;
268
269        let eligible = if node_item.is_final() {
270            // Non-specializable items are always projectable.
271            true
272        } else {
273            // Only reveal a specializable default if we're past type-checking
274            // and the obligation is monomorphic, otherwise passes such as
275            // transmute checking and polymorphic MIR optimizations could
276            // get a result which isn't correct for all monomorphizations.
277            match self.typing_mode() {
278                TypingMode::Coherence
279                | TypingMode::Analysis { .. }
280                | TypingMode::Borrowck { .. }
281                | TypingMode::PostBorrowckAnalysis { .. } => false,
282                TypingMode::PostAnalysis => {
283                    let poly_trait_ref = self.resolve_vars_if_possible(goal_trait_ref);
284                    !poly_trait_ref.still_further_specializable()
285                }
286            }
287        };
288
289        // FIXME: Check for defaultness here may cause diagnostics problems.
290        if eligible { Ok(Some(node_item.item.def_id)) } else { Ok(None) }
291    }
292
293    // FIXME: This actually should destructure the `Result` we get from transmutability and
294    // register candidates. We probably need to register >1 since we may have an OR of ANDs.
295    fn is_transmutable(
296        &self,
297        dst: Ty<'tcx>,
298        src: Ty<'tcx>,
299        assume: ty::Const<'tcx>,
300    ) -> Result<Certainty, NoSolution> {
301        // Erase regions because we compute layouts in `rustc_transmute`,
302        // which will ICE for region vars.
303        let (dst, src) = self.tcx.erase_and_anonymize_regions((dst, src));
304
305        let Some(assume) = rustc_transmute::Assume::from_const(self.tcx, assume) else {
306            return Err(NoSolution);
307        };
308
309        // FIXME(transmutability): This really should be returning nested goals for `Answer::If*`
310        match rustc_transmute::TransmuteTypeEnv::new(self.0.tcx)
311            .is_transmutable(rustc_transmute::Types { src, dst }, assume)
312        {
313            rustc_transmute::Answer::Yes => Ok(Certainty::Yes),
314            rustc_transmute::Answer::No(_) | rustc_transmute::Answer::If(_) => Err(NoSolution),
315        }
316    }
317}