@@ -20,6 +20,7 @@ extern crate tracing;
2020#[ macro_use]
2121extern crate rustc_middle;
2222
23+ use hir:: ConstContext ;
2324use required_consts:: RequiredConstsVisitor ;
2425use rustc_const_eval:: util;
2526use rustc_data_structures:: fx:: FxIndexSet ;
@@ -251,8 +252,13 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
251252 let const_kind = tcx. hir ( ) . body_const_context ( def) ;
252253
253254 // No need to const-check a non-const `fn`.
254- if const_kind. is_none ( ) {
255- return Default :: default ( ) ;
255+ match const_kind {
256+ Some ( ConstContext :: Const { .. } | ConstContext :: Static ( _) )
257+ | Some ( ConstContext :: ConstFn ) => { }
258+ None => span_bug ! (
259+ tcx. def_span( def) ,
260+ "`mir_const_qualif` should only be called on const fns and const items"
261+ ) ,
256262 }
257263
258264 // N.B., this `borrow()` is guaranteed to be valid (i.e., the value
@@ -317,7 +323,21 @@ fn mir_promoted(
317323 // Ensure that we compute the `mir_const_qualif` for constants at
318324 // this point, before we steal the mir-const result.
319325 // Also this means promotion can rely on all const checks having been done.
320- let const_qualifs = tcx. mir_const_qualif ( def) ;
326+
327+ let const_qualifs = match tcx. def_kind ( def) {
328+ DefKind :: Fn | DefKind :: AssocFn | DefKind :: Closure
329+ if tcx. constness ( def) == hir:: Constness :: Const
330+ || tcx. is_const_default_method ( def. to_def_id ( ) ) =>
331+ {
332+ tcx. mir_const_qualif ( def)
333+ }
334+ DefKind :: AssocConst
335+ | DefKind :: Const
336+ | DefKind :: Static ( _)
337+ | DefKind :: InlineConst
338+ | DefKind :: AnonConst => tcx. mir_const_qualif ( def) ,
339+ _ => ConstQualifs :: default ( ) ,
340+ } ;
321341 let mut body = tcx. mir_const ( def) . steal ( ) ;
322342 if let Some ( error_reported) = const_qualifs. tainted_by_errors {
323343 body. tainted_by_errors = Some ( error_reported) ;
0 commit comments