@@ -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,12 @@ 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 ( _) ) | Some ( ConstContext :: ConstFn ) => { }
257+ None => span_bug ! (
258+ tcx. def_span( def) ,
259+ "`mir_const_qualif` should only be called on const fns and const items"
260+ ) ,
256261 }
257262
258263 // N.B., this `borrow()` is guaranteed to be valid (i.e., the value
@@ -317,7 +322,21 @@ fn mir_promoted(
317322 // Ensure that we compute the `mir_const_qualif` for constants at
318323 // this point, before we steal the mir-const result.
319324 // Also this means promotion can rely on all const checks having been done.
320- let const_qualifs = tcx. mir_const_qualif ( def) ;
325+
326+ let const_qualifs = match tcx. def_kind ( def) {
327+ DefKind :: Fn | DefKind :: AssocFn | DefKind :: Closure
328+ if tcx. constness ( def) == hir:: Constness :: Const
329+ || tcx. is_const_default_method ( def. to_def_id ( ) ) =>
330+ {
331+ tcx. mir_const_qualif ( def)
332+ }
333+ DefKind :: AssocConst
334+ | DefKind :: Const
335+ | DefKind :: Static ( _)
336+ | DefKind :: InlineConst
337+ | DefKind :: AnonConst => tcx. mir_const_qualif ( def) ,
338+ _ => ConstQualifs :: default ( ) ,
339+ } ;
321340 let mut body = tcx. mir_const ( def) . steal ( ) ;
322341 if let Some ( error_reported) = const_qualifs. tainted_by_errors {
323342 body. tainted_by_errors = Some ( error_reported) ;
0 commit comments