@@ -30,6 +30,7 @@ use std::fmt;
30
30
use std:: ops:: { Deref , Index , IndexMut } ;
31
31
use std:: usize;
32
32
33
+ use rustc:: hir:: HirId ;
33
34
use crate :: transform:: { MirPass , MirSource } ;
34
35
use super :: promote_consts:: { self , Candidate , TempState } ;
35
36
@@ -1596,27 +1597,12 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
1596
1597
}
1597
1598
1598
1599
let def_id = src. def_id ( ) ;
1599
- let id = tcx. hir ( ) . as_local_hir_id ( def_id) . unwrap ( ) ;
1600
- let mut const_promoted_temps = None ;
1601
- let mode = match tcx. hir ( ) . body_owner_kind ( id) {
1602
- hir:: BodyOwnerKind :: Closure => Mode :: NonConstFn ,
1603
- hir:: BodyOwnerKind :: Fn => {
1604
- if tcx. is_const_fn ( def_id) {
1605
- Mode :: ConstFn
1606
- } else {
1607
- Mode :: NonConstFn
1608
- }
1609
- }
1610
- hir:: BodyOwnerKind :: Const => {
1611
- const_promoted_temps = Some ( tcx. mir_const_qualif ( def_id) . 1 ) ;
1612
- Mode :: Const
1613
- }
1614
- hir:: BodyOwnerKind :: Static ( hir:: MutImmutable ) => Mode :: Static ,
1615
- hir:: BodyOwnerKind :: Static ( hir:: MutMutable ) => Mode :: StaticMut ,
1616
- } ;
1600
+ let hir_id = tcx. hir ( ) . as_local_hir_id ( def_id) . unwrap ( ) ;
1601
+
1602
+ let mode = determine_mode ( tcx, hir_id, def_id) ;
1617
1603
1618
1604
debug ! ( "run_pass: mode={:?}" , mode) ;
1619
- if mode == Mode :: NonConstFn || mode == Mode :: ConstFn {
1605
+ if let Mode :: NonConstFn | Mode :: ConstFn = mode {
1620
1606
// This is ugly because Checker holds onto mir,
1621
1607
// which can't be mutated until its scope ends.
1622
1608
let ( temps, candidates) = {
@@ -1664,6 +1650,11 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
1664
1650
promote_consts:: promote_candidates ( def_id, body, tcx, temps, candidates)
1665
1651
) ;
1666
1652
} else {
1653
+ let const_promoted_temps = match mode {
1654
+ Mode :: Const => Some ( tcx. mir_const_qualif ( def_id) . 1 ) ,
1655
+ _ => None ,
1656
+ } ;
1657
+
1667
1658
if !body. control_flow_destroyed . is_empty ( ) {
1668
1659
let mut locals = body. vars_iter ( ) ;
1669
1660
if let Some ( local) = locals. next ( ) {
@@ -1695,11 +1686,10 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
1695
1686
error. emit ( ) ;
1696
1687
}
1697
1688
}
1698
- let promoted_temps = if mode == Mode :: Const {
1689
+ let promoted_temps = match mode {
1699
1690
// Already computed by `mir_const_qualif`.
1700
- const_promoted_temps. unwrap ( )
1701
- } else {
1702
- Checker :: new ( tcx, def_id, body, mode) . check_const ( ) . 1
1691
+ Mode :: Const => const_promoted_temps. unwrap ( ) ,
1692
+ _ => Checker :: new ( tcx, def_id, body, mode) . check_const ( ) . 1 ,
1703
1693
} ;
1704
1694
1705
1695
// In `const` and `static` everything without `StorageDead`
@@ -1747,7 +1737,7 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
1747
1737
let ty = body. return_ty ( ) ;
1748
1738
tcx. infer_ctxt ( ) . enter ( |infcx| {
1749
1739
let param_env = ty:: ParamEnv :: empty ( ) ;
1750
- let cause = traits:: ObligationCause :: new ( body. span , id , traits:: SharedStatic ) ;
1740
+ let cause = traits:: ObligationCause :: new ( body. span , hir_id , traits:: SharedStatic ) ;
1751
1741
let mut fulfillment_cx = traits:: FulfillmentContext :: new ( ) ;
1752
1742
fulfillment_cx. register_bound ( & infcx,
1753
1743
param_env,
@@ -1765,6 +1755,17 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
1765
1755
}
1766
1756
}
1767
1757
1758
+ fn determine_mode ( tcx : TyCtxt < ' _ > , hir_id : HirId , def_id : DefId ) -> Mode {
1759
+ match tcx. hir ( ) . body_owner_kind ( hir_id) {
1760
+ hir:: BodyOwnerKind :: Closure => Mode :: NonConstFn ,
1761
+ hir:: BodyOwnerKind :: Fn if tcx. is_const_fn ( def_id) => Mode :: ConstFn ,
1762
+ hir:: BodyOwnerKind :: Fn => Mode :: NonConstFn ,
1763
+ hir:: BodyOwnerKind :: Const => Mode :: Const ,
1764
+ hir:: BodyOwnerKind :: Static ( hir:: MutImmutable ) => Mode :: Static ,
1765
+ hir:: BodyOwnerKind :: Static ( hir:: MutMutable ) => Mode :: StaticMut ,
1766
+ }
1767
+ }
1768
+
1768
1769
fn args_required_const ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> Option < FxHashSet < usize > > {
1769
1770
let attrs = tcx. get_attrs ( def_id) ;
1770
1771
let attr = attrs. iter ( ) . find ( |a| a. check_name ( sym:: rustc_args_required_const) ) ?;
0 commit comments