@@ -20,6 +20,7 @@ extern crate tracing;
20
20
#[ macro_use]
21
21
extern crate rustc_middle;
22
22
23
+ use hir:: ConstContext ;
23
24
use required_consts:: RequiredConstsVisitor ;
24
25
use rustc_const_eval:: util;
25
26
use rustc_data_structures:: fx:: FxIndexSet ;
@@ -251,8 +252,12 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
251
252
let const_kind = tcx. hir ( ) . body_const_context ( def) ;
252
253
253
254
// 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
+ ) ,
256
261
}
257
262
258
263
// N.B., this `borrow()` is guaranteed to be valid (i.e., the value
@@ -317,7 +322,21 @@ fn mir_promoted(
317
322
// Ensure that we compute the `mir_const_qualif` for constants at
318
323
// this point, before we steal the mir-const result.
319
324
// 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
+ } ;
321
340
let mut body = tcx. mir_const ( def) . steal ( ) ;
322
341
if let Some ( error_reported) = const_qualifs. tainted_by_errors {
323
342
body. tainted_by_errors = Some ( error_reported) ;
0 commit comments