Skip to content

Commit 47d5bb7

Browse files
committed
Don't run the deaggregator on const fns
The deaggregator will generate assignments that aren't allowed in const expressions. This also refactors `is_const_fn` into a method on `TyCtxt`.
1 parent 0345a28 commit 47d5bb7

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

src/librustc/ty/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -2973,6 +2973,27 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
29732973
autoderefs))
29742974
}
29752975

2976+
/// Checks if the given def is a const fn
2977+
pub fn is_const_fn(self, def_id: DefId) -> bool {
2978+
use hir::intravisit::FnKind;
2979+
use hir::map::blocks::FnLikeNode;
2980+
2981+
if let Some(node_id) = self.map.as_local_node_id(def_id) {
2982+
let fn_like = FnLikeNode::from_node(self.map.get(node_id));
2983+
match fn_like.map(|f| f.kind()) {
2984+
Some(FnKind::ItemFn(_, _, _, c, ..)) => {
2985+
c == hir::Constness::Const
2986+
}
2987+
Some(FnKind::Method(_, m, ..)) => {
2988+
m.constness == hir::Constness::Const
2989+
}
2990+
_ => false
2991+
}
2992+
} else {
2993+
self.sess.cstore.is_const_fn(def_id)
2994+
}
2995+
}
2996+
29762997
pub fn upvar_capture(self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture<'tcx>> {
29772998
Some(self.tables.borrow().upvar_capture_map.get(&upvar_id).unwrap().clone())
29782999
}

src/librustc_mir/transform/deaggregator.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ impl<'tcx> MirPass<'tcx> for Deaggregator {
2222
fn run_pass<'a>(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
2323
source: MirSource, mir: &mut Mir<'tcx>) {
2424
let node_id = source.item_id();
25-
let node_path = tcx.item_path_str(tcx.map.local_def_id(node_id));
25+
let def_id = tcx.map.local_def_id(node_id);
26+
let node_path = tcx.item_path_str(def_id);
2627
debug!("running on: {:?}", node_path);
2728
// we only run when mir_opt_level > 1
2829
match tcx.sess.opts.debugging_opts.mir_opt_level {
@@ -37,6 +38,9 @@ impl<'tcx> MirPass<'tcx> for Deaggregator {
3738
// In fact, we might not want to trigger in other cases.
3839
// Ex: when we could use SROA. See issue #35259
3940

41+
// Don't deaggregate in const fns
42+
if tcx.is_const_fn(def_id) { return; }
43+
4044
let mut curr: usize = 0;
4145
for bb in mir.basic_blocks_mut() {
4246
let idx = match get_aggregate_statement_index(curr, &bb.statements) {

src/librustc_mir/transform/qualify_consts.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ use rustc::dep_graph::DepNode;
2020
use rustc::hir;
2121
use rustc::hir::map as hir_map;
2222
use rustc::hir::def_id::DefId;
23-
use rustc::hir::intravisit::FnKind;
24-
use rustc::hir::map::blocks::FnLikeNode;
2523
use rustc::traits::{self, Reveal};
2624
use rustc::ty::{self, TyCtxt, Ty};
2725
use rustc::ty::cast::CastTy;
@@ -116,23 +114,6 @@ impl fmt::Display for Mode {
116114
}
117115
}
118116

119-
fn is_const_fn(tcx: TyCtxt, def_id: DefId) -> bool {
120-
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
121-
let fn_like = FnLikeNode::from_node(tcx.map.get(node_id));
122-
match fn_like.map(|f| f.kind()) {
123-
Some(FnKind::ItemFn(_, _, _, c, ..)) => {
124-
c == hir::Constness::Const
125-
}
126-
Some(FnKind::Method(_, m, ..)) => {
127-
m.constness == hir::Constness::Const
128-
}
129-
_ => false
130-
}
131-
} else {
132-
tcx.sess.cstore.is_const_fn(def_id)
133-
}
134-
}
135-
136117
struct Qualifier<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
137118
mode: Mode,
138119
span: Span,
@@ -778,7 +759,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
778759
ty::TyFnDef(def_id, _, f) => {
779760
(f.abi == Abi::PlatformIntrinsic &&
780761
self.tcx.item_name(def_id).as_str().starts_with("simd_shuffle"),
781-
is_const_fn(self.tcx, def_id))
762+
self.tcx.is_const_fn(def_id))
782763
}
783764
_ => (false, false)
784765
};
@@ -997,7 +978,7 @@ impl<'tcx> MirMapPass<'tcx> for QualifyAndPromoteConstants {
997978
let src = MirSource::from_node(tcx, id);
998979
let mode = match src {
999980
MirSource::Fn(_) => {
1000-
if is_const_fn(tcx, def_id) {
981+
if tcx.is_const_fn(def_id) {
1001982
Mode::ConstFn
1002983
} else {
1003984
Mode::Fn

0 commit comments

Comments
 (0)