Skip to main content

rustc_trait_selection/traits/
const_evaluatable.rs

1//! Checking that constant values used in types can be successfully evaluated.
2//!
3//! For concrete constants, this is fairly simple as we can just try and evaluate it.
4//!
5//! When dealing with polymorphic constants, for example `size_of::<T>() - 1`,
6//! this is not as easy.
7//!
8//! In this case we try to build an abstract representation of this constant using
9//! `thir_abstract_const` which can then be checked for structural equality with other
10//! generic constants mentioned in the `caller_bounds` of the current environment.
11
12use rustc_infer::infer::InferCtxt;
13use rustc_middle::bug;
14use rustc_middle::traits::ObligationCause;
15use rustc_middle::ty::abstract_const::NotConstEvaluatable;
16use rustc_middle::ty::{self, TyCtxt, TypeVisitable, TypeVisitableExt, TypeVisitor};
17use rustc_span::{DUMMY_SP, Span};
18use tracing::{debug, instrument};
19
20use super::EvaluateConstErr;
21use crate::traits::ObligationCtxt;
22
23/// Check if a given constant can be evaluated.
24#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("is_const_evaluatable",
                                    "rustc_trait_selection::traits::const_evaluatable",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/const_evaluatable.rs"),
                                    ::tracing_core::__macro_support::Option::Some(24u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::const_evaluatable"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("unexpanded_ct")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("unexpanded_ct");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("param_env")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("param_env");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("span")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("span");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&unexpanded_ct)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&param_env)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn ::tracing::field::Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Result<(), NotConstEvaluatable> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = infcx.tcx;
            match tcx.expand_abstract_consts(unexpanded_ct).kind() {
                ty::ConstKind::Alias(_, _) | ty::ConstKind::Expr(_) => (),
                ty::ConstKind::Param(_) | ty::ConstKind::Bound(_, _) |
                    ty::ConstKind::Placeholder(_) | ty::ConstKind::Value(_) |
                    ty::ConstKind::Error(_) => return Ok(()),
                ty::ConstKind::Infer(_) =>
                    return Err(NotConstEvaluatable::MentionsInfer),
            };
            if tcx.features().generic_const_exprs() {
                let ct = tcx.expand_abstract_consts(unexpanded_ct);
                let is_anon_ct =
                    #[allow(non_exhaustive_omitted_patterns)] match ct.kind() {
                        ty::ConstKind::Alias(_, ty::AliasConst {
                            kind: ty::AliasConstKind::Anon { .. }, .. }) => true,
                        _ => false,
                    };
                if !is_anon_ct {
                    if satisfied_from_param_env(tcx, infcx, ct, param_env) {
                        return Ok(());
                    }
                    if ct.has_non_region_infer() {
                        return Err(NotConstEvaluatable::MentionsInfer);
                    } else if ct.has_non_region_param() {
                        return Err(NotConstEvaluatable::MentionsParam);
                    }
                }
                match unexpanded_ct.kind() {
                    ty::ConstKind::Expr(_) => {
                        tcx.dcx().span_bug(span,
                            "evaluating `ConstKind::Expr` is not currently supported");
                    }
                    ty::ConstKind::Alias(_, _) => {
                        match crate::traits::try_evaluate_const(infcx,
                                unexpanded_ct, param_env,
                                |ty| { Ok::<_, !>(ty.skip_norm_wip()) }) {
                            Err(EvaluateConstErr::HasGenericsOrInfers) => {
                                Err(NotConstEvaluatable::Error(infcx.dcx().span_delayed_bug(span,
                                            "Missing value for constant, but no error reported?")))
                            }
                            Err(EvaluateConstErr::EvaluationFailure(e) |
                                EvaluateConstErr::InvalidConstParamTy(e)) =>
                                Err(NotConstEvaluatable::Error(e)),
                            Ok(_) => Ok(()),
                        }
                    }
                    _ =>
                        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected constkind in `is_const_evalautable: {0:?}`",
                                unexpanded_ct)),
                }
            } else if tcx.features().min_generic_const_args() {
                crate::traits::evaluate_const(infcx, unexpanded_ct,
                    param_env);
                Ok(())
            } else {
                let alias_const =
                    match unexpanded_ct.kind() {
                        ty::ConstKind::Alias(_, alias_const) => alias_const,
                        ty::ConstKind::Expr(_) => {
                            ::rustc_middle::util::bug::bug_fmt(format_args!("`ConstKind::Expr` without `feature(generic_const_exprs)` enabled"))
                        }
                        _ =>
                            ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected constkind in `is_const_evalautable: {0:?}`",
                                    unexpanded_ct)),
                    };
                match crate::traits::try_evaluate_const(infcx, unexpanded_ct,
                        param_env, |ty| { Ok::<_, !>(ty.skip_norm_wip()) }) {
                    Err(_) if
                        tcx.sess.is_nightly_build() &&
                            satisfied_from_param_env(tcx, infcx,
                                tcx.expand_abstract_consts(unexpanded_ct), param_env) => {
                        tcx.dcx().struct_span_fatal(if span == DUMMY_SP {
                                            alias_const.kind.def_span(tcx)
                                        } else { span },
                                        "failed to evaluate generic const expression").with_note("the crate this constant originates from uses `#![feature(generic_const_exprs)]`").with_span_suggestion_verbose(DUMMY_SP,
                                "consider enabling this feature",
                                "#![feature(generic_const_exprs)]\n",
                                rustc_errors::Applicability::MaybeIncorrect).emit()
                    }
                    Err(EvaluateConstErr::HasGenericsOrInfers) => {
                        let err =
                            if alias_const.has_non_region_infer() {
                                NotConstEvaluatable::MentionsInfer
                            } else if alias_const.has_non_region_param() {
                                NotConstEvaluatable::MentionsParam
                            } else {
                                let guar =
                                    infcx.dcx().span_delayed_bug(span,
                                        "Missing value for constant, but no error reported?");
                                NotConstEvaluatable::Error(guar)
                            };
                        Err(err)
                    }
                    Err(EvaluateConstErr::EvaluationFailure(e) |
                        EvaluateConstErr::InvalidConstParamTy(e)) =>
                        Err(NotConstEvaluatable::Error(e)),
                    Ok(_) => Ok(()),
                }
            }
        }
    }
}#[instrument(skip(infcx), level = "debug")]
25pub fn is_const_evaluatable<'tcx>(
26    infcx: &InferCtxt<'tcx>,
27    unexpanded_ct: ty::Const<'tcx>,
28    param_env: ty::ParamEnv<'tcx>,
29    span: Span,
30) -> Result<(), NotConstEvaluatable> {
31    let tcx = infcx.tcx;
32    match tcx.expand_abstract_consts(unexpanded_ct).kind() {
33        ty::ConstKind::Alias(_, _) | ty::ConstKind::Expr(_) => (),
34        ty::ConstKind::Param(_)
35        | ty::ConstKind::Bound(_, _)
36        | ty::ConstKind::Placeholder(_)
37        | ty::ConstKind::Value(_)
38        | ty::ConstKind::Error(_) => return Ok(()),
39        ty::ConstKind::Infer(_) => return Err(NotConstEvaluatable::MentionsInfer),
40    };
41
42    if tcx.features().generic_const_exprs() {
43        let ct = tcx.expand_abstract_consts(unexpanded_ct);
44
45        let is_anon_ct = matches!(
46            ct.kind(),
47            ty::ConstKind::Alias(_, ty::AliasConst { kind: ty::AliasConstKind::Anon { .. }, .. })
48        );
49
50        if !is_anon_ct {
51            if satisfied_from_param_env(tcx, infcx, ct, param_env) {
52                return Ok(());
53            }
54            if ct.has_non_region_infer() {
55                return Err(NotConstEvaluatable::MentionsInfer);
56            } else if ct.has_non_region_param() {
57                return Err(NotConstEvaluatable::MentionsParam);
58            }
59        }
60
61        match unexpanded_ct.kind() {
62            ty::ConstKind::Expr(_) => {
63                // FIXME(generic_const_exprs): we have a fully concrete `ConstKind::Expr`, but
64                // haven't implemented evaluating `ConstKind::Expr` yet, so we are unable to tell
65                // if it is evaluatable or not. As this is unreachable for now, we can simple ICE
66                // here.
67                tcx.dcx().span_bug(span, "evaluating `ConstKind::Expr` is not currently supported");
68            }
69            ty::ConstKind::Alias(_, _) => {
70                match crate::traits::try_evaluate_const(infcx, unexpanded_ct, param_env, |ty| {
71                    Ok::<_, !>(ty.skip_norm_wip())
72                }) {
73                    Err(EvaluateConstErr::HasGenericsOrInfers) => {
74                        Err(NotConstEvaluatable::Error(infcx.dcx().span_delayed_bug(
75                            span,
76                            "Missing value for constant, but no error reported?",
77                        )))
78                    }
79                    Err(
80                        EvaluateConstErr::EvaluationFailure(e)
81                        | EvaluateConstErr::InvalidConstParamTy(e),
82                    ) => Err(NotConstEvaluatable::Error(e)),
83                    Ok(_) => Ok(()),
84                }
85            }
86            _ => bug!("unexpected constkind in `is_const_evalautable: {unexpanded_ct:?}`"),
87        }
88    } else if tcx.features().min_generic_const_args() {
89        // This is a sanity check to make sure that non-generics consts are checked to
90        // be evaluatable in case they aren't cchecked elsewhere. This will NOT error
91        // if the const uses generics, as desired.
92        crate::traits::evaluate_const(infcx, unexpanded_ct, param_env);
93        Ok(())
94    } else {
95        let alias_const = match unexpanded_ct.kind() {
96            ty::ConstKind::Alias(_, alias_const) => alias_const,
97            ty::ConstKind::Expr(_) => {
98                bug!("`ConstKind::Expr` without `feature(generic_const_exprs)` enabled")
99            }
100            _ => bug!("unexpected constkind in `is_const_evalautable: {unexpanded_ct:?}`"),
101        };
102
103        match crate::traits::try_evaluate_const(infcx, unexpanded_ct, param_env, |ty| {
104            Ok::<_, !>(ty.skip_norm_wip())
105        }) {
106            // If we're evaluating a generic foreign constant, under a nightly compiler while
107            // the current crate does not enable `feature(generic_const_exprs)`, abort
108            // compilation with a useful error.
109            Err(_)
110                if tcx.sess.is_nightly_build()
111                    && satisfied_from_param_env(
112                        tcx,
113                        infcx,
114                        tcx.expand_abstract_consts(unexpanded_ct),
115                        param_env,
116                    ) =>
117            {
118                tcx.dcx()
119                    .struct_span_fatal(
120                        // Slightly better span than just using `span` alone
121                        if span == DUMMY_SP { alias_const.kind.def_span(tcx) } else { span },
122                        "failed to evaluate generic const expression",
123                    )
124                    .with_note("the crate this constant originates from uses `#![feature(generic_const_exprs)]`")
125                    .with_span_suggestion_verbose(
126                        DUMMY_SP,
127                        "consider enabling this feature",
128                        "#![feature(generic_const_exprs)]\n",
129                        rustc_errors::Applicability::MaybeIncorrect,
130                    )
131                    .emit()
132            }
133
134            Err(EvaluateConstErr::HasGenericsOrInfers) => {
135                let err = if alias_const.has_non_region_infer() {
136                    NotConstEvaluatable::MentionsInfer
137                } else if alias_const.has_non_region_param() {
138                    NotConstEvaluatable::MentionsParam
139                } else {
140                    let guar = infcx.dcx().span_delayed_bug(
141                        span,
142                        "Missing value for constant, but no error reported?",
143                    );
144                    NotConstEvaluatable::Error(guar)
145                };
146
147                Err(err)
148            }
149            Err(
150                EvaluateConstErr::EvaluationFailure(e) | EvaluateConstErr::InvalidConstParamTy(e),
151            ) => Err(NotConstEvaluatable::Error(e)),
152            Ok(_) => Ok(()),
153        }
154    }
155}
156
157#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("satisfied_from_param_env",
                                    "rustc_trait_selection::traits::const_evaluatable",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/const_evaluatable.rs"),
                                    ::tracing_core::__macro_support::Option::Some(157u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::const_evaluatable"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("ct")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("ct");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("param_env")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("param_env");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ct)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&param_env)
                                                            as &dyn ::tracing::field::Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: bool = loop {};
            return __tracing_attr_fake_return;
        }
        {
            struct Visitor<'a, 'tcx> {
                ct: ty::Const<'tcx>,
                param_env: ty::ParamEnv<'tcx>,
                infcx: &'a InferCtxt<'tcx>,
                single_match: Option<Result<ty::Const<'tcx>, ()>>,
            }
            impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for Visitor<'a, 'tcx> {
                fn visit_const(&mut self, c: ty::Const<'tcx>) {
                    {
                        use ::tracing::__macro_support::Callsite as _;
                        static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                            {
                                static META: ::tracing::Metadata<'static> =
                                    {
                                        ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/const_evaluatable.rs:177",
                                            "rustc_trait_selection::traits::const_evaluatable",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/const_evaluatable.rs"),
                                            ::tracing_core::__macro_support::Option::Some(177u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::const_evaluatable"),
                                            ::tracing_core::field::FieldSet::new(&["message"],
                                                ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                            ::tracing::metadata::Kind::EVENT)
                                    };
                                ::tracing::callsite::DefaultCallsite::new(&META)
                            };
                        let enabled =
                            ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                    ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::LevelFilter::current() &&
                                {
                                    let interest = __CALLSITE.interest();
                                    !interest.is_never() &&
                                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                            interest)
                                };
                        if enabled {
                            (|value_set: ::tracing::field::ValueSet|
                                        {
                                            let meta = __CALLSITE.metadata();
                                            ::tracing::Event::dispatch(meta, &value_set);
                                            ;
                                        })({
                                    #[allow(unused_imports)]
                                    use ::tracing::field::{debug, display, Value};
                                    __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("is_const_evaluatable: candidate={0:?}",
                                                                        c) as &dyn ::tracing::field::Value))])
                                });
                        } else { ; }
                    };
                    if self.infcx.probe(|_|
                                {
                                    let ocx = ObligationCtxt::new(self.infcx);
                                    ocx.eq(&ObligationCause::dummy(), self.param_env, c,
                                                self.ct).is_ok() &&
                                        ocx.evaluate_obligations_error_on_ambiguity().no_errors()
                                }) {
                        self.single_match =
                            match self.single_match {
                                None => Some(Ok(c)),
                                Some(Ok(o)) if o == c => Some(Ok(c)),
                                Some(_) => Some(Err(())),
                            };
                    }
                    if let ty::ConstKind::Expr(e) = c.kind() {
                        e.visit_with(self);
                    } else {}
                }
            }
            let mut single_match: Option<Result<ty::Const<'tcx>, ()>> = None;
            for clause in param_env.caller_bounds() {
                match clause.kind().skip_binder() {
                    ty::ClauseKind::ConstEvaluatable(ce) => {
                        let b_ct = tcx.expand_abstract_consts(ce);
                        let mut v = Visitor { ct, infcx, param_env, single_match };
                        let _ = b_ct.visit_with(&mut v);
                        single_match = v.single_match;
                    }
                    _ => {}
                }
            }
            if let Some(Ok(c)) = single_match {
                let ocx = ObligationCtxt::new(infcx);
                if !ocx.eq(&ObligationCause::dummy(), param_env, c,
                                ct).is_ok() {
                    ::core::panicking::panic("assertion failed: ocx.eq(&ObligationCause::dummy(), param_env, c, ct).is_ok()")
                };
                if !ocx.evaluate_obligations_error_on_ambiguity().no_errors()
                    {
                    ::core::panicking::panic("assertion failed: ocx.evaluate_obligations_error_on_ambiguity().no_errors()")
                };
                return true;
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/const_evaluatable.rs:226",
                                    "rustc_trait_selection::traits::const_evaluatable",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/const_evaluatable.rs"),
                                    ::tracing_core::__macro_support::Option::Some(226u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::const_evaluatable"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("is_const_evaluatable: no")
                                                        as &dyn ::tracing::field::Value))])
                        });
                } else { ; }
            };
            false
        }
    }
}#[instrument(skip(infcx, tcx), level = "debug")]
158fn satisfied_from_param_env<'tcx>(
159    tcx: TyCtxt<'tcx>,
160    infcx: &InferCtxt<'tcx>,
161    ct: ty::Const<'tcx>,
162    param_env: ty::ParamEnv<'tcx>,
163) -> bool {
164    // Try to unify with each subtree in the AbstractConst to allow for
165    // `N + 1` being const evaluatable even if theres only a `ConstEvaluatable`
166    // predicate for `(N + 1) * 2`
167    struct Visitor<'a, 'tcx> {
168        ct: ty::Const<'tcx>,
169        param_env: ty::ParamEnv<'tcx>,
170
171        infcx: &'a InferCtxt<'tcx>,
172        single_match: Option<Result<ty::Const<'tcx>, ()>>,
173    }
174
175    impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for Visitor<'a, 'tcx> {
176        fn visit_const(&mut self, c: ty::Const<'tcx>) {
177            debug!("is_const_evaluatable: candidate={:?}", c);
178            if self.infcx.probe(|_| {
179                let ocx = ObligationCtxt::new(self.infcx);
180                ocx.eq(&ObligationCause::dummy(), self.param_env, c, self.ct).is_ok()
181                    && ocx.evaluate_obligations_error_on_ambiguity().no_errors()
182            }) {
183                self.single_match = match self.single_match {
184                    None => Some(Ok(c)),
185                    Some(Ok(o)) if o == c => Some(Ok(c)),
186                    Some(_) => Some(Err(())),
187                };
188            }
189
190            if let ty::ConstKind::Expr(e) = c.kind() {
191                e.visit_with(self);
192            } else {
193                // FIXME(generic_const_exprs): This doesn't recurse into `<T as Trait<U>>::ASSOC`'s args.
194                // This is currently unobservable as `<T as Trait<{ U + 1 }>>::ASSOC` creates an anon const
195                // with its own `ConstEvaluatable` bound in the param env which we will visit separately.
196                //
197                // If we start allowing directly writing `ConstKind::Expr` without an intermediate anon const
198                // this will be incorrect. It might be worth investigating making `clauses_of` elaborate
199                // all of the `ConstEvaluatable` bounds rather than having a visitor here.
200            }
201        }
202    }
203
204    let mut single_match: Option<Result<ty::Const<'tcx>, ()>> = None;
205
206    for clause in param_env.caller_bounds() {
207        match clause.kind().skip_binder() {
208            ty::ClauseKind::ConstEvaluatable(ce) => {
209                let b_ct = tcx.expand_abstract_consts(ce);
210                let mut v = Visitor { ct, infcx, param_env, single_match };
211                let _ = b_ct.visit_with(&mut v);
212
213                single_match = v.single_match;
214            }
215            _ => {} // don't care
216        }
217    }
218
219    if let Some(Ok(c)) = single_match {
220        let ocx = ObligationCtxt::new(infcx);
221        assert!(ocx.eq(&ObligationCause::dummy(), param_env, c, ct).is_ok());
222        assert!(ocx.evaluate_obligations_error_on_ambiguity().no_errors());
223        return true;
224    }
225
226    debug!("is_const_evaluatable: no");
227    false
228}