Skip to content

Commit ac46091

Browse files
committed
Auto merge of #41733 - nikomatsakis:incr-comp-remove-ast-ty-to-ty-cache, r=eddyb
Remove ast-ty-to-ty cache As discussed on IRC, this basically just removes the cache, and rewrites rustdoc and save-analysis so call into the astconv code. It *might* make sense for this to be a more fine-grained query, but that would (at least) require us to be using `HirId` and not `NodeId`. (Perhaps I should open a FIXME?) I didn't measure perf impact (yet?). I did observe that the cache seems to hit *rarely* -- and only in between items (I experimented with a cache "per def-id", but that had zero hits). In other words, every single hit on the cache is a dependency bug, since it is "shuttling" information between items without dependency edges. r? @eddyb
2 parents b16c7a2 + 3f2dd4d commit ac46091

File tree

14 files changed

+43
-60
lines changed

14 files changed

+43
-60
lines changed

src/Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc/ty/context.rs

-4
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,6 @@ pub struct GlobalCtxt<'tcx> {
566566
/// error reporting, and so is lazily initialised and generally
567567
/// shouldn't taint the common path (hence the RefCell).
568568
pub all_traits: RefCell<Option<Vec<DefId>>>,
569-
570-
/// HIR Ty -> Ty lowering cache.
571-
pub ast_ty_to_ty_cache: RefCell<NodeMap<Ty<'tcx>>>,
572569
}
573570

574571
impl<'tcx> GlobalCtxt<'tcx> {
@@ -786,7 +783,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
786783
derive_macros: RefCell::new(NodeMap()),
787784
stability_interner: RefCell::new(FxHashSet()),
788785
all_traits: RefCell::new(None),
789-
ast_ty_to_ty_cache: RefCell::new(NodeMap()),
790786
}, f)
791787
}
792788

src/librustc_save_analysis/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crate-type = ["dylib"]
1111
[dependencies]
1212
log = "0.3"
1313
rustc = { path = "../librustc" }
14+
rustc_typeck = { path = "../librustc_typeck" }
1415
syntax = { path = "../libsyntax" }
1516
syntax_pos = { path = "../libsyntax_pos" }
1617
rls-data = "0.1"

src/librustc_save_analysis/dump_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
122122
f(self);
123123
self.save_ctxt.tables = old_tables;
124124
} else {
125-
f(self)
125+
f(self);
126126
}
127127
}
128128

src/librustc_save_analysis/lib.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#[macro_use] extern crate log;
2828
#[macro_use] extern crate syntax;
2929
extern crate rustc_serialize;
30+
extern crate rustc_typeck;
3031
extern crate syntax_pos;
3132

3233
extern crate rls_data;
@@ -50,6 +51,7 @@ use rustc::hir::def_id::DefId;
5051
use rustc::session::config::CrateType::CrateTypeExecutable;
5152
use rustc::session::Session;
5253
use rustc::ty::{self, TyCtxt};
54+
use rustc_typeck::hir_ty_to_ty;
5355

5456
use std::env;
5557
use std::fs::File;
@@ -606,11 +608,12 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
606608
Def::Local(def_id)
607609
}
608610

