Skip to main content

rustc_const_eval/check_consts/
qualifs.rs

1//! Structural const qualification.
2//!
3//! See the `Qualif` trait for more info.
4
5// FIXME(const_trait_impl): This API should be really reworked. It's dangerously general for
6// having basically only two use-cases that act in different ways.
7
8use rustc_attr_ir::lang_items::LangItem;
9use rustc_errors::ErrorGuaranteed;
10use rustc_infer::infer::TyCtxtInferExt;
11use rustc_middle::mir;
12use rustc_middle::mir::*;
13use rustc_middle::ty::{self, AdtDef, Ty, TypingMode};
14use rustc_span::bug;
15use rustc_trait_selection::traits::{Obligation, ObligationCause, ObligationCtxt};
16use tracing::instrument;
17
18use super::ConstCx;
19
20pub fn in_any_value_of_ty<'tcx>(
21    cx: &ConstCx<'_, 'tcx>,
22    ty: Ty<'tcx>,
23    tainted_by_errors: Option<ErrorGuaranteed>,
24) -> ConstQualifs {
25    ConstQualifs {
26        has_mut_interior: HasMutInterior::in_any_value_of_ty(cx, ty),
27        needs_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
28        needs_non_const_drop: NeedsNonConstDrop::in_any_value_of_ty(cx, ty),
29        tainted_by_errors,
30    }
31}
32
33/// A "qualif"(-ication) is a way to look for something "bad" in the MIR that would disqualify some
34/// code for promotion or prevent it from evaluating at compile time.
35///
36/// Normally, we would determine what qualifications apply to each type and error when an illegal
37/// operation is performed on such a type. However, this was found to be too imprecise, especially
38/// in the presence of `enum`s. If only a single variant of an enum has a certain qualification, we
39/// needn't reject code unless it actually constructs and operates on the qualified variant.
40///
41/// To accomplish this, const-checking and promotion use a value-based analysis (as opposed to a
42/// type-based one). Qualifications propagate structurally across variables: If a local (or a
43/// projection of a local) is assigned a qualified value, that local itself becomes qualified.
44///
45/// Note that whenever this analysis relies on something being UB and hence impossible in
46/// well-defined code, it is crucial that const-eval actually detects that UB! Const-eval is not
47/// guaranteed to detect all UB, and if it would skip this particular UB we may end up executing
48/// code that has not been const-checked.
49pub trait Qualif {
50    /// The name of the file used to debug the dataflow analysis that computes this qualif.
51    const ANALYSIS_NAME: &'static str;
52
53    /// Whether this `Qualif` is cleared when a local is moved from.
54    const IS_CLEARED_ON_MOVE: bool;
55
56    /// Whether this `Qualif` might be evaluated after the promotion and can encounter a promoted.
57    const ALLOW_PROMOTED: bool;
58
59    /// Extracts the field of `ConstQualifs` that corresponds to this `Qualif`.
60    fn in_qualifs(qualifs: &ConstQualifs) -> bool;
61
62    /// Returns `true` if *any* value of the given type could possibly have this `Qualif`.
63    ///
64    /// This function determines `Qualif`s when we cannot do a value-based analysis. Since qualif
65    /// propagation is context-insensitive, this includes function arguments and values returned
66    /// from a call to another function.
67    ///
68    /// It also determines the `Qualif`s for primitive types.
69    fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool;
70
71    /// Returns `true` if the `Qualif` is structural in an ADT's fields, i.e. if we may
72    /// recurse into an operand *value* to determine whether it has this `Qualif`.
73    ///
74    /// If this returns false, `in_any_value_of_ty` will be invoked to determine the
75    /// final qualif for this ADT.
76    fn is_structural_in_adt_value<'tcx>(cx: &ConstCx<'_, 'tcx>, adt: AdtDef<'tcx>) -> bool;
77}
78
79/// Constant containing interior mutability (`UnsafeCell<T>`).
80/// This must be ruled out to make sure that evaluating the constant at compile-time
81/// and at *any point* during the run-time would produce the same result. In particular,
82/// promotion of temporaries must not change program behavior; if the promoted could be
83/// written to, that would be a problem.
84pub struct HasMutInterior;
85
86impl Qualif for HasMutInterior {
87    const ANALYSIS_NAME: &'static str = "flow_has_mut_interior";
88    const IS_CLEARED_ON_MOVE: bool = false;
89    const ALLOW_PROMOTED: bool = false;
90
91    fn in_qualifs(qualifs: &ConstQualifs) -> bool {
92        qualifs.has_mut_interior
93    }
94
95    fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
96        // Avoid selecting for simple cases, such as builtin types.
97        if ty.is_trivially_freeze() {
98            return false;
99        }
100
101        // Avoid selecting for `UnsafeCell` either.
102        if ty.ty_adt_def().is_some_and(|adt| adt.is_unsafe_cell()) {
103            return true;
104        }
105
106        // We do not use `ty.is_freeze` here, because that requires revealing opaque types, which
107        // requires borrowck, which in turn will invoke mir_const_qualifs again, causing a cycle error.
108        // Instead we invoke an obligation context manually, and provide the opaque type inference settings
109        // that allow the trait solver to just error out instead of cycling.
110        let freeze_def_id = cx.tcx.require_lang_item(LangItem::Freeze, cx.body.span);
111        let did = cx.body.source.def_id().expect_local();
112
113        let typing_env = if cx.tcx.use_typing_mode_post_typeck_until_borrowck() {
114            cx.typing_env
115        } else {
116            ty::TypingEnv::new(cx.typing_env.param_env, TypingMode::analysis_in_body(cx.tcx, did))
117        };
118
119        let (infcx, param_env) = cx.tcx.infer_ctxt().build_with_typing_env(typing_env);
120        let ocx = ObligationCtxt::new(&infcx);
121        let obligation = Obligation::new(
122            cx.tcx,
123            ObligationCause::dummy_with_span(cx.body.span),
124            param_env,
125            ty::TraitRef::new(cx.tcx, freeze_def_id, [ty::GenericArg::from(ty)]),
126        );
127        ocx.register_obligation(obligation);
128        let errors = ocx.evaluate_obligations_error_on_ambiguity();
129        !errors.no_errors()
130    }
131
132    fn is_structural_in_adt_value<'tcx>(_cx: &ConstCx<'_, 'tcx>, adt: AdtDef<'tcx>) -> bool {
133        // Exactly one type, `UnsafeCell`, has the `HasMutInterior` qualif inherently.
134        // It arises structurally for all other types.
135        !adt.is_unsafe_cell()
136    }
137}
138
139/// Constant containing an ADT that implements `Drop`.
140/// This must be ruled out because implicit promotion would remove side-effects
141/// that occur as part of dropping that value. N.B., the implicit promotion has
142/// to reject const Drop implementations because even if side-effects are ruled
143/// out through other means, the execution of the drop could diverge.
144pub struct NeedsDrop;
145
146impl Qualif for NeedsDrop {
147    const ANALYSIS_NAME: &'static str = "flow_needs_drop";
148    const IS_CLEARED_ON_MOVE: bool = true;
149    const ALLOW_PROMOTED: bool = true;
150
151    fn in_qualifs(qualifs: &ConstQualifs) -> bool {
152        qualifs.needs_drop
153    }
154
155    fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
156        ty.needs_drop(cx.tcx, cx.typing_env)
157    }
158
159    fn is_structural_in_adt_value<'tcx>(cx: &ConstCx<'_, 'tcx>, adt: AdtDef<'tcx>) -> bool {
160        !adt.has_dtor(cx.tcx)
161    }
162}
163
164/// Constant containing an ADT that implements non-const `Drop`.
165/// This must be ruled out because we cannot run `Drop` during compile-time.
166pub struct NeedsNonConstDrop;
167
168impl Qualif for NeedsNonConstDrop {
169    const ANALYSIS_NAME: &'static str = "flow_needs_nonconst_drop";
170    const IS_CLEARED_ON_MOVE: bool = true;
171    const ALLOW_PROMOTED: bool = true;
172
173    fn in_qualifs(qualifs: &ConstQualifs) -> bool {
174        qualifs.needs_non_const_drop
175    }
176
177    {}
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
            ::tracing::Level::TRACE <=
                ::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("in_any_value_of_ty",
                                "rustc_const_eval::check_consts::qualifs",
                                ::tracing::Level::TRACE,
                                ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_const_eval/src/check_consts/qualifs.rs"),
                                ::tracing_core::__macro_support::Option::Some(177u32),
                                ::tracing_core::__macro_support::Option::Some("rustc_const_eval::check_consts::qualifs"),
                                ::tracing_core::field::FieldSet::new(&[{
                                                    const NAME:
                                                        ::tracing::__macro_support::FieldName<{
                                                            ::tracing::__macro_support::FieldName::len("ty")
                                                        }> =
                                                        ::tracing::__macro_support::FieldName::new("ty");
                                                    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::TRACE <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::TRACE <=
                                ::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(&ty)
                                                        as &dyn ::tracing::field::Value))])
                        })
            } else {
                let span =
                    ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                {};
                span
            }
        };
    __tracing_attr_guard = __tracing_attr_span.enter();
}
#[allow(clippy :: redundant_closure_call)]
let x =
    (move ||
                {

                    #[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;
                    }
                    {
                        if !ty.needs_drop(cx.tcx, cx.typing_env) { return false; }
                        let destruct_def_id =
                            cx.tcx.require_lang_item(LangItem::Destruct, cx.body.span);
                        let (infcx, param_env) =
                            cx.tcx.infer_ctxt().build_with_typing_env(cx.typing_env);
                        let ocx = ObligationCtxt::new(&infcx);
                        ocx.register_obligation(Obligation::new(cx.tcx,
                                ObligationCause::misc(cx.body.span, cx.def_id()), param_env,
                                ty::Binder::dummy(ty::TraitRef::new(cx.tcx, destruct_def_id,
                                            [ty])).to_host_effect_clause(cx.tcx,
                                    match cx.const_kind() {
                                        rustc_hir::ConstContext::ConstFn =>
                                            ty::BoundConstness::Maybe,
                                        rustc_hir::ConstContext::Static(_) |
                                            rustc_hir::ConstContext::Const { .. } =>
                                            ty::BoundConstness::Const,
                                    })));
                        !ocx.evaluate_obligations_error_on_ambiguity().no_errors()
                    }
                })();
{
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_const_eval/src/check_consts/qualifs.rs:177",
                        "rustc_const_eval::check_consts::qualifs",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/b6eeb93c992027a03e1fd120b6652546396b142c/compiler/rustc_const_eval/src/check_consts/qualifs.rs"),
                        ::tracing_core::__macro_support::Option::Some(177u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::check_consts::qualifs"),
                        ::tracing_core::field::FieldSet::new(&[{
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("return")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("return");
                                            NAME.as_str()
                                        }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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(&::tracing::field::debug(&x)
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};
x;#[instrument(level = "trace", skip(cx), ret)]
178    fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
179        // If this doesn't need drop at all, then don't select `[const] Destruct`.
180        if !ty.needs_drop(cx.tcx, cx.typing_env) {
181            return false;
182        }
183
184        // We check that the type is `[const] Destruct` since that will verify that
185        // the type is both `[const] Drop` (if a drop impl exists for the adt), *and*
186        // that the components of this type are also `[const] Destruct`. This
187        // amounts to verifying that there are no values in this ADT that may have
188        // a non-const drop.
189        let destruct_def_id = cx.tcx.require_lang_item(LangItem::Destruct, cx.body.span);
190        let (infcx, param_env) = cx.tcx.infer_ctxt().build_with_typing_env(cx.typing_env);
191        let ocx = ObligationCtxt::new(&infcx);
192        ocx.register_obligation(Obligation::new(
193            cx.tcx,
194            ObligationCause::misc(cx.body.span, cx.def_id()),
195            param_env,
196            ty::Binder::dummy(ty::TraitRef::new(cx.tcx, destruct_def_id, [ty]))
197                .to_host_effect_clause(
198                    cx.tcx,
199                    match cx.const_kind() {
200                        rustc_hir::ConstContext::ConstFn => ty::BoundConstness::Maybe,
201                        rustc_hir::ConstContext::Static(_)
202                        | rustc_hir::ConstContext::Const { .. } => ty::BoundConstness::Const,
203                    },
204                ),
205        ));
206        !ocx.evaluate_obligations_error_on_ambiguity().no_errors()
207    }
208
209    fn is_structural_in_adt_value<'tcx>(cx: &ConstCx<'_, 'tcx>, adt: AdtDef<'tcx>) -> bool {
210        // As soon as an ADT has a destructor, then the drop becomes non-structural
211        // in its value since:
212        // 1. The destructor may have `[const]` bounds which are not present on the type.
213        //   Someone needs to check that those are satisfied.
214        //   While this could be instead satisfied by checking that the `[const] Drop`
215        //   impl holds (i.e. replicating part of the `in_any_value_of_ty` logic above),
216        //   even in this case, we have another problem, which is,
217        // 2. The destructor may *modify* the operand being dropped, so even if we
218        //   did recurse on the components of the operand, we may not be even dropping
219        //   the same values that were present before the custom destructor was invoked.
220        !adt.has_dtor(cx.tcx)
221    }
222}
223
224// FIXME: Use `mir::visit::Visitor` for the `in_*` functions if/when it supports early return.
225
226/// Returns `true` if this `Rvalue` contains qualif `Q`.
227pub fn in_rvalue<'tcx, Q, F>(
228    cx: &ConstCx<'_, 'tcx>,
229    in_local: &mut F,
230    rvalue: &Rvalue<'tcx>,
231) -> bool
232where
233    Q: Qualif,
234    F: FnMut(Local) -> bool,
235{
236    match rvalue {
237        Rvalue::ThreadLocalRef(_) => Q::in_any_value_of_ty(cx, rvalue.ty(cx.body, cx.tcx)),
238
239        Rvalue::Discriminant(place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
240
241        Rvalue::CopyForDeref(place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
242
243        Rvalue::Use(operand, _)
244        | Rvalue::Repeat(operand, _)
245        | Rvalue::UnaryOp(_, operand)
246        | Rvalue::Cast(_, operand, _) => in_operand::<Q, _>(cx, in_local, operand),
247
248        Rvalue::BinaryOp(_, (lhs, rhs)) => {
249            in_operand::<Q, _>(cx, in_local, lhs) || in_operand::<Q, _>(cx, in_local, rhs)
250        }
251
252        Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => {
253            // Special-case reborrows to be more like a copy of the reference.
254            if let Some((place_base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
255                let base_ty = place_base.ty(cx.body, cx.tcx).ty;
256                if let ty::Ref(..) = base_ty.kind() {
257                    return in_place::<Q, _>(cx, in_local, place_base);
258                }
259            }
260
261            in_place::<Q, _>(cx, in_local, place.as_ref())
262        }
263
264        Rvalue::Reborrow(_, _, place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
265
266        Rvalue::WrapUnsafeBinder(op, _) => in_operand::<Q, _>(cx, in_local, op),
267
268        Rvalue::Aggregate(kind, operands) => {
269            // Return early if we know that the struct or enum being constructed is always
270            // qualified.
271            if let AggregateKind::Adt(adt_did, ..) = **kind {
272                let def = cx.tcx.adt_def(adt_did);
273                // Don't do any value-based reasoning for unions.
274                // Also, if the ADT is not structural in its fields,
275                // then we cannot recurse on its fields. Instead,
276                // we fall back to checking the qualif for *any* value
277                // of the ADT.
278                if def.is_union() || !Q::is_structural_in_adt_value(cx, def) {
279                    return Q::in_any_value_of_ty(cx, rvalue.ty(cx.body, cx.tcx));
280                }
281            }
282
283            // Otherwise, proceed structurally...
284            operands.iter().any(|o| in_operand::<Q, _>(cx, in_local, o))
285        }
286    }
287}
288
289/// Returns `true` if this `Place` contains qualif `Q`.
290pub fn in_place<'tcx, Q, F>(cx: &ConstCx<'_, 'tcx>, in_local: &mut F, place: PlaceRef<'tcx>) -> bool
291where
292    Q: Qualif,
293    F: FnMut(Local) -> bool,
294{
295    let mut place = place;
296    while let Some((place_base, elem)) = place.last_projection() {
297        match elem {
298            ProjectionElem::Index(index) if in_local(index) => return true,
299
300            ProjectionElem::Deref
301            | ProjectionElem::PhantomDeref
302            | ProjectionElem::Field(_, _)
303            | ProjectionElem::OpaqueCast(_)
304            | ProjectionElem::ConstantIndex { .. }
305            | ProjectionElem::Subslice { .. }
306            | ProjectionElem::Downcast(_, _)
307            | ProjectionElem::Index(_)
308            | ProjectionElem::UnwrapUnsafeBinder(_) => {}
309        }
310
311        let base_ty = place_base.ty(cx.body, cx.tcx);
312        let proj_ty = base_ty.projection_ty(cx.tcx, elem).ty;
313        if !Q::in_any_value_of_ty(cx, proj_ty) {
314            return false;
315        }
316
317        // `Deref` currently unconditionally "qualifies" if `in_any_value_of_ty` returns true,
318        // i.e., we treat all qualifs as non-structural for deref projections. Generally,
319        // we can say very little about `*ptr` even if we know that `ptr` satisfies all
320        // sorts of properties.
321        if elem == ProjectionElem::Deref {
322            // We have to assume that this qualifies.
323            return true;
324        }
325
326        place = place_base;
327    }
328
329    if !place.projection.is_empty() {
    ::core::panicking::panic("assertion failed: place.projection.is_empty()")
};assert!(place.projection.is_empty());
330    in_local(place.local)
331}
332
333/// Returns `true` if this `Operand` contains qualif `Q`.
334pub fn in_operand<'tcx, Q, F>(
335    cx: &ConstCx<'_, 'tcx>,
336    in_local: &mut F,
337    operand: &Operand<'tcx>,
338) -> bool
339where
340    Q: Qualif,
341    F: FnMut(Local) -> bool,
342{
343    let constant = match operand {
344        Operand::Copy(place) | Operand::Move(place) => {
345            return in_place::<Q, _>(cx, in_local, place.as_ref());
346        }
347        Operand::RuntimeChecks(_) => return Q::in_any_value_of_ty(cx, cx.tcx.types.bool),
348
349        Operand::Constant(c) => c,
350    };
351
352    // Check the qualifs of the value of `const` items.
353    let uneval = match constant.const_ {
354        Const::Ty(_, ct) => match ct.kind() {
355            ty::ConstKind::Param(_) | ty::ConstKind::Error(_) => None,
356            // Alias consts in MIR bodies don't have associated MIR (e.g. `type const`).
357            ty::ConstKind::Alias(_, _) => None,
358            // FIXME(mgca): Investigate whether using `None` for `ConstKind::Value` is overly
359            // strict, and if instead we should be doing some kind of value-based analysis.
360            ty::ConstKind::Value(_) => None,
361            _ => ::rustc_span::macros::bug_impl(None,
    format_args!("expected ConstKind::Param, ConstKind::Value, ConstKind::Alias, or ConstKind::Error here, found {0:?}",
        ct), Location::caller())bug!(
362                "expected ConstKind::Param, ConstKind::Value, ConstKind::Alias, or ConstKind::Error here, found {:?}",
363                ct
364            ),
365        },
366        Const::Unevaluated(uv, _) => Some(uv),
367        Const::Val(..) => None,
368    };
369
370    if let Some(mir::UnevaluatedConst { def, args: _, promoted }) = uneval {
371        // Use qualifs of the type for the promoted. Promoteds in MIR body should be possible
372        // only for `NeedsNonConstDrop` with precise drop checking. This is the only const
373        // check performed after the promotion. Verify that with an assertion.
374        if !(promoted.is_none() || Q::ALLOW_PROMOTED) {
    ::core::panicking::panic("assertion failed: promoted.is_none() || Q::ALLOW_PROMOTED")
};assert!(promoted.is_none() || Q::ALLOW_PROMOTED);
375
376        // Don't peak inside trait associated constants.
377        if promoted.is_none() && cx.tcx.trait_of_assoc(def).is_none() {
378            let qualifs = cx.tcx.at(constant.span).mir_const_qualif(def);
379
380            if !Q::in_qualifs(&qualifs) {
381                return false;
382            }
383
384            // Just in case the type is more specific than
385            // the definition, e.g., impl associated const
386            // with type parameters, take it into account.
387        }
388    }
389
390    // Otherwise use the qualifs of the type.
391    Q::in_any_value_of_ty(cx, constant.const_.ty())
392}