@@ -19,6 +19,7 @@ extern crate tracing;
19
19
#[ macro_use]
20
20
extern crate rustc_middle;
21
21
22
+ use hir:: ConstContext ;
22
23
use required_consts:: RequiredConstsVisitor ;
23
24
use rustc_const_eval:: util;
24
25
use rustc_data_structures:: fx:: FxIndexSet ;
@@ -231,8 +232,12 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
231
232
let const_kind = tcx. hir ( ) . body_const_context ( def) ;
232
233
233
234
// No need to const-check a non-const `fn`.
234
- if const_kind. is_none ( ) {
235
- return Default :: default ( ) ;
235
+ match const_kind {
236
+ Some ( ConstContext :: Const | ConstContext :: Static ( _) ) | Some ( ConstContext :: ConstFn ) => { }
237
+ None => span_bug ! (
238
+ tcx. def_span( def) ,
239
+ "`mir_const_qualif` should only be called on const fns and const items"
240
+ ) ,
236
241
}
237
242
238
243
// N.B., this `borrow()` is guaranteed to be valid (i.e., the value
@@ -297,7 +302,21 @@ fn mir_promoted(
297
302
// Ensure that we compute the `mir_const_qualif` for constants at
298
303
// this point, before we steal the mir-const result.
299
304
// Also this means promotion can rely on all const checks having been done.
300
- let const_qualifs = tcx. mir_const_qualif ( def) ;
305
+
306
+ let const_qualifs = match tcx. def_kind ( def) {
307
+ DefKind :: Fn | DefKind :: AssocFn | DefKind :: Closure
308
+ if tcx. constness ( def) == hir:: Constness :: Const
309
+ || tcx. is_const_default_method ( def. to_def_id ( ) ) =>
310
+ {
311
+ tcx. mir_const_qualif ( def)
312
+ }
313
+ DefKind :: AssocConst
314
+ | DefKind :: Const
315
+ | DefKind :: Static ( _)
316
+ | DefKind :: InlineConst
317
+ | DefKind :: AnonConst => tcx. mir_const_qualif ( def) ,
318
+ _ => ConstQualifs :: default ( ) ,
319
+ } ;
301
320
let mut body = tcx. mir_const ( def) . steal ( ) ;
302
321
if let Some ( error_reported) = const_qualifs. tainted_by_errors {
303
322
body. tainted_by_errors = Some ( error_reported) ;
0 commit comments