609-
Node::NodeTy(&hir::Ty { node: hir::TyPath(ref qpath), .. }) => {
610-
match *qpath {
611-
hir::QPath::Resolved(_, ref path) => path.def,
612-
hir::QPath::TypeRelative(..) => {
613-
if let Some(ty) = self.tcx.ast_ty_to_ty_cache.borrow().get(&id) {
611+
Node::NodeTy(ty) => {
612+
if let hir::Ty { node: hir::TyPath(ref qpath), .. } = *ty {
613+
match *qpath {
614+
hir::QPath::Resolved(_, ref path) => path.def,
615+
hir::QPath::TypeRelative(..) => {
616+
let ty = hir_ty_to_ty(self.tcx, ty);
614617
if let ty::TyProjection(proj) = ty.sty {
615618
for item in self.tcx.associated_items(proj.trait_ref.def_id) {
616619
if item.kind == ty::AssociatedKind::Type {
@@ -620,9 +623,11 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
620623
}
621624
}
622625
}
626+
Def::Err
623627
}
624-
Def::Err
625628
}
629+
} else {
630+
Def::Err
626631
}
627632
}
628633

src/librustc_typeck/astconv.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ use rustc::ty::wf::object_region_bounds;
2525
use rustc_back::slice;
2626
use require_c_abi_if_variadic;
2727
use util::common::{ErrorReported, FN_OUTPUT_NAME};
28-
use util::nodemap::{NodeMap, FxHashSet};
28+
use util::nodemap::FxHashSet;
2929

30-
use std::cell::RefCell;
3130
use std::iter;
3231
use syntax::{abi, ast};
3332
use syntax::feature_gate::{GateIssue, emit_feature_err};
@@ -37,9 +36,6 @@ use syntax_pos::Span;
3736
pub trait AstConv<'gcx, 'tcx> {
3837
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'gcx, 'tcx>;
3938

40-
/// A cache used for the result of `ast_ty_to_ty_cache`
41-
fn ast_ty_to_ty_cache(&self) -> &RefCell<NodeMap<Ty<'tcx>>>;
42-
4339
/// Returns the set of bounds in scope for the type parameter with
4440
/// the given id.
4541
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId)
@@ -1074,11 +1070,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
10741070

10751071
let tcx = self.tcx();
10761072

1077-
let cache = self.ast_ty_to_ty_cache();
1078-
if let Some(ty) = cache.borrow().get(&ast_ty.id) {
1079-
return ty;
1080-
}
1081-
10821073
let result_ty = match ast_ty.node {
10831074
hir::TySlice(ref ty) => {
10841075
tcx.mk_slice(self.ast_ty_to_ty(&ty))
@@ -1240,8 +1231,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
12401231
}
12411232
};
12421233

1243-
cache.borrow_mut().insert(ast_ty.id, result_ty);
1244-
12451234
result_ty
12461235
}
12471236

src/librustc_typeck/check/coercion.rs

-4
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,6 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
965965
}
966966
}
967967

968-
pub fn is_empty(&self) -> bool {
969-
self.pushed == 0
970-
}
971-
972968
/// Return the "expected type" with which this coercion was
973969
/// constructed. This represents the "downward propagated" type
974970
/// that was given to us at the start of typing whatever construct

src/librustc_typeck/check/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,6 @@ impl<'gcx, 'tcx> EnclosingBreakables<'gcx, 'tcx> {
451451
}
452452

453453
pub struct FnCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
454-
ast_ty_to_ty_cache: RefCell<NodeMap<Ty<'tcx>>>,
455-
456454
body_id: ast::NodeId,
457455

458456
// Number of errors that had been reported when we started
@@ -1516,10 +1514,6 @@ pub fn check_enum<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
15161514
impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
15171515
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.tcx }
15181516

1519-
fn ast_ty_to_ty_cache(&self) -> &RefCell<NodeMap<Ty<'tcx>>> {
1520-
&self.ast_ty_to_ty_cache
1521-
}
1522-
15231517
fn get_free_substs(&self) -> Option<&Substs<'tcx>> {
15241518
Some(&self.parameter_environment.free_substs)
15251519
}
@@ -1621,7 +1615,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
16211615
body_id: ast::NodeId)
16221616
-> FnCtxt<'a, 'gcx, 'tcx> {
16231617
FnCtxt {
1624-
ast_ty_to_ty_cache: RefCell::new(NodeMap()),
16251618
body_id: body_id,
16261619
err_count_on_creation: inh.tcx.sess.err_count(),
16271620
ret_coercion: None,

src/librustc_typeck/check/writeback.rs

-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
4343
wbcx.visit_liberated_fn_sigs();
4444
wbcx.visit_fru_field_types();
4545
wbcx.visit_anon_types();
46-
wbcx.visit_type_nodes();
4746
wbcx.visit_cast_types();
4847
wbcx.visit_lints();
4948
wbcx.visit_free_region_map();
@@ -442,13 +441,6 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
442441
}
443442
}
444443

