Skip to main content

rustc_trait_selection/solve/
delegate.rs

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