Skip to content

Commit da47c2e

Browse files
committed
rustc_typeck: save the type cache for rustdoc and save-analysis.
1 parent 962633c commit da47c2e

File tree

10 files changed

+111
-81
lines changed

10 files changed

+111
-81
lines changed

src/librustc/ty/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ use ty;
2929
use ty::subst::{Subst, Substs};
3030
use ty::walk::TypeWalker;
3131
use util::common::MemoizationMap;
32-
use util::nodemap::NodeSet;
33-
use util::nodemap::{FxHashMap, FxHashSet};
32+
use util::nodemap::{NodeSet, NodeMap, FxHashMap, FxHashSet};
3433

3534
use serialize::{self, Encodable, Encoder};
3635
use std::borrow::Cow;
@@ -111,12 +110,13 @@ pub type Disr = ConstInt;
111110
/// The complete set of all analyses described in this module. This is
112111
/// produced by the driver and fed to trans and later passes.
113112
#[derive(Clone)]
114-
pub struct CrateAnalysis<'a> {
113+
pub struct CrateAnalysis<'tcx> {
115114
pub export_map: ExportMap,
116115
pub access_levels: middle::privacy::AccessLevels,
117116
pub reachable: NodeSet,
118-
pub name: &'a str,
117+
pub name: String,
119118
pub glob_map: Option<hir::GlobMap>,
119+
pub hir_ty_to_ty: NodeMap<Ty<'tcx>>,
120120
}
121121

122122
#[derive(Copy, Clone)]

