rustc_next_trait_solver/solve/
effect_goals.rs

1//! Dealing with host effect goals, i.e. enforcing the constness in
2//! `T: const Trait` or `T: [const] Trait`.
3
4use rustc_type_ir::fast_reject::DeepRejectCtxt;
5use rustc_type_ir::inherent::*;
6use rustc_type_ir::lang_items::SolverTraitLangItem;
7use rustc_type_ir::solve::inspect::ProbeKind;
8use rustc_type_ir::solve::{AliasBoundKind, SizedTraitKind};
9use rustc_type_ir::{self as ty, Interner, TypingMode, elaborate};
10use tracing::instrument;
11
12use super::assembly::{Candidate, structural_traits};
13use crate::delegate::SolverDelegate;
14use crate::solve::{
15    BuiltinImplSource, CandidateSource, Certainty, EvalCtxt, Goal, GoalSource, NoSolution,
16    QueryResult, assembly,
17};
18
19impl<D, I> assembly::GoalKind<D> for ty::HostEffectPredicate<I>
20where
21    D: SolverDelegate<Interner = I>,
22    I: Interner,
23{
24    fn self_ty(self) -> I::Ty {
25        self.self_ty()
26    }
27
28    fn trait_ref(self, _: I) -> ty::TraitRef<I> {
29        self.trait_ref
30    }
31
32    fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
33        self.with_replaced_self_ty(cx, self_ty)
34    }
35
36    fn trait_def_id(self, _: I) -> I::TraitId {
37        self.def_id()
38    }
39
40    fn fast_reject_assumption(
41        ecx: &mut EvalCtxt<'_, D>,
42        goal: Goal<I, Self>,
43        assumption: I::Clause,
44    ) -> Result<(), NoSolution> {
45        if let Some(host_clause) = assumption.as_host_effect_clause()
46            && host_clause.def_id() == goal.predicate.def_id()
47            && host_clause.constness().satisfies(goal.predicate.constness)
48            && DeepRejectCtxt::relate_rigid_rigid(ecx.cx()).args_may_unify(
49                goal.predicate.trait_ref.args,
50                host_clause.skip_binder().trait_ref.args,
51            )
52        {
53            Ok(())
54        } else {
55            Err(NoSolution)
56        }
57    }
58
59    fn match_assumption(
60        ecx: &mut EvalCtxt<'_, D>,
61        goal: Goal<I, Self>,
62        assumption: I::Clause,
63        then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
64    ) -> QueryResult<I> {
65        let host_clause = assumption.as_host_effect_clause().unwrap();
66
67        let assumption_trait_pred = ecx.instantiate_binder_with_infer(host_clause);
68        ecx.eq(goal.param_env, goal.predicate.trait_ref, assumption_trait_pred.trait_ref)?;
69
70        then(ecx)
71    }
72
73    /// Register additional assumptions for aliases corresponding to `[const]` item bounds.
74    ///
75    /// Unlike item bounds, they are not simply implied by the well-formedness of the alias.
76    /// Instead, they only hold if the const conditions on the alias also hold. This is why
77    /// we also register the const conditions of the alias after matching the goal against
78    /// the assumption.
79    fn consider_additional_alias_assumptions(
80        ecx: &mut EvalCtxt<'_, D>,
81        goal: Goal<I, Self>,
82        alias_ty: ty::AliasTy<I>,
83    ) -> Vec<Candidate<I>> {
84        let cx = ecx.cx();
85        let mut candidates = vec![];
86
87        if !ecx.cx().alias_has_const_conditions(alias_ty.def_id) {
88            return vec![];
89        }
90
91        for clause in elaborate::elaborate(
92            cx,
93            cx.explicit_implied_const_bounds(alias_ty.def_id)
94                .iter_instantiated(cx, alias_ty.args)
95                .map(|trait_ref| trait_ref.to_host_effect_clause(cx, goal.predicate.constness)),
96        ) {
97            candidates.extend(Self::probe_and_match_goal_against_assumption(
98                ecx,
99                CandidateSource::AliasBound(AliasBoundKind::SelfBounds),
100                goal,
101                clause,
102                |ecx| {
103                    // Const conditions must hold for the implied const bound to hold.
104                    ecx.add_goals(
105                        GoalSource::AliasBoundConstCondition,
106                        cx.const_conditions(alias_ty.def_id)
107                            .iter_instantiated(cx, alias_ty.args)
108                            .map(|trait_ref| {
109                                goal.with(
110                                    cx,
111                                    trait_ref.to_host_effect_clause(cx, goal.predicate.constness),
112                                )
113                            }),
114                    );
115                    ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
116                },
117            ));
118        }
119
120        candidates
121    }
122
123    fn consider_impl_candidate(
124        ecx: &mut EvalCtxt<'_, D>,
125        goal: Goal<I, Self>,
126        impl_def_id: I::ImplId,
127        then: impl FnOnce(&mut EvalCtxt<'_, D>, Certainty) -> QueryResult<I>,
128    ) -> Result<Candidate<I>, NoSolution> {
129        let cx = ecx.cx();
130
131        let impl_trait_ref = cx.impl_trait_ref(impl_def_id);
132        if !DeepRejectCtxt::relate_rigid_infer(ecx.cx())
133            .args_may_unify(goal.predicate.trait_ref.args, impl_trait_ref.skip_binder().args)
134        {
135            return Err(NoSolution);
136        }
137
138        let impl_polarity = cx.impl_polarity(impl_def_id);
139        let certainty = match impl_polarity {
140            ty::ImplPolarity::Negative => return Err(NoSolution),
141            ty::ImplPolarity::Reservation => match ecx.typing_mode() {
142                TypingMode::Coherence => Certainty::AMBIGUOUS,
143                TypingMode::Analysis { .. }
144                | TypingMode::Borrowck { .. }
145                | TypingMode::PostBorrowckAnalysis { .. }
146                | TypingMode::PostAnalysis => return Err(NoSolution),
147            },
148            ty::ImplPolarity::Positive => Certainty::Yes,
149        };
150
151        if !cx.impl_is_const(impl_def_id) {
152            return Err(NoSolution);
153        }
154
155        ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
156            let impl_args = ecx.fresh_args_for_item(impl_def_id.into());
157            ecx.record_impl_args(impl_args);
158            let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args);
159
160            ecx.eq(goal.param_env, goal.predicate.trait_ref, impl_trait_ref)?;
161            let where_clause_bounds = cx
162                .predicates_of(impl_def_id.into())
163                .iter_instantiated(cx, impl_args)
164                .map(|pred| goal.with(cx, pred));
165            ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
166
167            // For this impl to be `const`, we need to check its `[const]` bounds too.
168            let const_conditions = cx
169                .const_conditions(impl_def_id.into())
170                .iter_instantiated(cx, impl_args)
171                .map(|bound_trait_ref| {
172                    goal.with(
173                        cx,
174                        bound_trait_ref.to_host_effect_clause(cx, goal.predicate.constness),
175                    )
176                });
177            ecx.add_goals(GoalSource::ImplWhereBound, const_conditions);
178
179            then(ecx, certainty)
180        })
181    }
182
183    fn consider_error_guaranteed_candidate(
184        ecx: &mut EvalCtxt<'_, D>,
185        _guar: I::ErrorGuaranteed,
186    ) -> Result<Candidate<I>, NoSolution> {
187        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
188            .enter(|ecx| ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes))
189    }
190
191    fn consider_auto_trait_candidate(
192        _ecx: &mut EvalCtxt<'_, D>,
193        _goal: Goal<I, Self>,
194    ) -> Result<Candidate<I>, NoSolution> {
195        unreachable!("auto traits are never const")
196    }
197
198    fn consider_trait_alias_candidate(
199        ecx: &mut EvalCtxt<'_, D>,
200        goal: Goal<I, Self>,
201    ) -> Result<Candidate<I>, NoSolution> {
202        let cx = ecx.cx();
203
204        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
205            let where_clause_bounds = cx
206                .predicates_of(goal.predicate.def_id().into())
207                .iter_instantiated(cx, goal.predicate.trait_ref.args)
208                .map(|p| goal.with(cx, p));
209
210            let const_conditions = cx
211                .const_conditions(goal.predicate.def_id().into())
212                .iter_instantiated(cx, goal.predicate.trait_ref.args)
213                .map(|bound_trait_ref| {
214                    goal.with(
215                        cx,
216                        bound_trait_ref.to_host_effect_clause(cx, goal.predicate.constness),
217                    )
218                });
219            // While you could think of trait aliases to have a single builtin impl
220            // which uses its implied trait bounds as where-clauses, using
221            // `GoalSource::ImplWhereClause` here would be incorrect, as we also
222            // impl them, which means we're "stepping out of the impl constructor"
223            // again. To handle this, we treat these cycles as ambiguous for now.
224            ecx.add_goals(GoalSource::Misc, where_clause_bounds);
225            ecx.add_goals(GoalSource::Misc, const_conditions);
226            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
227        })
228    }
229
230    fn consider_builtin_sizedness_candidates(
231        _ecx: &mut EvalCtxt<'_, D>,
232        _goal: Goal<I, Self>,
233        _sizedness: SizedTraitKind,
234    ) -> Result<Candidate<I>, NoSolution> {
235        unreachable!("Sized/MetaSized is never const")
236    }
237
238    fn consider_builtin_copy_clone_candidate(
239        ecx: &mut EvalCtxt<'_, D>,
240        goal: Goal<I, Self>,
241    ) -> Result<Candidate<I>, NoSolution> {
242        let cx = ecx.cx();
243
244        let self_ty = goal.predicate.self_ty();
245        let constituent_tys =
246            structural_traits::instantiate_constituent_tys_for_copy_clone_trait(ecx, self_ty)?;
247
248        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
249            ecx.enter_forall(constituent_tys, |ecx, tys| {
250                ecx.add_goals(
251                    GoalSource::ImplWhereBound,
252                    tys.into_iter().map(|ty| {
253                        goal.with(
254                            cx,
255                            ty::ClauseKind::HostEffect(
256                                goal.predicate.with_replaced_self_ty(cx, ty),
257                            ),
258                        )
259                    }),
260                );
261            });
262
263            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
264        })
265    }
266
267    fn consider_builtin_fn_ptr_trait_candidate(
268        _ecx: &mut EvalCtxt<'_, D>,
269        _goal: Goal<I, Self>,
270    ) -> Result<Candidate<I>, NoSolution> {
271        todo!("Fn* are not yet const")
272    }
273
274    fn consider_builtin_fn_trait_candidates(
275        ecx: &mut EvalCtxt<'_, D>,
276        goal: Goal<I, Self>,
277        _kind: rustc_type_ir::ClosureKind,
278    ) -> Result<Candidate<I>, NoSolution> {
279        let cx = ecx.cx();
280
281        let self_ty = goal.predicate.self_ty();
282        let (inputs_and_output, def_id, args) =
283            structural_traits::extract_fn_def_from_const_callable(cx, self_ty)?;
284        let (inputs, output) = ecx.instantiate_binder_with_infer(inputs_and_output);
285
286        // A built-in `Fn` impl only holds if the output is sized.
287        // (FIXME: technically we only need to check this if the type is a fn ptr...)
288        let output_is_sized_pred =
289            ty::TraitRef::new(cx, cx.require_trait_lang_item(SolverTraitLangItem::Sized), [output]);
290        let requirements = cx
291            .const_conditions(def_id.into())
292            .iter_instantiated(cx, args)
293            .map(|trait_ref| {
294                (
295                    GoalSource::ImplWhereBound,
296                    goal.with(cx, trait_ref.to_host_effect_clause(cx, goal.predicate.constness)),
297                )
298            })
299            .chain([(GoalSource::ImplWhereBound, goal.with(cx, output_is_sized_pred))]);
300
301        let pred = ty::Binder::dummy(ty::TraitRef::new(
302            cx,
303            goal.predicate.def_id(),
304            [goal.predicate.self_ty(), inputs],
305        ))
306        .to_host_effect_clause(cx, goal.predicate.constness);
307
308        Self::probe_and_consider_implied_clause(
309            ecx,
310            CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
311            goal,
312            pred,
313            requirements,
314        )
315    }
316
317    fn consider_builtin_async_fn_trait_candidates(
318        _ecx: &mut EvalCtxt<'_, D>,
319        _goal: Goal<I, Self>,
320        _kind: rustc_type_ir::ClosureKind,
321    ) -> Result<Candidate<I>, NoSolution> {
322        todo!("AsyncFn* are not yet const")
323    }
324
325    fn consider_builtin_async_fn_kind_helper_candidate(
326        _ecx: &mut EvalCtxt<'_, D>,
327        _goal: Goal<I, Self>,
328    ) -> Result<Candidate<I>, NoSolution> {
329        unreachable!("AsyncFnKindHelper is not const")
330    }
331
332    fn consider_builtin_tuple_candidate(
333        _ecx: &mut EvalCtxt<'_, D>,
334        _goal: Goal<I, Self>,
335    ) -> Result<Candidate<I>, NoSolution> {
336        unreachable!("Tuple trait is not const")
337    }
338
339    fn consider_builtin_pointee_candidate(
340        _ecx: &mut EvalCtxt<'_, D>,
341        _goal: Goal<I, Self>,
342    ) -> Result<Candidate<I>, NoSolution> {
343        unreachable!("Pointee is not const")
344    }
345
346    fn consider_builtin_future_candidate(
347        _ecx: &mut EvalCtxt<'_, D>,
348        _goal: Goal<I, Self>,
349    ) -> Result<Candidate<I>, NoSolution> {
350        unreachable!("Future is not const")
351    }
352
353    fn consider_builtin_iterator_candidate(
354        _ecx: &mut EvalCtxt<'_, D>,
355        _goal: Goal<I, Self>,
356    ) -> Result<Candidate<I>, NoSolution> {
357        todo!("Iterator is not yet const")
358    }
359
360    fn consider_builtin_fused_iterator_candidate(
361        _ecx: &mut EvalCtxt<'_, D>,
362        _goal: Goal<I, Self>,
363    ) -> Result<Candidate<I>, NoSolution> {
364        unreachable!("FusedIterator is not const")
365    }
366
367    fn consider_builtin_async_iterator_candidate(
368        _ecx: &mut EvalCtxt<'_, D>,
369        _goal: Goal<I, Self>,
370    ) -> Result<Candidate<I>, NoSolution> {
371        unreachable!("AsyncIterator is not const")
372    }
373
374    fn consider_builtin_coroutine_candidate(
375        _ecx: &mut EvalCtxt<'_, D>,
376        _goal: Goal<I, Self>,
377    ) -> Result<Candidate<I>, NoSolution> {
378        unreachable!("Coroutine is not const")
379    }
380
381    fn consider_builtin_discriminant_kind_candidate(
382        _ecx: &mut EvalCtxt<'_, D>,
383        _goal: Goal<I, Self>,
384    ) -> Result<Candidate<I>, NoSolution> {
385        unreachable!("DiscriminantKind is not const")
386    }
387
388    fn consider_builtin_destruct_candidate(
389        ecx: &mut EvalCtxt<'_, D>,
390        goal: Goal<I, Self>,
391    ) -> Result<Candidate<I>, NoSolution> {
392        let cx = ecx.cx();
393
394        let self_ty = goal.predicate.self_ty();
395        let const_conditions = structural_traits::const_conditions_for_destruct(cx, self_ty)?;
396
397        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
398            ecx.add_goals(
399                GoalSource::AliasBoundConstCondition,
400                const_conditions.into_iter().map(|trait_ref| {
401                    goal.with(
402                        cx,
403                        ty::Binder::dummy(trait_ref)
404                            .to_host_effect_clause(cx, goal.predicate.constness),
405                    )
406                }),
407            );
408            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
409        })
410    }
411
412    fn consider_builtin_transmute_candidate(
413        _ecx: &mut EvalCtxt<'_, D>,
414        _goal: Goal<I, Self>,
415    ) -> Result<Candidate<I>, NoSolution> {
416        unreachable!("TransmuteFrom is not const")
417    }
418
419    fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
420        _ecx: &mut EvalCtxt<'_, D>,
421        _goal: Goal<I, Self>,
422    ) -> Result<Candidate<I>, NoSolution> {
423        unreachable!("BikeshedGuaranteedNoDrop is not const");
424    }
425
426    fn consider_structural_builtin_unsize_candidates(
427        _ecx: &mut EvalCtxt<'_, D>,
428        _goal: Goal<I, Self>,
429    ) -> Vec<Candidate<I>> {
430        unreachable!("Unsize is not const")
431    }
432}
433
434impl<D, I> EvalCtxt<'_, D>
435where
436    D: SolverDelegate<Interner = I>,
437    I: Interner,
438{
439    #[instrument(level = "trace", skip(self))]
440    pub(super) fn compute_host_effect_goal(
441        &mut self,
442        goal: Goal<I, ty::HostEffectPredicate<I>>,
443    ) -> QueryResult<I> {
444        let (_, proven_via) = self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
445            let trait_goal: Goal<I, ty::TraitPredicate<I>> =
446                goal.with(ecx.cx(), goal.predicate.trait_ref);
447            ecx.compute_trait_goal(trait_goal)
448        })?;
449        self.assemble_and_merge_candidates(proven_via, goal, |_ecx| Err(NoSolution))
450    }
451}