445-
fn visit_type_nodes(&self) {
446-
for (&id, ty) in self.fcx.ast_ty_to_ty_cache.borrow().iter() {
447-
let ty = self.resolve(ty, &id);
448-
self.fcx.tcx.ast_ty_to_ty_cache.borrow_mut().insert(id, ty);
449-
}
450-
}
451-
452444
fn resolve<T>(&self, x: &T, span: &Locatable) -> T::Lifted
453445
where T: TypeFoldable<'tcx> + ty::Lift<'gcx>
454446
{

src/librustc_typeck/collect.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ use rustc::ty::{ToPredicate, ReprOptions};
6464
use rustc::ty::{self, AdtKind, ToPolyTraitRef, Ty, TyCtxt};
6565
use rustc::ty::maps::Providers;
6666
use rustc::ty::util::IntTypeExt;
67-
use util::nodemap::{NodeMap, FxHashMap};
67+
use util::nodemap::FxHashMap;
6868

6969
use rustc_const_math::ConstInt;
7070

71-
use std::cell::RefCell;
7271
use std::collections::BTreeMap;
7372

7473
use syntax::{abi, ast};
@@ -116,7 +115,7 @@ pub fn provide(providers: &mut Providers) {
116115
/// `ItemCtxt` is parameterized by a `DefId` that it uses to satisfy
117116
/// `get_type_parameter_bounds` requests, drawing the information from
118117
/// the AST (`hir::Generics`), recursively.
119-
struct ItemCtxt<'a,'tcx:'a> {
118+
pub struct ItemCtxt<'a,'tcx:'a> {
120119
tcx: TyCtxt<'a, 'tcx, 'tcx>,
121120
item_def_id: DefId,
122121
}
@@ -180,7 +179,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'a, 'tcx> {
180179
// Utility types and common code for the above passes.
181180

182181
impl<'a, 'tcx> ItemCtxt<'a, 'tcx> {
183-
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
182+
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
184183
-> ItemCtxt<'a,'tcx> {
185184
ItemCtxt {
186185
tcx: tcx,
@@ -190,18 +189,14 @@ impl<'a, 'tcx> ItemCtxt<'a, 'tcx> {
190189
}
191190

192191
impl<'a,'tcx> ItemCtxt<'a,'tcx> {
193-
fn to_ty(&self, ast_ty: &hir::Ty) -> Ty<'tcx> {
192+
pub fn to_ty(&self, ast_ty: &hir::Ty) -> Ty<'tcx> {
194193
AstConv::ast_ty_to_ty(self, ast_ty)
195194
}
196195
}
197196

198197
impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> {
199198
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> { self.tcx }
200199

201-
fn ast_ty_to_ty_cache(&self) -> &RefCell<NodeMap<Ty<'tcx>>> {
202-
&self.tcx.ast_ty_to_ty_cache
203-
}
204-
205200
fn get_type_parameter_bounds(&self,
206201
span: Span,
207202
def_id: DefId)

src/librustc_typeck/lib.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ use std::iter;
122122
// registered before they are used.
123123
pub mod diagnostics;
124124

125-
pub mod check;
126-
pub mod check_unused;
125+
mod check;
126+
mod check_unused;
127127
mod astconv;
128-
pub mod collect;
128+
mod collect;
129129
mod constrained_type_params;
130130
mod impl_wf_check;
131-
pub mod coherence;
132-
pub mod variance;
131+
mod coherence;
132+
mod variance;
133133

134134
pub struct TypeAndSubsts<'tcx> {
135135
pub substs: &'tcx Substs<'tcx>,
@@ -337,4 +337,16 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
337337
}
338338
}
339339

340+
/// A quasi-deprecated helper used in rustdoc and save-analysis to get
341+
/// the type from a HIR node.
342+
pub fn hir_ty_to_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, hir_ty: &hir::Ty) -> Ty<'tcx> {
343+
// In case there are any projections etc, find the "environment"
344+
// def-id that will be used to determine the traits/predicates in
345+
// scope. This is derived from the enclosing item-like thing.
346+
let env_node_id = tcx.hir.get_parent(hir_ty.id);
347+
let env_def_id = tcx.hir.local_def_id(env_node_id);
348+
let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id);
349+
item_cx.to_ty(hir_ty)
350+
}
351+
340352
__build_diagnostic_array! { librustc_typeck, DIAGNOSTICS }

src/librustdoc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rustc_errors = { path = "../librustc_errors" }
2121
rustc_lint = { path = "../librustc_lint" }
2222
rustc_metadata = { path = "../librustc_metadata" }
2323
rustc_resolve = { path = "../librustc_resolve" }
24+
rustc_typeck = { path = "../librustc_typeck" }
2425
rustc_trans = { path = "../librustc_trans" }
2526
serialize = { path = "../libserialize" }
2627
syntax = { path = "../libsyntax" }

src/librustdoc/clean/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use rustc::ty::subst::Substs;
3636
use rustc::ty::{self, AdtKind};
3737
use rustc::middle::stability;
3838
use rustc::util::nodemap::{FxHashMap, FxHashSet};
39+
use rustc_typeck::hir_ty_to_ty;
3940

4041
use rustc::hir;
4142

@@ -1779,10 +1780,9 @@ impl Clean<Type> for hir::Ty {
17791780
}
17801781
TyPath(hir::QPath::TypeRelative(ref qself, ref segment)) => {
17811782
let mut def = Def::Err;
1782-
if let Some(ty) = cx.tcx.ast_ty_to_ty_cache.borrow().get(&self.id) {
1783-
if let ty::TyProjection(proj) = ty.sty {
1784-
def = Def::Trait(proj.trait_ref.def_id);
1785-
}
1783+
let ty = hir_ty_to_ty(cx.tcx, self);
1784+
if let ty::TyProjection(proj) = ty.sty {
1785+
def = Def::Trait(proj.trait_ref.def_id);
17861786
}
17871787
let trait_path = hir::Path {
17881788
span: self.span,

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern crate rustc_resolve;
4141
extern crate rustc_lint;
4242
extern crate rustc_back;
4343
extern crate rustc_metadata;
44+
extern crate rustc_typeck;
4445
extern crate serialize;
4546
#[macro_use] extern crate syntax;
4647
extern crate syntax_pos;

0 commit comments

Comments
 (0)