Skip to content

Commit e22873d

Browse files
committed
rustc: make the const-eval cache polymorphic.
1 parent a941244 commit e22873d

File tree

14 files changed

+150
-149
lines changed

14 files changed

+150
-149
lines changed

src/librustc/dep_graph/dep_node.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub enum DepNode<D: Clone + Debug> {
9797
TypeckBodiesKrate,
9898
TypeckTables(D),
9999
UsedTraitImports(D),
100-
MonomorphicConstEval(D),
100+
ConstEval(D),
101101

102102
// The set of impls for a given trait. Ultimately, it would be
103103
// nice to get more fine-grained here (e.g., to include a
@@ -233,7 +233,7 @@ impl<D: Clone + Debug> DepNode<D> {
233233
InherentImpls(ref d) => op(d).map(InherentImpls),
234234
TypeckTables(ref d) => op(d).map(TypeckTables),
235235
UsedTraitImports(ref d) => op(d).map(UsedTraitImports),
236-
MonomorphicConstEval(ref d) => op(d).map(MonomorphicConstEval),
236+
ConstEval(ref d) => op(d).map(ConstEval),
237237
TraitImpls(ref d) => op(d).map(TraitImpls),
238238
TraitItems(ref d) => op(d).map(TraitItems),
239239
ReprHints(ref d) => op(d).map(ReprHints),

src/librustc/middle/const_val.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ pub fn eval_length(tcx: TyCtxt,
227227
{
228228
let count_expr = &tcx.hir.body(count).value;
229229
let count_def_id = tcx.hir.body_owner_def_id(count);
230-
match ty::queries::monomorphic_const_eval::get(tcx, count_expr.span, count_def_id) {
230+
let substs = Substs::empty();
231+
match ty::queries::const_eval::get(tcx, count_expr.span, (count_def_id, substs)) {
231232
Ok(Integral(Usize(count))) => {
232233
let val = count.as_u64(tcx.sess.target.uint_type);
233234
assert_eq!(val as usize as u64, val);

src/librustc/middle/cstore.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ pub trait CrateStore {
249249
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro;
250250

251251
// misc. metadata
252-
fn maybe_get_item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
253-
-> Option<&'tcx hir::Body>;
252+
fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
253+
-> &'tcx hir::Body;
254254
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body>;
255255
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool;
256256

@@ -399,9 +399,9 @@ impl CrateStore for DummyCrateStore {
399399
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
400400

401401
// misc. metadata
402-
fn maybe_get_item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
403-
-> Option<&'tcx hir::Body> {
404-
bug!("maybe_get_item_body")
402+
fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
403+
-> &'tcx hir::Body {
404+
bug!("item_body")
405405
}
406406
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body> {
407407
bug!("item_body_nested_bodies")

src/librustc/ty/maps.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use middle::privacy::AccessLevels;
1616
use mir;
1717
use session::CompileResult;
1818
use ty::{self, CrateInherentImpls, Ty, TyCtxt};
19+
use ty::subst::Substs;
1920
use util::nodemap::NodeSet;
2021

2122
use rustc_data_structures::indexed_vec::IndexVec;
@@ -74,6 +75,15 @@ impl Key for (CrateNum, DefId) {
7475
}
7576
}
7677

78+
impl<'tcx> Key for (DefId, &'tcx Substs<'tcx>) {
79+
fn map_crate(&self) -> CrateNum {
80+
self.0.krate
81+
}
82+
fn default_span(&self, tcx: TyCtxt) -> Span {
83+
self.0.default_span(tcx)
84+
}
85+
}
86+
7787
trait Value<'tcx>: Sized {
7888
fn from_cycle_error<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self;
7989
}
@@ -217,6 +227,13 @@ impl<'tcx> QueryDescription for queries::reachable_set<'tcx> {
217227
}
218228
}
219229

230+
impl<'tcx> QueryDescription for queries::const_eval<'tcx> {
231+
fn describe(tcx: TyCtxt, (def_id, _): (DefId, &'tcx Substs<'tcx>)) -> String {
232+
format!("const-evaluating `{}`",
233+
tcx.item_path_str(def_id))
234+
}
235+
}
236+
220237
macro_rules! define_maps {
221238
(<$tcx:tt>
222239
$($(#[$attr:meta])*
@@ -446,16 +463,17 @@ define_maps! { <'tcx>
446463
/// (Defined only for LOCAL_CRATE)
447464
pub crate_inherent_impls_overlap_check: crate_inherent_impls_dep_node(CrateNum) -> (),
448465

449-
/// Results of evaluating monomorphic constants embedded in
450-
/// other items, such as enum variant explicit discriminants.
451-
pub monomorphic_const_eval: MonomorphicConstEval(DefId) -> const_val::EvalResult<'tcx>,
466+
/// Results of evaluating const items or constants embedded in
467+
/// other items (such as enum variant explicit discriminants).
468+
pub const_eval: const_eval_dep_node((DefId, &'tcx Substs<'tcx>))
469+
-> const_val::EvalResult<'tcx>,
452470

453471
/// Performs the privacy check and computes "access levels".
454472
pub privacy_access_levels: PrivacyAccessLevels(CrateNum) -> Rc<AccessLevels>,
455473

456474
pub reachable_set: reachability_dep_node(CrateNum) -> Rc<NodeSet>,
457475

458-
pub mir_shims: mir_shim(ty::InstanceDef<'tcx>) -> &'tcx RefCell<mir::Mir<'tcx>>
476+
pub mir_shims: mir_shim_dep_node(ty::InstanceDef<'tcx>) -> &'tcx RefCell<mir::Mir<'tcx>>
459477
}
460478

461479
fn coherent_trait_dep_node((_, def_id): (CrateNum, DefId)) -> DepNode<DefId> {
@@ -470,10 +488,14 @@ fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
470488
DepNode::Reachability
471489
}
472490

473-
fn mir_shim(instance: ty::InstanceDef) -> DepNode<DefId> {
491+
fn mir_shim_dep_node(instance: ty::InstanceDef) -> DepNode<DefId> {
474492
instance.dep_node()
475493
}
476494

477495
fn typeck_item_bodies_dep_node(_: CrateNum) -> DepNode<DefId> {
478496
DepNode::TypeckBodiesKrate
479497
}
498+
499+
fn const_eval_dep_node((def_id, _): (DefId, &Substs)) -> DepNode<DefId> {
500+
DepNode::ConstEval(def_id)
501+
}

src/librustc/ty/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,8 @@ impl<'a, 'gcx, 'tcx> AdtDef {
17011701
self.variants.iter().map(move |v| {
17021702
let mut discr = prev_discr.map_or(initial, |d| d.wrap_incr());
17031703
if let VariantDiscr::Explicit(expr_did) = v.discr {
1704-
match queries::monomorphic_const_eval::get(tcx, DUMMY_SP, expr_did) {
1704+
let substs = Substs::empty();
1705+
match queries::const_eval::get(tcx, DUMMY_SP, (expr_did, substs)) {
17051706
Ok(ConstVal::Integral(v)) => {
17061707
discr = v;
17071708
}
@@ -1733,7 +1734,8 @@ impl<'a, 'gcx, 'tcx> AdtDef {
17331734
explicit_index -= distance;
17341735
}
17351736
ty::VariantDiscr::Explicit(expr_did) => {
1736-
match queries::monomorphic_const_eval::get(tcx, DUMMY_SP, expr_did) {
1737+
let substs = Substs::empty();
1738+
match queries::const_eval::get(tcx, DUMMY_SP, (expr_did, substs)) {
17371739
Ok(ConstVal::Integral(v)) => {
17381740
explicit_value = v;
17391741
break;

0 commit comments

Comments
 (0)