Skip to content

Commit 3cc8087

Browse files
committed
qualify_consts: extractt 'determine_mode'.
1 parent 4295eea commit 3cc8087

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::fmt;
3030
use std::ops::{Deref, Index, IndexMut};
3131
use std::usize;
3232

33+
use rustc::hir::HirId;
3334
use crate::transform::{MirPass, MirSource};
3435
use super::promote_consts::{self, Candidate, TempState};
3536

@@ -1596,27 +1597,12 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
15961597
}
15971598

15981599
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);
16171603

16181604
debug!("run_pass: mode={:?}", mode);
1619-
if mode == Mode::NonConstFn || mode == Mode::ConstFn {
1605+
if let Mode::NonConstFn | Mode::ConstFn = mode {
16201606
// This is ugly because Checker holds onto mir,
16211607
// which can't be mutated until its scope ends.
16221608
let (temps, candidates) = {
@@ -1664,6 +1650,11 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
16641650
promote_consts::promote_candidates(def_id, body, tcx, temps, candidates)
16651651
);
16661652
} else {
1653+
let const_promoted_temps = match mode {
1654+
Mode::Const => Some(tcx.mir_const_qualif(def_id).1),
1655+
_ => None,
1656+
};
1657+
16671658
if !body.control_flow_destroyed.is_empty() {
16681659
let mut locals = body.vars_iter();
16691660
if let Some(local) = locals.next() {
@@ -1695,11 +1686,10 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
16951686
error.emit();
16961687
}
16971688
}
1698-
let promoted_temps = if mode == Mode::Const {
1689+
let promoted_temps = match mode {
16991690
// 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,
17031693
};
17041694

17051695
// In `const` and `static` everything without `StorageDead`
@@ -1747,7 +1737,7 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
17471737
let ty = body.return_ty();
17481738
tcx.infer_ctxt().enter(|infcx| {
17491739
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);
17511741
let mut fulfillment_cx = traits::FulfillmentContext::new();
17521742
fulfillment_cx.register_bound(&infcx,
17531743
param_env,
@@ -1765,6 +1755,17 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
17651755
}
17661756
}
17671757

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+
17681769
fn args_required_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<FxHashSet<usize>> {
17691770
let attrs = tcx.get_attrs(def_id);
17701771
let attr = attrs.iter().find(|a| a.check_name(sym::rustc_args_required_const))?;

0 commit comments

Comments
 (0)