Skip to main content

rustc_trait_selection/traits/
effects.rs

1use rustc_hir::{self as hir, LangItem};
2use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes};
3use rustc_infer::traits::{
4    ImplDerivedHostCause, ImplSource, Obligation, ObligationCause, ObligationCauseCode,
5    PredicateObligation,
6};
7use rustc_middle::span_bug;
8use rustc_middle::traits::query::NoSolution;
9use rustc_middle::ty::elaborate::elaborate;
10use rustc_middle::ty::fast_reject::DeepRejectCtxt;
11use rustc_middle::ty::{self, Ty, Unnormalized};
12use thin_vec::{ThinVec, thin_vec};
13
14use super::SelectionContext;
15use super::normalize::normalize_with_depth_to;
16
17pub type HostEffectObligation<'tcx> = Obligation<'tcx, ty::HostEffectPredicate<'tcx>>;
18
19pub enum EvaluationFailure {
20    Ambiguous,
21    NoSolution,
22}
23
24pub fn evaluate_host_effect_obligation<'tcx>(
25    selcx: &mut SelectionContext<'_, 'tcx>,
26    obligation: &HostEffectObligation<'tcx>,
27) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
28    if selcx.infcx.typing_mode().is_coherence() {
29        ::rustc_middle::util::bug::span_bug_fmt(obligation.cause.span,
    format_args!("should not select host obligation in old solver in intercrate mode"));span_bug!(
30            obligation.cause.span,
31            "should not select host obligation in old solver in intercrate mode"
32        );
33    }
34
35    let ref obligation = selcx.infcx.resolve_vars_if_possible(obligation.clone());
36
37    // Force ambiguity for infer self ty.
38    if obligation.predicate.self_ty().is_ty_var() {
39        return Err(EvaluationFailure::Ambiguous);
40    }
41
42    match evaluate_host_effect_from_bounds(selcx, obligation) {
43        Ok(result) => return Ok(result),
44        Err(EvaluationFailure::Ambiguous) => return Err(EvaluationFailure::Ambiguous),
45        Err(EvaluationFailure::NoSolution) => {}
46    }
47
48    match evaluate_host_effect_from_conditionally_const_item_bounds(selcx, obligation) {
49        Ok(result) => return Ok(result),
50        Err(EvaluationFailure::Ambiguous) => return Err(EvaluationFailure::Ambiguous),
51        Err(EvaluationFailure::NoSolution) => {}
52    }
53
54    match evaluate_host_effect_from_item_bounds(selcx, obligation) {
55        Ok(result) => return Ok(result),
56        Err(EvaluationFailure::Ambiguous) => return Err(EvaluationFailure::Ambiguous),
57        Err(EvaluationFailure::NoSolution) => {}
58    }
59
60    match evaluate_host_effect_from_builtin_impls(selcx, obligation) {
61        Ok(result) => return Ok(result),
62        Err(EvaluationFailure::Ambiguous) => return Err(EvaluationFailure::Ambiguous),
63        Err(EvaluationFailure::NoSolution) => {}
64    }
65
66    match evaluate_host_effect_from_selection_candidate(selcx, obligation) {
67        Ok(result) => return Ok(result),
68        Err(EvaluationFailure::Ambiguous) => return Err(EvaluationFailure::Ambiguous),
69        Err(EvaluationFailure::NoSolution) => {}
70    }
71
72    match evaluate_host_effect_from_trait_alias(selcx, obligation) {
73        Ok(result) => return Ok(result),
74        Err(EvaluationFailure::Ambiguous) => return Err(EvaluationFailure::Ambiguous),
75        Err(EvaluationFailure::NoSolution) => {}
76    }
77
78    Err(EvaluationFailure::NoSolution)
79}
80
81fn match_candidate<'tcx>(
82    selcx: &mut SelectionContext<'_, 'tcx>,
83    obligation: &HostEffectObligation<'tcx>,
84    candidate: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
85    candidate_is_unnormalized: bool,
86    more_nested: impl FnOnce(&mut SelectionContext<'_, 'tcx>, &mut ThinVec<PredicateObligation<'tcx>>),
87) -> Result<ThinVec<PredicateObligation<'tcx>>, NoSolution> {
88    if !candidate.skip_binder().constness.satisfies(obligation.predicate.constness) {
89        return Err(NoSolution);
90    }
91
92    let mut candidate = selcx.infcx.instantiate_binder_with_fresh_vars(
93        obligation.cause.span,
94        BoundRegionConversionTime::HigherRankedType,
95        candidate,
96    );
97
98    let mut nested = ::thin_vec::ThinVec::new()thin_vec![];
99
100    // Unlike param-env bounds, item bounds may not be normalized.
101    if candidate_is_unnormalized {
102        candidate = normalize_with_depth_to(
103            selcx,
104            obligation.param_env,
105            obligation.cause.clone(),
106            obligation.recursion_depth,
107            candidate,
108            &mut nested,
109        );
110    }
111
112    nested.extend(
113        selcx
114            .infcx
115            .at(&obligation.cause, obligation.param_env)
116            .eq(DefineOpaqueTypes::Yes, obligation.predicate.trait_ref, candidate.trait_ref)?
117            .into_obligations(),
118    );
119
120    more_nested(selcx, &mut nested);
121
122    Ok(nested)
123}
124
125fn evaluate_host_effect_from_bounds<'tcx>(
126    selcx: &mut SelectionContext<'_, 'tcx>,
127    obligation: &HostEffectObligation<'tcx>,
128) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
129    let infcx = selcx.infcx;
130    let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
131    let mut candidate = None;
132
133    for clause in obligation.param_env.caller_bounds() {
134        let bound_clause = clause.kind();
135        let ty::ClauseKind::HostEffect(data) = bound_clause.skip_binder() else {
136            continue;
137        };
138        let data = bound_clause.rebind(data);
139        if data.skip_binder().trait_ref.def_id != obligation.predicate.trait_ref.def_id {
140            continue;
141        }
142
143        if !drcx
144            .args_may_unify(obligation.predicate.trait_ref.args, data.skip_binder().trait_ref.args)
145        {
146            continue;
147        }
148
149        let is_match =
150            infcx.probe(|_| match_candidate(selcx, obligation, data, false, |_, _| {}).is_ok());
151
152        if is_match {
153            if candidate.is_some() {
154                return Err(EvaluationFailure::Ambiguous);
155            } else {
156                candidate = Some(data);
157            }
158        }
159    }
160
161    if let Some(data) = candidate {
162        Ok(match_candidate(selcx, obligation, data, false, |_, _| {})
163            .expect("candidate matched before, so it should match again"))
164    } else {
165        Err(EvaluationFailure::NoSolution)
166    }
167}
168
169/// Assembles constness bounds from `~const` item bounds on alias types, which only
170/// hold if the `~const` where bounds also hold and the parent trait is `~const`.
171fn evaluate_host_effect_from_conditionally_const_item_bounds<'tcx>(
172    selcx: &mut SelectionContext<'_, 'tcx>,
173    obligation: &HostEffectObligation<'tcx>,
174) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
175    let infcx = selcx.infcx;
176    let tcx = infcx.tcx;
177    let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
178    let mut candidate = None;
179
180    let mut consider_ty = obligation.predicate.self_ty();
181    while let ty::Alias(
182        alias_ty @ ty::AliasTy {
183            kind: kind @ (ty::Projection { def_id } | ty::Opaque { def_id }),
184            ..
185        },
186    ) = *consider_ty.kind()
187    {
188        if tcx.is_conditionally_const(def_id) {
189            for clause in elaborate(
190                tcx,
191                tcx.explicit_implied_const_bounds(def_id)
192                    .iter_instantiated_copied(tcx, alias_ty.args)
193                    .map(Unnormalized::skip_norm_wip)
194                    .map(|(trait_ref, _)| {
195                        trait_ref.to_host_effect_clause(tcx, obligation.predicate.constness)
196                    }),
197            ) {
198                let bound_clause = clause.kind();
199                let ty::ClauseKind::HostEffect(data) = bound_clause.skip_binder() else {
200                    {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("should not elaborate non-HostEffect from HostEffect")));
}unreachable!("should not elaborate non-HostEffect from HostEffect")
201                };
202                let data = bound_clause.rebind(data);
203                if data.skip_binder().trait_ref.def_id != obligation.predicate.trait_ref.def_id {
204                    continue;
205                }
206
207                if !drcx.args_may_unify(
208                    obligation.predicate.trait_ref.args,
209                    data.skip_binder().trait_ref.args,
210                ) {
211                    continue;
212                }
213
214                let is_match = infcx
215                    .probe(|_| match_candidate(selcx, obligation, data, true, |_, _| {}).is_ok());
216
217                if is_match {
218                    if candidate.is_some() {
219                        return Err(EvaluationFailure::Ambiguous);
220                    } else {
221                        candidate = Some((data, alias_ty));
222                    }
223                }
224            }
225        }
226
227        if !#[allow(non_exhaustive_omitted_patterns)] match kind {
    ty::Projection { .. } => true,
    _ => false,
}matches!(kind, ty::Projection { .. }) {
228            break;
229        }
230
231        consider_ty = alias_ty.self_ty();
232    }
233
234    if let Some((data, alias_ty)) = candidate {
235        Ok(match_candidate(selcx, obligation, data, true, |selcx, nested| {
236            // An alias bound only holds if we also check the const conditions
237            // of the alias, so we need to register those, too.
238            let const_conditions =
239                tcx.const_conditions(alias_ty.kind.def_id()).instantiate(tcx, alias_ty.args);
240            let const_conditions: Vec<_> = const_conditions
241                .into_iter()
242                .map(|(trait_ref, span)| {
243                    let trait_ref = normalize_with_depth_to(
244                        selcx,
245                        obligation.param_env,
246                        obligation.cause.clone(),
247                        obligation.recursion_depth,
248                        trait_ref.skip_norm_wip(),
249                        nested,
250                    );
251                    (trait_ref, span)
252                })
253                .collect();
254            nested.extend(const_conditions.into_iter().map(|(trait_ref, _)| {
255                obligation
256                    .with(tcx, trait_ref.to_host_effect_clause(tcx, obligation.predicate.constness))
257            }));
258        })
259        .expect("candidate matched before, so it should match again"))
260    } else {
261        Err(EvaluationFailure::NoSolution)
262    }
263}
264
265/// Assembles constness bounds "normal" item bounds on aliases, which may include
266/// unconditionally `const` bounds that are *not* conditional and thus always hold.
267fn evaluate_host_effect_from_item_bounds<'tcx>(
268    selcx: &mut SelectionContext<'_, 'tcx>,
269    obligation: &HostEffectObligation<'tcx>,
270) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
271    let infcx = selcx.infcx;
272    let tcx = infcx.tcx;
273    let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
274    let mut candidate = None;
275
276    let mut consider_ty = obligation.predicate.self_ty();
277    while let ty::Alias(
278        alias_ty @ ty::AliasTy {
279            kind: kind @ (ty::Projection { def_id } | ty::Opaque { def_id }),
280            ..
281        },
282    ) = *consider_ty.kind()
283    {
284        for clause in tcx
285            .item_bounds(def_id)
286            .iter_instantiated(tcx, alias_ty.args)
287            .map(Unnormalized::skip_norm_wip)
288        {
289            let bound_clause = clause.kind();
290            let ty::ClauseKind::HostEffect(data) = bound_clause.skip_binder() else {
291                continue;
292            };
293            let data = bound_clause.rebind(data);
294            if data.skip_binder().trait_ref.def_id != obligation.predicate.trait_ref.def_id {
295                continue;
296            }
297
298            if !drcx.args_may_unify(
299                obligation.predicate.trait_ref.args,
300                data.skip_binder().trait_ref.args,
301            ) {
302                continue;
303            }
304
305            let is_match =
306                infcx.probe(|_| match_candidate(selcx, obligation, data, true, |_, _| {}).is_ok());
307
308            if is_match {
309                if candidate.is_some() {
310                    return Err(EvaluationFailure::Ambiguous);
311                } else {
312                    candidate = Some(data);
313                }
314            }
315        }
316
317        if !#[allow(non_exhaustive_omitted_patterns)] match kind {
    ty::Projection { .. } => true,
    _ => false,
}matches!(kind, ty::Projection { .. }) {
318            break;
319        }
320
321        consider_ty = alias_ty.self_ty();
322    }
323
324    if let Some(data) = candidate {
325        Ok(match_candidate(selcx, obligation, data, true, |_, _| {})
326            .expect("candidate matched before, so it should match again"))
327    } else {
328        Err(EvaluationFailure::NoSolution)
329    }
330}
331
332fn evaluate_host_effect_from_builtin_impls<'tcx>(
333    selcx: &mut SelectionContext<'_, 'tcx>,
334    obligation: &HostEffectObligation<'tcx>,
335) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
336    match selcx.tcx().as_lang_item(obligation.predicate.def_id()) {
337        Some(LangItem::Copy | LangItem::Clone) => {
338            evaluate_host_effect_for_copy_clone_goal(selcx, obligation)
339        }
340        Some(LangItem::Destruct) => evaluate_host_effect_for_destruct_goal(selcx, obligation),
341        Some(LangItem::Fn | LangItem::FnMut | LangItem::FnOnce) => {
342            evaluate_host_effect_for_fn_goal(selcx, obligation)
343        }
344        _ => Err(EvaluationFailure::NoSolution),
345    }
346}
347
348fn evaluate_host_effect_for_copy_clone_goal<'tcx>(
349    selcx: &mut SelectionContext<'_, 'tcx>,
350    obligation: &HostEffectObligation<'tcx>,
351) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
352    let tcx = selcx.tcx();
353    let self_ty = obligation.predicate.self_ty();
354    let constituent_tys = match *self_ty.kind() {
355        // impl Copy/Clone for FnDef, FnPtr
356        ty::FnDef(..) | ty::FnPtr(..) | ty::Error(_) => Ok(ty::Binder::dummy(::alloc::vec::Vec::new()vec![])),
357
358        // Implementations are provided in core
359        ty::Uint(_)
360        | ty::Int(_)
361        | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
362        | ty::Bool
363        | ty::Float(_)
364        | ty::Char
365        | ty::RawPtr(..)
366        | ty::Never
367        | ty::Ref(_, _, ty::Mutability::Not)
368        | ty::Array(..) => Err(EvaluationFailure::NoSolution),
369
370        // Cannot implement in core, as we can't be generic over patterns yet,
371        // so we'd have to list all patterns and type combinations.
372        ty::Pat(ty, ..) => Ok(ty::Binder::dummy(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [ty]))vec![ty])),
373
374        ty::Dynamic(..)
375        | ty::Str
376        | ty::Slice(_)
377        | ty::Foreign(..)
378        | ty::Ref(_, _, ty::Mutability::Mut)
379        | ty::Adt(_, _)
380        | ty::Alias(_)
381        | ty::Param(_)
382        | ty::Placeholder(..) => Err(EvaluationFailure::NoSolution),
383
384        ty::Bound(..)
385        | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
386            {
    ::core::panicking::panic_fmt(format_args!("unexpected type `{0:?}`",
            self_ty));
}panic!("unexpected type `{self_ty:?}`")
387        }
388
389        // impl Copy/Clone for (T1, T2, .., Tn) where T1: Copy/Clone, T2: Copy/Clone, .. Tn: Copy/Clone
390        ty::Tuple(tys) => Ok(ty::Binder::dummy(tys.to_vec())),
391
392        // impl Copy/Clone for Closure where Self::TupledUpvars: Copy/Clone
393        ty::Closure(_, args) => Ok(ty::Binder::dummy(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [args.as_closure().tupled_upvars_ty()]))vec![args.as_closure().tupled_upvars_ty()])),
394
395        // impl Copy/Clone for CoroutineClosure where Self::TupledUpvars: Copy/Clone
396        ty::CoroutineClosure(_, args) => {
397            Ok(ty::Binder::dummy(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [args.as_coroutine_closure().tupled_upvars_ty()]))vec![args.as_coroutine_closure().tupled_upvars_ty()]))
398        }
399
400        // only when `coroutine_clone` is enabled and the coroutine is movable
401        // impl Copy/Clone for Coroutine where T: Copy/Clone forall T in (upvars, witnesses)
402        ty::Coroutine(def_id, args) => {
403            if selcx.should_stall_coroutine(def_id) {
404                return Err(EvaluationFailure::Ambiguous);
405            }
406            match tcx.coroutine_movability(def_id) {
407                ty::Movability::Static => Err(EvaluationFailure::NoSolution),
408                ty::Movability::Movable => {
409                    if tcx.features().coroutine_clone() {
410                        Ok(ty::Binder::dummy(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [args.as_coroutine().tupled_upvars_ty(),
                Ty::new_coroutine_witness_for_coroutine(tcx, def_id, args)]))vec![
411                            args.as_coroutine().tupled_upvars_ty(),
412                            Ty::new_coroutine_witness_for_coroutine(tcx, def_id, args),
413                        ]))
414                    } else {
415                        Err(EvaluationFailure::NoSolution)
416                    }
417                }
418            }
419        }
420
421        ty::UnsafeBinder(_) => Err(EvaluationFailure::NoSolution),
422
423        // impl Copy/Clone for CoroutineWitness where T: Copy/Clone forall T in coroutine_hidden_types
424        ty::CoroutineWitness(def_id, args) => Ok(tcx
425            .coroutine_hidden_types(def_id)
426            .instantiate(tcx, args)
427            .skip_norm_wip()
428            .map_bound(|bound| bound.types.to_vec())),
429    }?;
430
431    Ok(constituent_tys
432        .iter()
433        .map(|ty| {
434            obligation.with(
435                tcx,
436                ty.map_bound(|ty| ty::TraitRef::new(tcx, obligation.predicate.def_id(), [ty]))
437                    .to_host_effect_clause(tcx, obligation.predicate.constness),
438            )
439        })
440        .collect())
441}
442
443// NOTE: Keep this in sync with `const_conditions_for_destruct` in the new solver.
444fn evaluate_host_effect_for_destruct_goal<'tcx>(
445    selcx: &mut SelectionContext<'_, 'tcx>,
446    obligation: &HostEffectObligation<'tcx>,
447) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
448    let tcx = selcx.tcx();
449    let destruct_def_id = tcx.require_lang_item(LangItem::Destruct, obligation.cause.span);
450    let self_ty = obligation.predicate.self_ty();
451
452    let const_conditions = match *self_ty.kind() {
453        // `ManuallyDrop` is trivially `[const] Destruct` as we do not run any drop glue on it.
454        ty::Adt(adt_def, _) if adt_def.is_manually_drop() => ::thin_vec::ThinVec::new()thin_vec![],
455
456        // An ADT is `[const] Destruct` only if all of the fields are,
457        // *and* if there is a `Drop` impl, that `Drop` impl is also `[const]`.
458        ty::Adt(adt_def, args) => {
459            let mut const_conditions: ThinVec<_> = adt_def
460                .all_fields()
461                .map(|field| ty::TraitRef::new(tcx, destruct_def_id, [field.ty(tcx, args)]))
462                .collect();
463            match adt_def.destructor(tcx).map(|dtor| tcx.constness(dtor.did)) {
464                // `Drop` impl exists, but it's not const. Type cannot be `[const] Destruct`.
465                Some(hir::Constness::NotConst) => return Err(EvaluationFailure::NoSolution),
466                // `Drop` impl exists, and it's const. Require `Ty: [const] Drop` to hold.
467                Some(hir::Constness::Const) => {
468                    let drop_def_id = tcx.require_lang_item(LangItem::Drop, obligation.cause.span);
469                    let drop_trait_ref = ty::TraitRef::new(tcx, drop_def_id, [self_ty]);
470                    const_conditions.push(drop_trait_ref);
471                }
472                // No `Drop` impl, no need to require anything else.
473                None => {}
474            }
475            const_conditions
476        }
477
478        ty::Array(ty, _) | ty::Pat(ty, _) | ty::Slice(ty) => {
479            {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(ty::TraitRef::new(tcx, destruct_def_id, [ty]));
    vec
}thin_vec![ty::TraitRef::new(tcx, destruct_def_id, [ty])]
480        }
481
482        ty::Tuple(tys) => {
483            tys.iter().map(|field_ty| ty::TraitRef::new(tcx, destruct_def_id, [field_ty])).collect()
484        }
485
486        // Trivially implement `[const] Destruct`
487        ty::Bool
488        | ty::Char
489        | ty::Int(..)
490        | ty::Uint(..)
491        | ty::Float(..)
492        | ty::Str
493        | ty::RawPtr(..)
494        | ty::Ref(..)
495        | ty::FnDef(..)
496        | ty::FnPtr(..)
497        | ty::Never
498        | ty::Infer(ty::InferTy::FloatVar(_) | ty::InferTy::IntVar(_))
499        | ty::Error(_) => ::thin_vec::ThinVec::new()thin_vec![],
500
501        // Closures are [const] Destruct when all of their upvars (captures) are [const] Destruct.
502        ty::Closure(_, args) => {
503            let closure_args = args.as_closure();
504            {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(ty::TraitRef::new(tcx, destruct_def_id,
            [closure_args.tupled_upvars_ty()]));
    vec
}thin_vec![ty::TraitRef::new(tcx, destruct_def_id, [closure_args.tupled_upvars_ty()])]
505        }
506
507        // Coroutines could implement `[const] Drop`,
508        // but they don't really need to right now.
509        ty::CoroutineClosure(_, _) | ty::Coroutine(_, _) | ty::CoroutineWitness(_, _) => {
510            return Err(EvaluationFailure::NoSolution);
511        }
512
513        // FIXME(unsafe_binders): Unsafe binders could implement `[const] Drop`
514        // if their inner type implements it.
515        ty::UnsafeBinder(_) => return Err(EvaluationFailure::NoSolution),
516
517        ty::Dynamic(..) | ty::Param(_) | ty::Alias(..) | ty::Placeholder(_) | ty::Foreign(_) => {
518            return Err(EvaluationFailure::NoSolution);
519        }
520
521        ty::Bound(..)
522        | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
523            {
    ::core::panicking::panic_fmt(format_args!("unexpected type `{0:?}`",
            self_ty));
}panic!("unexpected type `{self_ty:?}`")
524        }
525    };
526
527    Ok(const_conditions
528        .into_iter()
529        .map(|trait_ref| {
530            obligation.with(
531                tcx,
532                ty::Binder::dummy(trait_ref)
533                    .to_host_effect_clause(tcx, obligation.predicate.constness),
534            )
535        })
536        .collect())
537}
538
539// NOTE: Keep this in sync with `extract_fn_def_from_const_callable` in the new solver.
540fn evaluate_host_effect_for_fn_goal<'tcx>(
541    selcx: &mut SelectionContext<'_, 'tcx>,
542    obligation: &HostEffectObligation<'tcx>,
543) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
544    let tcx = selcx.tcx();
545    let self_ty = obligation.predicate.self_ty();
546
547    let (def, args) = match *self_ty.kind() {
548        ty::FnDef(def, args) => (def, args),
549
550        // We may support function pointers at some point in the future
551        ty::FnPtr(..) => return Err(EvaluationFailure::NoSolution),
552
553        // Coroutines could implement `[const] Fn`,
554        // but they don't really need to right now.
555        ty::CoroutineClosure(_, _) => return Err(EvaluationFailure::NoSolution),
556
557        ty::Closure(def, args) => {
558            // For now we limit ourselves to closures without binders. The next solver can handle them.
559            let sig =
560                args.as_closure().sig().no_bound_vars().ok_or(EvaluationFailure::NoSolution)?;
561            (
562                def,
563                tcx.mk_args_from_iter(
564                    [ty::GenericArg::from(*sig.inputs().get(0).unwrap()), sig.output().into()]
565                        .into_iter(),
566                ),
567            )
568        }
569
570        // Everything else needs explicit impls or cannot have an impl
571        _ => return Err(EvaluationFailure::NoSolution),
572    };
573
574    match tcx.constness(def) {
575        hir::Constness::Const => Ok(tcx
576            .const_conditions(def)
577            .instantiate(tcx, args)
578            .into_iter()
579            .map(|(c, span)| {
580                let code = ObligationCauseCode::WhereClause(def, span);
581                let cause =
582                    ObligationCause::new(obligation.cause.span, obligation.cause.body_id, code);
583                Obligation::new(
584                    tcx,
585                    cause,
586                    obligation.param_env,
587                    c.to_host_effect_clause(tcx, obligation.predicate.constness).skip_norm_wip(),
588                )
589            })
590            .collect()),
591        hir::Constness::NotConst => Err(EvaluationFailure::NoSolution),
592    }
593}
594
595fn evaluate_host_effect_from_selection_candidate<'tcx>(
596    selcx: &mut SelectionContext<'_, 'tcx>,
597    obligation: &HostEffectObligation<'tcx>,
598) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
599    let tcx = selcx.tcx();
600    selcx.infcx.commit_if_ok(|_| {
601        match selcx.select(&obligation.with(tcx, obligation.predicate.trait_ref)) {
602            Ok(None) => Err(EvaluationFailure::Ambiguous),
603            Err(_) => Err(EvaluationFailure::NoSolution),
604            Ok(Some(source)) => match source {
605                ImplSource::UserDefined(impl_) => {
606                    if tcx.impl_trait_header(impl_.impl_def_id).constness != hir::Constness::Const {
607                        return Err(EvaluationFailure::NoSolution);
608                    }
609
610                    let mut nested = impl_.nested;
611                    nested.extend(
612                        tcx.const_conditions(impl_.impl_def_id)
613                            .instantiate(tcx, impl_.args)
614                            .into_iter()
615                            .map(|(trait_ref, span)| {
616                                Obligation::new(
617                                    tcx,
618                                    obligation.cause.clone().derived_host_cause(
619                                        ty::Binder::dummy(obligation.predicate),
620                                        |derived| {
621                                            ObligationCauseCode::ImplDerivedHost(Box::new(
622                                                ImplDerivedHostCause {
623                                                    derived,
624                                                    impl_def_id: impl_.impl_def_id,
625                                                    span,
626                                                },
627                                            ))
628                                        },
629                                    ),
630                                    obligation.param_env,
631                                    trait_ref
632                                        .to_host_effect_clause(tcx, obligation.predicate.constness)
633                                        .skip_norm_wip(),
634                                )
635                            }),
636                    );
637
638                    Ok(nested)
639                }
640                _ => Err(EvaluationFailure::NoSolution),
641            },
642        }
643    })
644}
645
646fn evaluate_host_effect_from_trait_alias<'tcx>(
647    selcx: &mut SelectionContext<'_, 'tcx>,
648    obligation: &HostEffectObligation<'tcx>,
649) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
650    let tcx = selcx.tcx();
651    let def_id = obligation.predicate.def_id();
652    if !tcx.trait_is_alias(def_id) {
653        return Err(EvaluationFailure::NoSolution);
654    }
655
656    Ok(tcx
657        .const_conditions(def_id)
658        .instantiate(tcx, obligation.predicate.trait_ref.args)
659        .into_iter()
660        .map(|(trait_ref, span)| {
661            Obligation::new(
662                tcx,
663                obligation.cause.clone().derived_host_cause(
664                    ty::Binder::dummy(obligation.predicate),
665                    |derived| {
666                        ObligationCauseCode::ImplDerivedHost(Box::new(ImplDerivedHostCause {
667                            derived,
668                            impl_def_id: def_id,
669                            span,
670                        }))
671                    },
672                ),
673                obligation.param_env,
674                trait_ref
675                    .to_host_effect_clause(tcx, obligation.predicate.constness)
676                    .skip_norm_wip(),
677            )
678        })
679        .collect())
680}