@@ -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,13 @@ 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 ( _) )
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
+ ) ,
256
262
}
257
263
258
264
// N.B., this `borrow()` is guaranteed to be valid (i.e., the value
@@ -317,7 +323,21 @@ fn mir_promoted(
317
323
// Ensure that we compute the `mir_const_qualif` for constants at
318
324
// this point, before we steal the mir-const result.
319
325
// 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
+ } ;
321
341
let mut body = tcx. mir_const ( def) . steal ( ) ;
322
342
if let Some ( error_reported) = const_qualifs. tainted_by_errors {
323
343
body. tainted_by_errors = Some ( error_reported) ;
0 commit comments