src/librustc_driver/driver.rs

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc::middle::{self, dependency_format, stability, reachable};
2525
use rustc::middle::privacy::AccessLevels;
2626
use rustc::ty::{self, TyCtxt};
2727
use rustc::util::common::time;
28-
use rustc::util::nodemap::NodeSet;
28+
use rustc::util::nodemap::{NodeSet, NodeMap};
2929
use rustc_borrowck as borrowck;
3030
use rustc_incremental::{self, IncrementalHashesMap};
3131
use rustc_resolve::{MakeGlobMap, Resolver};
@@ -332,31 +332,31 @@ impl<'a> PhaseController<'a> {
332332
/// State that is passed to a callback. What state is available depends on when
333333
/// during compilation the callback is made. See the various constructor methods
334334
/// (`state_*`) in the impl to see which data is provided for any given entry point.
335-
pub struct CompileState<'a, 'b, 'ast: 'a, 'tcx: 'b> where 'ast: 'tcx {
335+
pub struct CompileState<'a, 'tcx: 'a> {
336336
pub input: &'a Input,
337-
pub session: &'ast Session,
337+
pub session: &'tcx Session,
338338
pub krate: Option<ast::Crate>,
339339
pub registry: Option<Registry<'a>>,
340340
pub cstore: Option<&'a CStore>,
341341
pub crate_name: Option<&'a str>,
342342
pub output_filenames: Option<&'a OutputFilenames>,
343343
pub out_dir: Option<&'a Path>,
344344
pub out_file: Option<&'a Path>,
345-
pub arenas: Option<&'ast ty::CtxtArenas<'ast>>,
345+
pub arenas: Option<&'tcx ty::CtxtArenas<'tcx>>,
346346
pub expanded_crate: Option<&'a ast::Crate>,
347347
pub hir_crate: Option<&'a hir::Crate>,
348-
pub ast_map: Option<&'a hir_map::Map<'ast>>,
348+
pub ast_map: Option<&'a hir_map::Map<'tcx>>,
349349
pub resolutions: Option<&'a Resolutions>,
350-
pub analysis: Option<&'a ty::CrateAnalysis<'a>>,
351-
pub tcx: Option<TyCtxt<'b, 'tcx, 'tcx>>,
350+
pub analysis: Option<&'a ty::CrateAnalysis<'tcx>>,
351+
pub tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
352352
pub trans: Option<&'a trans::CrateTranslation>,
353353
}
354354

355-
impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
355+
impl<'a, 'tcx> CompileState<'a, 'tcx> {
356356
fn empty(input: &'a Input,
357-
session: &'ast Session,
357+
session: &'tcx Session,
358358
out_dir: &'a Option<PathBuf>)
359-
-> CompileState<'a, 'b, 'ast, 'tcx> {
359+
-> Self {
360360
CompileState {
361361
input: input,
362362
session: session,
@@ -379,12 +379,12 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
379379
}
380380

381381
fn state_after_parse(input: &'a Input,
382-
session: &'ast Session,
382+
session: &'tcx Session,
383383
out_dir: &'a Option<PathBuf>,
384384
out_file: &'a Option<PathBuf>,
385385
krate: ast::Crate,
386386
cstore: &'a CStore)
387-
-> CompileState<'a, 'b, 'ast, 'tcx> {
387+
-> Self {
388388
CompileState {
389389
// Initialize the registry before moving `krate`
390390
registry: Some(Registry::new(&session, krate.span)),
@@ -396,13 +396,13 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
396396
}
397397

398398
fn state_after_expand(input: &'a Input,
399-
session: &'ast Session,
399+
session: &'tcx Session,
400400
out_dir: &'a Option<PathBuf>,
401401
out_file: &'a Option<PathBuf>,
402402
cstore: &'a CStore,
403403
expanded_crate: &'a ast::Crate,
404404
crate_name: &'a str)
405-
-> CompileState<'a, 'b, 'ast, 'tcx> {
405+
-> Self {
406406
CompileState {
407407
crate_name: Some(crate_name),
408408
cstore: Some(cstore),
@@ -413,18 +413,18 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
413413
}
414414

415415
fn state_after_hir_lowering(input: &'a Input,
416-
session: &'ast Session,
416+
session: &'tcx Session,
417417
out_dir: &'a Option<PathBuf>,
418418
out_file: &'a Option<PathBuf>,
419-
arenas: &'ast ty::CtxtArenas<'ast>,
419+
arenas: &'tcx ty::CtxtArenas<'tcx>,
420420
cstore: &'a CStore,
421-
hir_map: &'a hir_map::Map<'ast>,
422-
analysis: &'a ty::CrateAnalysis,
421+
hir_map: &'a hir_map::Map<'tcx>,
422+
analysis: &'a ty::CrateAnalysis<'static>,
423423
resolutions: &'a Resolutions,
424424
krate: &'a ast::Crate,
425425
hir_crate: &'a hir::Crate,
426426
crate_name: &'a str)
427-
-> CompileState<'a, 'b, 'ast, 'tcx> {
427+
-> Self {
428428
CompileState {
429429
crate_name: Some(crate_name),
430430
arenas: Some(arenas),
@@ -440,15 +440,15 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
440440
}
441441

442442
fn state_after_analysis(input: &'a Input,
443-
session: &'ast Session,
443+
session: &'tcx Session,
444444
out_dir: &'a Option<PathBuf>,
445445
out_file: &'a Option<PathBuf>,
446446
krate: Option<&'a ast::Crate>,
447447
hir_crate: &'a hir::Crate,
448-
analysis: &'a ty::CrateAnalysis<'a>,
449-
tcx: TyCtxt<'b, 'tcx, 'tcx>,
448+
analysis: &'a ty::CrateAnalysis<'tcx>,
449+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
450450
crate_name: &'a str)
451-
-> CompileState<'a, 'b, 'ast, 'tcx> {
451+
-> Self {
452452
CompileState {
453453
analysis: Some(analysis),
454454
tcx: Some(tcx),
@@ -462,11 +462,11 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
462462

463463

464464
fn state_after_llvm(input: &'a Input,
465-
session: &'ast Session,
465+
session: &'tcx Session,
466466
out_dir: &'a Option<PathBuf>,
467467
out_file: &'a Option<PathBuf>,
468468
trans: &'a trans::CrateTranslation)
469-
-> CompileState<'a, 'b, 'ast, 'tcx> {
469+
-> Self {
470470
CompileState {
471471
trans: Some(trans),
472472
out_file: out_file.as_ref().map(|s| &**s),
@@ -475,10 +475,10 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
475475
}
476476

477477
fn state_when_compilation_done(input: &'a Input,
478-
session: &'ast Session,
478+
session: &'tcx Session,
479479
out_dir: &'a Option<PathBuf>,
480480
out_file: &'a Option<PathBuf>)
481-
-> CompileState<'a, 'b, 'ast, 'tcx> {
481+
-> Self {
482482
CompileState {
483483
out_file: out_file.as_ref().map(|s| &**s),
484484
..CompileState::empty(input, session, out_dir)
@@ -532,10 +532,10 @@ fn count_nodes(krate: &ast::Crate) -> usize {
532532
// For continuing compilation after a parsed crate has been
533533
// modified
534534

535-
pub struct ExpansionResult<'a> {
535+
pub struct ExpansionResult {
536536
pub expanded_crate: ast::Crate,
537537
pub defs: hir_map::Definitions,
538-
pub analysis: ty::CrateAnalysis<'a>,
538+
pub analysis: ty::CrateAnalysis<'static>,
539539
pub resolutions: Resolutions,
540540
pub hir_forest: hir_map::Forest,
541541
}
@@ -547,15 +547,15 @@ pub struct ExpansionResult<'a> {
547547
/// standard library and prelude, and name resolution.
548548
///
549549
/// Returns `None` if we're aborting after handling -W help.
550-
pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
551-
cstore: &CStore,
552-
krate: ast::Crate,
553-
registry: Option<Registry>,
554-
crate_name: &'a str,
555-
addl_plugins: Option<Vec<String>>,
556-
make_glob_map: MakeGlobMap,
557-
after_expand: F)
558-
-> Result<ExpansionResult<'a>, usize>
550+
pub fn phase_2_configure_and_expand<F>(sess: &Session,
551+
cstore: &CStore,
552+
krate: ast::Crate,
553+
registry: Option<Registry>,
554+
crate_name: &str,
555+
addl_plugins: Option<Vec<String>>,
556+
make_glob_map: MakeGlobMap,
557+
after_expand: F)
558+
-> Result<ExpansionResult, usize>
559559
where F: FnOnce(&ast::Crate) -> CompileResult,
560560
{
561561
let time_passes = sess.time_passes();
@@ -789,8 +789,9 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
789789
export_map: resolver.export_map,
790790
access_levels: AccessLevels::default(),
791791
reachable: NodeSet(),
792-
name: crate_name,
792+
name: crate_name.to_string(),
793793
glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
794+
hir_ty_to_ty: NodeMap(),
794795
},
795796
resolutions: Resolutions {
796797
def_map: resolver.def_map,
@@ -807,14 +808,14 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
807808
/// structures carrying the results of the analysis.
808809
pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
809810
hir_map: hir_map::Map<'tcx>,
810-
mut analysis: ty::CrateAnalysis,
811+
mut analysis: ty::CrateAnalysis<'tcx>,
811812
resolutions: Resolutions,
812813
arenas: &'tcx ty::CtxtArenas<'tcx>,
813814
name: &str,
814815
f: F)
815816
-> Result<R, usize>
816817
where F: for<'a> FnOnce(TyCtxt<'a, 'tcx, 'tcx>,
817-
ty::CrateAnalysis,
818+
ty::CrateAnalysis<'tcx>,
818819
IncrementalHashesMap,
819820
CompileResult) -> R
820821
{
@@ -886,7 +887,8 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
886887
|| rustc_incremental::load_dep_graph(tcx, &incremental_hashes_map));
887888

888889
// passes are timed inside typeck
889-
try_with_f!(typeck::check_crate(tcx), (tcx, analysis, incremental_hashes_map));
890+
analysis.hir_ty_to_ty =
891+
try_with_f!(typeck::check_crate(tcx), (tcx, analysis, incremental_hashes_map));
890892

891893
time(time_passes,
892894
"const checking",

src/librustc_driver/pretty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl PpSourceMode {
200200
fn call_with_pp_support_hir<'tcx, A, B, F>(&self,
201201
sess: &'tcx Session,
202202
ast_map: &hir_map::Map<'tcx>,
203-
analysis: &ty::CrateAnalysis,
203+
analysis: &ty::CrateAnalysis<'tcx>,
204204
resolutions: &Resolutions,
205205
arenas: &'tcx ty::CtxtArenas<'tcx>,
206206
id: &str,
@@ -817,7 +817,7 @@ pub fn print_after_parsing(sess: &Session,
817817

818818
pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
819819
ast_map: &hir_map::Map<'tcx>,
820-
analysis: &ty::CrateAnalysis,
820+
analysis: &ty::CrateAnalysis<'tcx>,
821821
resolutions: &Resolutions,
822822
input: &Input,
823823
krate: &ast::Crate,
@@ -934,7 +934,7 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
934934
// Instead, we call that function ourselves.
935935
fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
936936
ast_map: &hir_map::Map<'tcx>,
937-
analysis: &ty::CrateAnalysis,
937+
analysis: &ty::CrateAnalysis<'tcx>,
938938
resolutions: &Resolutions,
939939
crate_name: &str,
940940
arenas: &'tcx ty::CtxtArenas<'tcx>,

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub struct DumpVisitor<'l, 'tcx: 'l, 'll, D: 'll> {
6868
save_ctxt: SaveContext<'l, 'tcx>,
6969
sess: &'l Session,
7070
tcx: TyCtxt<'l, 'tcx, 'tcx>,
71-
analysis: &'l ty::CrateAnalysis<'l>,
7271
dumper: &'ll mut D,
7372

7473
span: SpanUtils<'l>,
@@ -84,17 +83,14 @@ pub struct DumpVisitor<'l, 'tcx: 'l, 'll, D: 'll> {
8483
}
8584

8685
impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
87-
pub fn new(tcx: TyCtxt<'l, 'tcx, 'tcx>,
88-
save_ctxt: SaveContext<'l, 'tcx>,
89-
analysis: &'l ty::CrateAnalysis<'l>,
86+
pub fn new(save_ctxt: SaveContext<'l, 'tcx>,
9087
dumper: &'ll mut D)
9188
-> DumpVisitor<'l, 'tcx, 'll, D> {
92-
let span_utils = SpanUtils::new(&tcx.sess);
89+
let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
9390
DumpVisitor {
94-
sess: &tcx.sess,
95-
tcx: tcx,
91+
sess: &save_ctxt.tcx.sess,
92+
tcx: save_ctxt.tcx,
9693
save_ctxt: save_ctxt,
97-
analysis: analysis,
9894
dumper: dumper,
9995
span: span_utils.clone(),
10096
cur_scope: CRATE_NODE_ID,
@@ -1207,7 +1203,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
12071203
ast::ViewPathGlob(ref path) => {
12081204
// Make a comma-separated list of names of imported modules.
12091205
let mut names = vec![];
1210-
let glob_map = &self.analysis.glob_map;
1206+
let glob_map = &self.save_ctxt.analysis.glob_map;
12111207
let glob_map = glob_map.as_ref().unwrap();
12121208
if glob_map.contains_key(&item.id) {
12131209
for n in glob_map.get(&item.id).unwrap() {

src/librustc_save_analysis/lib.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub mod recorder {
8585

8686
pub struct SaveContext<'l, 'tcx: 'l> {
8787
tcx: TyCtxt<'l, 'tcx, 'tcx>,
88+
analysis: &'l ty::CrateAnalysis<'tcx>,
8889
span_utils: SpanUtils<'tcx>,
8990
}
9091

@@ -93,16 +94,20 @@ macro_rules! option_try(
9394
);
9495

9596
impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
96-
pub fn new(tcx: TyCtxt<'l, 'tcx, 'tcx>) -> SaveContext<'l, 'tcx> {
97+
pub fn new(tcx: TyCtxt<'l, 'tcx, 'tcx>,
98+
analysis: &'l ty::CrateAnalysis<'tcx>)
99+
-> SaveContext<'l, 'tcx> {
97100
let span_utils = SpanUtils::new(&tcx.sess);
98-
SaveContext::from_span_utils(tcx, span_utils)
101+
SaveContext::from_span_utils(tcx, analysis, span_utils)
99102
}
100103

101104
pub fn from_span_utils(tcx: TyCtxt<'l, 'tcx, 'tcx>,
105+
analysis: &'l ty::CrateAnalysis<'tcx>,
102106
span_utils: SpanUtils<'tcx>)
103107
-> SaveContext<'l, 'tcx> {
104108
SaveContext {
105109
tcx: tcx,
110+
analysis: analysis,
106111
span_utils: span_utils,
107112
}
108113
}
@@ -520,8 +525,18 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
520525
match *qpath {
521526
hir::QPath::Resolved(_, ref path) => path.def,
522527
hir::QPath::TypeRelative(..) => {
523-
// FIXME(eddyb) Avoid keeping associated type resolutions.
524-
self.tcx.tables().type_relative_path_defs[&id]
528+
if let Some(ty) = self.analysis.hir_ty_to_ty.get(&id) {
529+
if let ty::TyProjection(proj) = ty.sty {
530+
for item in self.tcx.associated_items(proj.trait_ref.def_id) {
531+
if item.kind == ty::AssociatedKind::Type {
532+
if item.name == proj.item_name {
533+
return Def::AssociatedTy(item.def_id);
534+
}
535+
}
536+
}
537+
}
538+
}
539+
Def::Err
525540
}
526541
}
527542
}
@@ -799,7 +814,7 @@ impl Format {
799814

800815
pub fn process_crate<'l, 'tcx>(tcx: TyCtxt<'l, 'tcx, 'tcx>,
801816
krate: &ast::Crate,
802-
analysis: &'l ty::CrateAnalysis<'l>,
817+
analysis: &'l ty::CrateAnalysis<'tcx>,
803818
cratename: &str,
804819
odir: Option<&Path>,
805820
format: Format) {
@@ -847,12 +862,12 @@ pub fn process_crate<'l, 'tcx>(tcx: TyCtxt<'l, 'tcx, 'tcx>,
847862
root_path.pop();
848863
let output = &mut output_file;
849864

850-
let save_ctxt = SaveContext::new(tcx);
865+
let save_ctxt = SaveContext::new(tcx, analysis);
851866

852867
macro_rules! dump {
853868
($new_dumper: expr) => {{
854869
let mut dumper = $new_dumper;
855-
let mut visitor = DumpVisitor::new(tcx, save_ctxt, analysis, &mut dumper);
870+
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
856871

857872
visitor.dump_crate_info(cratename, krate);
858873
visit::walk_crate(&mut visitor, krate);

0 commit comments

Comments
 (0)