Skip to content

Commit a4d2424

Browse files
committed
Auto merge of #33443 - jseyfried:resolve_ast, r=nrc
Perform name resolution before and during ast->hir lowering This PR performs name resolution before and during ast->hir lowering instead of in phase 3. r? @nrc
2 parents 72ed7e7 + 805666a commit a4d2424

File tree

20 files changed

+706
-555
lines changed

20 files changed

+706
-555
lines changed

src/librustc/hir/lowering.rs

+91-70
Large diffs are not rendered by default.

src/librustc/hir/map/definitions.rs

+4
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ impl Definitions {
195195
self.opt_def_index(node).map(DefId::local)
196196
}
197197

198+
pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
199+
self.opt_local_def_id(node).unwrap()
200+
}
201+
198202
pub fn as_local_node_id(&self, def_id: DefId) -> Option<ast::NodeId> {
199203
if def_id.krate == LOCAL_CRATE {
200204
assert!(def_id.index.as_usize() < self.data.len());

src/librustc/hir/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,7 @@ pub type FreevarMap = NodeMap<Vec<Freevar>>;
16391639

16401640
pub type CaptureModeMap = NodeMap<CaptureClause>;
16411641

1642+
#[derive(Clone)]
16421643
pub struct TraitCandidate {
16431644
pub def_id: DefId,
16441645
pub import_id: Option<NodeId>,

src/librustc/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub type Disr = ConstInt;
108108

109109
/// The complete set of all analyses described in this module. This is
110110
/// produced by the driver and fed to trans and later passes.
111+
#[derive(Clone)]
111112
pub struct CrateAnalysis<'a> {
112113
pub export_map: ExportMap,
113114
pub access_levels: middle::privacy::AccessLevels,

src/librustc_driver/driver.rs

+71-38
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
use rustc::dep_graph::DepGraph;
1212
use rustc::hir;
13-
use rustc::hir::map as hir_map;
13+
use rustc::hir::{map as hir_map, FreevarMap, TraitMap};
14+
use rustc::hir::def::DefMap;
1415
use rustc_mir as mir;
1516
use rustc::mir::mir_map::MirMap;
1617
use rustc::session::{Session, CompileResult, compile_result_from_err_count};
@@ -60,6 +61,14 @@ use syntax::visit;
6061
use syntax;
6162
use syntax_ext;
6263

64+
#[derive(Clone)]
65+
pub struct Resolutions {
66+
pub def_map: RefCell<DefMap>,
67+
pub freevars: FreevarMap,
68+
pub trait_map: TraitMap,
69+
pub maybe_unused_trait_imports: NodeSet,
70+
}
71+
6372
pub fn compile_input(sess: &Session,
6473
cstore: &CStore,
6574
cfg: ast::CrateConfig,
@@ -139,15 +148,17 @@ pub fn compile_input(sess: &Session,
139148

140149
time(sess.time_passes(),
141150
"external crate/lib resolution",
142-
|| LocalCrateReader::new(sess, &cstore, &defs, &expanded_crate, &id)
151+
|| LocalCrateReader::new(sess, &cstore, defs, &expanded_crate, &id)
143152
.read_crates(&dep_graph));
144153

145-
// Lower ast -> hir.
146-
let lcx = LoweringContext::new(sess, Some(&expanded_crate), defs);
147-
let hir_forest = &mut time(sess.time_passes(),
148-
"lowering ast -> hir",
149-
|| hir_map::Forest::new(lower_crate(&lcx, &expanded_crate),
150-
dep_graph));
154+
time(sess.time_passes(),
155+
"early lint checks",
156+
|| lint::check_ast_crate(sess, &expanded_crate));
157+
158+
let (analysis, resolutions, mut hir_forest) = {
159+
let defs = &mut *defs.borrow_mut();
160+
lower_and_resolve(sess, &id, defs, &expanded_crate, dep_graph, control.make_glob_map)
161+
};
151162

152163
// Discard MTWT tables that aren't required past lowering to HIR.
153164
if !keep_mtwt_tables(sess) {
@@ -157,6 +168,7 @@ pub fn compile_input(sess: &Session,
157168
let arenas = ty::CtxtArenas::new();
158169

159170
// Construct the HIR map
171+
let hir_forest = &mut hir_forest;
160172
let hir_map = time(sess.time_passes(),
161173
"indexing hir",
162174
move || hir_map::map_crate(hir_forest, defs));
@@ -175,6 +187,8 @@ pub fn compile_input(sess: &Session,
175187
&arenas,
176188
&cstore,
177189
&hir_map,
190+
&analysis,
191+
&resolutions,
178192
&expanded_crate,
179193
&hir_map.krate(),
180194
&id),
@@ -185,10 +199,6 @@ pub fn compile_input(sess: &Session,
185199
hir::check_attr::check_crate(sess, &expanded_crate);
186200
});
187201

188-
time(sess.time_passes(),
189-
"early lint checks",
190-
|| lint::check_ast_crate(sess, &expanded_crate));
191-
192202
let opt_crate = if keep_ast(sess) {
193203
Some(&expanded_crate)
194204
} else {
@@ -198,9 +208,10 @@ pub fn compile_input(sess: &Session,
198208

199209
phase_3_run_analysis_passes(sess,
200210
hir_map,
211+
analysis,
212+
resolutions,
201213
&arenas,
202214
&id,
203-
control.make_glob_map,
204215
|tcx, mir_map, analysis, result| {
205216
{
206217
// Eventually, we will want to track plugins.
@@ -353,6 +364,7 @@ pub struct CompileState<'a, 'b, 'ast: 'a, 'tcx: 'b> where 'ast: 'tcx {
353364
pub expanded_crate: Option<&'a ast::Crate>,
354365
pub hir_crate: Option<&'a hir::Crate>,
355366
pub ast_map: Option<&'a hir_map::Map<'ast>>,
367+
pub resolutions: Option<&'a Resolutions>,
356368
pub mir_map: Option<&'b MirMap<'tcx>>,
357369
pub analysis: Option<&'a ty::CrateAnalysis<'a>>,
358370
pub tcx: Option<&'b TyCtxt<'tcx>>,
@@ -377,6 +389,7 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
377389
expanded_crate: None,
378390
hir_crate: None,
379391
ast_map: None,
392+
resolutions: None,
380393
analysis: None,
381394
mir_map: None,
382395
tcx: None,
@@ -423,6 +436,8 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
423436
arenas: &'ast ty::CtxtArenas<'ast>,
424437
cstore: &'a CStore,
425438
hir_map: &'a hir_map::Map<'ast>,
439+
analysis: &'a ty::CrateAnalysis,
440+
resolutions: &'a Resolutions,
426441
krate: &'a ast::Crate,
427442
hir_crate: &'a hir::Crate,
428443
crate_name: &'a str)
@@ -432,6 +447,8 @@ impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
432447
arenas: Some(arenas),
433448
cstore: Some(cstore),
434449
ast_map: Some(hir_map),
450+
analysis: Some(analysis),
451+
resolutions: Some(resolutions),
435452
expanded_crate: Some(krate),
436453
hir_crate: Some(hir_crate),
437454
out_file: out_file.as_ref().map(|s| &**s),
@@ -756,14 +773,48 @@ pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {
756773
krate
757774
}
758775

776+
pub fn lower_and_resolve<'a>(sess: &Session,
777+
id: &'a str,
778+
defs: &mut hir_map::Definitions,
779+
krate: &ast::Crate,
780+
dep_graph: DepGraph,
781+
make_glob_map: resolve::MakeGlobMap)
782+
-> (ty::CrateAnalysis<'a>, Resolutions, hir_map::Forest) {
783+
resolve::with_resolver(sess, defs, make_glob_map, |mut resolver| {
784+
time(sess.time_passes(), "name resolution", || {
785+
resolve::resolve_crate(&mut resolver, krate);
786+
});
787+
788+
// Lower ast -> hir.
789+
let hir_forest = time(sess.time_passes(), "lowering ast -> hir", || {
790+
let lcx = LoweringContext::new(sess, Some(krate), &mut resolver);
791+
hir_map::Forest::new(lower_crate(&lcx, krate), dep_graph)
792+
});
793+
794+
(ty::CrateAnalysis {
795+
export_map: resolver.export_map,
796+
access_levels: AccessLevels::default(),
797+
reachable: NodeSet(),
798+
name: &id,
799+
glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
800+
}, Resolutions {
801+
def_map: RefCell::new(resolver.def_map),
802+
freevars: resolver.freevars,
803+
trait_map: resolver.trait_map,
804+
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
805+
}, hir_forest)
806+
})
807+
}
808+
759809
/// Run the resolution, typechecking, region checking and other
760810
/// miscellaneous analysis passes on the crate. Return various
761811
/// structures carrying the results of the analysis.
762812
pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
763813
hir_map: hir_map::Map<'tcx>,
814+
mut analysis: ty::CrateAnalysis,
815+
resolutions: Resolutions,
764816
arenas: &'tcx ty::CtxtArenas<'tcx>,
765817
name: &str,
766-
make_glob_map: resolve::MakeGlobMap,
767818
f: F)
768819
-> Result<R, usize>
769820
where F: FnOnce(&TyCtxt<'tcx>, Option<MirMap<'tcx>>, ty::CrateAnalysis, CompileResult) -> R
@@ -788,30 +839,11 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
788839
})
789840
})?;
790841

791-
let resolve::CrateMap {
792-
def_map,
793-
freevars,
794-
maybe_unused_trait_imports,
795-
export_map,
796-
trait_map,
797-
glob_map,
798-
} = time(sess.time_passes(),
799-
"name resolution",
800-
|| resolve::resolve_crate(sess, &hir_map, make_glob_map));
801-
802-
let mut analysis = ty::CrateAnalysis {
803-
export_map: export_map,
804-
access_levels: AccessLevels::default(),
805-
reachable: NodeSet(),
806-
name: name,
807-
glob_map: glob_map,
808-
};
809-
810842
let named_region_map = time(time_passes,
811843
"lifetime resolution",
812844
|| middle::resolve_lifetime::krate(sess,
813845
&hir_map,
814-
&def_map.borrow()))?;
846+
&resolutions.def_map.borrow()))?;
815847

816848
time(time_passes,
817849
"looking for entry point",
@@ -831,17 +863,18 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
831863

832864
time(time_passes,
833865
"static item recursion checking",
834-
|| static_recursion::check_crate(sess, &def_map.borrow(), &hir_map))?;
866+
|| static_recursion::check_crate(sess, &resolutions.def_map.borrow(), &hir_map))?;
835867

836868
let index = stability::Index::new(&hir_map);
837869

870+
let trait_map = resolutions.trait_map;
838871
TyCtxt::create_and_enter(sess,
839872
arenas,
840-
def_map,
873+
resolutions.def_map,
841874
named_region_map,
842875
hir_map,
843-
freevars,
844-
maybe_unused_trait_imports,
876+
resolutions.freevars,
877+
resolutions.maybe_unused_trait_imports,
845878
region_map,
846879
lang_items,
847880
index,

src/librustc_driver/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
469469
control.after_write_deps.callback = box move |state| {
470470
pretty::print_after_write_deps(state.session,
471471
state.ast_map.unwrap(),
472+
state.analysis.unwrap(),
473+
state.resolutions.unwrap(),
472474
state.input,
473475
&state.expanded_crate.take().unwrap(),
474476
state.crate_name.unwrap(),

src/librustc_driver/pretty.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ pub use self::PpSourceMode::*;
1515
pub use self::PpMode::*;
1616
use self::NodesMatchingUII::*;
1717

18-
use {driver, abort_on_err};
18+
use abort_on_err;
19+
use driver::{self, Resolutions};
1920

2021
use rustc::dep_graph::DepGraph;
2122
use rustc::ty::{self, TyCtxt};
@@ -25,7 +26,6 @@ use rustc::session::Session;
2526
use rustc::session::config::Input;
2627
use rustc_borrowck as borrowck;
2728
use rustc_borrowck::graphviz as borrowck_dot;
28-
use rustc_resolve as resolve;
2929

3030
use rustc_mir::pretty::write_mir_pretty;
3131
use rustc_mir::graphviz::write_mir_graphviz;
@@ -202,6 +202,8 @@ impl PpSourceMode {
202202
fn call_with_pp_support_hir<'tcx, A, B, F>(&self,
203203
sess: &'tcx Session,
204204
ast_map: &hir_map::Map<'tcx>,
205+
analysis: &ty::CrateAnalysis,
206+
resolutions: &Resolutions,
205207
arenas: &'tcx ty::CtxtArenas<'tcx>,
206208
id: &str,
207209
payload: B,
@@ -228,9 +230,10 @@ impl PpSourceMode {
228230
PpmTyped => {
229231
abort_on_err(driver::phase_3_run_analysis_passes(sess,
230232
ast_map.clone(),
233+
analysis.clone(),
234+
resolutions.clone(),
231235
arenas,
232236
id,
233-
resolve::MakeGlobMap::No,
234237
|tcx, _, _, _| {
235238
let annotation = TypedAnnotation {
236239
tcx: tcx,
@@ -811,6 +814,8 @@ pub fn print_after_parsing(sess: &Session,
811814

812815
pub fn print_after_write_deps<'tcx, 'a: 'tcx>(sess: &'a Session,
813816
ast_map: &hir_map::Map<'tcx>,
817+
analysis: &ty::CrateAnalysis,
818+
resolutions: &Resolutions,
814819
input: &Input,
815820
krate: &ast::Crate,
816821
crate_name: &str,
@@ -822,7 +827,8 @@ pub fn print_after_write_deps<'tcx, 'a: 'tcx>(sess: &'a Session,
822827
let _ignore = dep_graph.in_ignore();
823828

824829
if ppm.needs_analysis() {
825-
print_with_analysis(sess, ast_map, crate_name, arenas, ppm, opt_uii, ofile);
830+
print_with_analysis(sess, ast_map, analysis, resolutions,
831+
crate_name, arenas, ppm, opt_uii, ofile);
826832
return;
827833
}
828834

@@ -853,6 +859,8 @@ pub fn print_after_write_deps<'tcx, 'a: 'tcx>(sess: &'a Session,
853859
let out: &mut Write = &mut out;
854860
s.call_with_pp_support_hir(sess,
855861
ast_map,
862+
analysis,
863+
resolutions,
856864
arenas,
857865
crate_name,
858866
box out,
@@ -874,6 +882,8 @@ pub fn print_after_write_deps<'tcx, 'a: 'tcx>(sess: &'a Session,
874882
let out: &mut Write = &mut out;
875883
s.call_with_pp_support_hir(sess,
876884
ast_map,
885+
analysis,
886+
resolutions,
877887
arenas,
878888
crate_name,
879889
(out,uii),
@@ -914,6 +924,8 @@ pub fn print_after_write_deps<'tcx, 'a: 'tcx>(sess: &'a Session,
914924
// Instead, we call that function ourselves.
915925
fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
916926
ast_map: &hir_map::Map<'tcx>,
927+
analysis: &ty::CrateAnalysis,
928+
resolutions: &Resolutions,
917929
crate_name: &str,
918930
arenas: &'tcx ty::CtxtArenas<'tcx>,
919931
ppm: PpMode,
@@ -931,9 +943,10 @@ fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
931943

932944
abort_on_err(driver::phase_3_run_analysis_passes(sess,
933945
ast_map.clone(),
946+
analysis.clone(),
947+
resolutions.clone(),
934948
arenas,
935949
crate_name,
936-
resolve::MakeGlobMap::No,
937950
|tcx, mir_map, _, _| {
938951
match ppm {
939952
PpmMir | PpmMirCFG => {

0 commit comments

Comments
 (0)