Skip to content

Commit 9f71053

Browse files
committed
On-demand is_const_fn
1 parent e40ef96 commit 9f71053

File tree

11 files changed

+39
-30
lines changed

11 files changed

+39
-30
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ define_dep_nodes!(
339339
ItemSignature(DefId),
340340
ItemVarianceConstraints(DefId),
341341
ItemVariances(DefId),
342+
IsConstFn(DefId),
342343
IsForeignItem(DefId),
343344
TypeParamPredicates { item_id: DefId, param_id: DefId },
344345
SizedConstraint(DefId),

src/librustc/middle/cstore.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ pub trait CrateStore {
243243
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem;
244244

245245
// flags
246-
fn is_const_fn(&self, did: DefId) -> bool;
247246
fn is_dllimport_foreign_item(&self, def: DefId) -> bool;
248247
fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool;
249248

@@ -364,7 +363,6 @@ impl CrateStore for DummyCrateStore {
364363
{ bug!("associated_item_cloned") }
365364

366365
// flags
367-
fn is_const_fn(&self, did: DefId) -> bool { bug!("is_const_fn") }
368366
fn is_dllimport_foreign_item(&self, id: DefId) -> bool { false }
369367
fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool { false }
370368

src/librustc/ty/maps.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,12 @@ impl<'tcx> QueryDescription for queries::is_object_safe<'tcx> {
476476
}
477477
}
478478

479+
impl<'tcx> QueryDescription for queries::is_const_fn<'tcx> {
480+
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
481+
format!("checking if item is const fn: `{}`", tcx.item_path_str(def_id))
482+
}
483+
}
484+
479485
macro_rules! define_maps {
480486
(<$tcx:tt>
481487
$($(#[$attr:meta])*
@@ -791,6 +797,9 @@ define_maps! { <'tcx>
791797
[] adt_sized_constraint: SizedConstraint(DefId) -> &'tcx [Ty<'tcx>],
792798
[] adt_dtorck_constraint: DtorckConstraint(DefId) -> ty::DtorckConstraint<'tcx>,
793799

800+
/// True if this is a const fn
801+
[] is_const_fn: IsConstFn(DefId) -> bool,
802+
794803
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
795804
[] is_foreign_item: IsForeignItem(DefId) -> bool,
796805

src/librustc_const_eval/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ fn eval_const_expr_partial<'a, 'tcx>(cx: &ConstContext<'a, 'tcx>,
351351
signal!(e, TypeckError)
352352
}
353353
} else {
354-
if tcx.sess.cstore.is_const_fn(def_id) {
354+
if tcx.is_const_fn(def_id) {
355355
tcx.sess.cstore.item_body(tcx, def_id)
356356
} else {
357357
signal!(e, TypeckError)

src/librustc_driver/driver.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
899899
reachable::provide(&mut local_providers);
900900
rustc_const_eval::provide(&mut local_providers);
901901
middle::region::provide(&mut local_providers);
902+
cstore::provide_local(&mut local_providers);
902903

903904
let mut extern_providers = ty::maps::Providers::default();
904905
cstore::provide(&mut extern_providers);

src/librustc_metadata/cstore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub use rustc::middle::cstore::{NativeLibrary, NativeLibraryKind, LinkagePrefere
3434
pub use rustc::middle::cstore::NativeLibraryKind::*;
3535
pub use rustc::middle::cstore::{CrateSource, LinkMeta, LibSource};
3636

37-
pub use cstore_impl::provide;
37+
pub use cstore_impl::{provide, provide_local};
3838

3939
// A map from external crate numbers (as decoded from some crate file) to
4040
// local crate numbers (as generated during this session). Each external

src/librustc_metadata/cstore_impl.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc::ty::{self, TyCtxt};
2323
use rustc::ty::maps::Providers;
2424
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
2525
use rustc::hir::map::{DefKey, DefPath, DisambiguatedDefPathData, DefPathHash};
26+
use rustc::hir::map::blocks::FnLikeNode;
2627
use rustc::hir::map::definitions::{DefPathTable, GlobalMetaDataKind};
2728
use rustc::util::nodemap::{NodeSet, DefIdMap};
2829
use rustc_back::PanicStrategy;
@@ -106,6 +107,7 @@ provide! { <'tcx> tcx, def_id, cdata
106107
closure_kind => { cdata.closure_kind(def_id.index) }
107108
closure_type => { cdata.closure_ty(def_id.index, tcx) }
108109
inherent_impls => { Rc::new(cdata.get_inherent_implementations_for_type(def_id.index)) }
110+
is_const_fn => { cdata.is_const_fn(def_id.index) }
109111
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
110112
is_default_impl => { cdata.is_default_impl(def_id.index) }
111113
describe_def => { cdata.get_def(def_id.index) }
@@ -131,6 +133,24 @@ provide! { <'tcx> tcx, def_id, cdata
131133
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
132134
}
133135

136+
pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
137+
fn is_const_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool {
138+
let node_id = tcx.hir.as_local_node_id(def_id)
139+
.expect("Non-local call to local provider is_const_fn");
140+
141+
if let Some(fn_like) = FnLikeNode::from_node(tcx.hir.get(node_id)) {
142+
fn_like.constness() == hir::Constness::Const
143+
} else {
144+
false
145+
}
146+
}
147+
148+
*providers = Providers {
149+
is_const_fn,
150+
..*providers
151+
};
152+
}
153+
134154
impl CrateStore for cstore::CStore {
135155
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any> {
136156
self.get_crate_data(krate)
@@ -172,12 +192,6 @@ impl CrateStore for cstore::CStore {
172192
self.get_crate_data(def.krate).get_associated_item(def.index)
173193
}
174194

175-
fn is_const_fn(&self, did: DefId) -> bool
176-
{
177-
self.read_dep_node(did);
178-
self.get_crate_data(did.krate).is_const_fn(did.index)
179-
}
180-
181195
fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool
182196
{
183197
self.do_is_statically_included_foreign_item(def_id)

src/librustc_mir/transform/copy_prop.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use rustc::mir::transform::{MirPass, MirSource};
3434
use rustc::mir::visit::MutVisitor;
3535
use rustc::ty::TyCtxt;
3636
use util::def_use::DefUseAnalysis;
37-
use transform::qualify_consts;
3837

3938
pub struct CopyPropagation;
4039

@@ -55,7 +54,7 @@ impl MirPass for CopyPropagation {
5554
return
5655
}
5756
MirSource::Fn(function_node_id) => {
58-
if qualify_consts::is_const_fn(tcx, tcx.hir.local_def_id(function_node_id)) {
57+
if tcx.is_const_fn(tcx.hir.local_def_id(function_node_id)) {
5958
// Don't run on const functions, as, again, trans might not be able to evaluate
6059
// the optimized IR.
6160
return

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_data_structures::indexed_vec::{IndexVec, Idx};
1919
use rustc::hir;
2020
use rustc::hir::map as hir_map;
2121
use rustc::hir::def_id::DefId;
22-
use rustc::hir::map::blocks::FnLikeNode;
2322
use rustc::traits::{self, Reveal};
2423
use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
2524
use rustc::ty::cast::CastTy;
@@ -109,18 +108,6 @@ impl fmt::Display for Mode {
109108
}
110109
}
111110

112-
pub fn is_const_fn(tcx: TyCtxt, def_id: DefId) -> bool {
113-
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
114-
if let Some(fn_like) = FnLikeNode::from_node(tcx.hir.get(node_id)) {
115-
fn_like.constness() == hir::Constness::Const
116-
} else {
117-
false
118-
}
119-
} else {
120-
tcx.sess.cstore.is_const_fn(def_id)
121-
}
122-
}
123-
124111
struct Qualifier<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
125112
mode: Mode,
126113
span: Span,
@@ -766,7 +753,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
766753
ty::TyFnDef(def_id, _, f) => {
767754
(f.abi() == Abi::PlatformIntrinsic &&
768755
self.tcx.item_name(def_id).as_str().starts_with("simd_shuffle"),
769-
is_const_fn(self.tcx, def_id))
756+
self.tcx.is_const_fn(def_id))
770757
}
771758
_ => (false, false)
772759
};
@@ -957,7 +944,7 @@ impl MirPass for QualifyAndPromoteConstants {
957944
let def_id = tcx.hir.local_def_id(id);
958945
let mode = match src {
959946
MirSource::Fn(_) => {
960-
if is_const_fn(tcx, def_id) {
947+
if tcx.is_const_fn(def_id) {
961948
Mode::ConstFn
962949
} else {
963950
Mode::Fn

src/librustc_passes/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
101101
fn_like.constness() == hir::Constness::Const
102102
})
103103
} else {
104-
self.tcx.sess.cstore.is_const_fn(def_id)
104+
self.tcx.is_const_fn(def_id)
105105
};
106106
}
107107
}

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn build_external_trait(cx: &DocContext, did: DefId) -> clean::Trait {
151151
fn build_external_function(cx: &DocContext, did: DefId) -> clean::Function {
152152
let sig = cx.tcx.type_of(did).fn_sig();
153153

154-
let constness = if cx.tcx.sess.cstore.is_const_fn(did) {
154+
let constness = if cx.tcx.is_const_fn(did) {
155155
hir::Constness::Const
156156
} else {
157157
hir::Constness::NotConst
@@ -352,7 +352,7 @@ pub fn build_impl(cx: &DocContext, did: DefId, ret: &mut Vec<clean::Item>) {
352352
clean::TyMethodItem(clean::TyMethod {
353353
unsafety, decl, generics, abi
354354
}) => {
355-
let constness = if tcx.sess.cstore.is_const_fn(item.def_id) {
355+
let constness = if tcx.is_const_fn(item.def_id) {
356356
hir::Constness::Const
357357
} else {
358358
hir::Constness::NotConst

0 commit comments

Comments
 (0)