Skip to content

Commit c52968a

Browse files
committed
Move name resolution to phase 2
1 parent 58d7e1b commit c52968a

File tree

1 file changed

+66
-57
lines changed

1 file changed

+66
-57
lines changed

src/librustc_driver/driver.rs

+66-57
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ pub fn compile_input(sess: &Session,
9494
// large chunks of memory alive and we want to free them as soon as
9595
// possible to keep the peak memory usage low
9696
let (outputs, trans) = {
97-
let (outputs, expanded_crate, id) = {
98-
let krate = match phase_1_parse_input(sess, cfg, input) {
99-
Ok(krate) => krate,
100-
Err(mut parse_error) => {
101-
parse_error.emit();
102-
return Err(1);
103-
}
104-
};
97+
let krate = match phase_1_parse_input(sess, cfg, input) {
98+
Ok(krate) => krate,
99+
Err(mut parse_error) => {
100+
parse_error.emit();
101+
return Err(1);
102+
}
103+
};
105104

105+
let krate = {
106106
let mut compile_state = CompileState::state_after_parse(input,
107107
sess,
108108
outdir,
@@ -113,17 +113,15 @@ pub fn compile_input(sess: &Session,
113113
sess,
114114
compile_state,
115115
Ok(()));
116-
let krate = compile_state.krate.unwrap();
117116

118-
let outputs = build_output_filenames(input, outdir, output, &krate.attrs, sess);
119-
let id = link::find_crate_name(Some(sess), &krate.attrs, input);
120-
let expanded_crate = phase_2_configure_and_expand(sess,
121-
&cstore,
122-
krate,
123-
&id,
124-
addl_plugins)?;
117+
compile_state.krate.unwrap()
118+
};
125119

126-
(outputs, expanded_crate, id)
120+
let outputs = build_output_filenames(input, outdir, output, &krate.attrs, sess);
121+
let id = link::find_crate_name(Some(sess), &krate.attrs, input);
122+
let ExpansionResult { expanded_crate, defs, analysis, resolutions, mut hir_forest } = {
123+
let make_glob_map = control.make_glob_map;
124+
phase_2_configure_and_expand(sess, &cstore, krate, &id, addl_plugins, make_glob_map)?
127125
};
128126

129127
controller_entry_point!(after_expand,
@@ -150,42 +148,12 @@ pub fn compile_input(sess: &Session,
150148
&id),
151149
Ok(()));
152150

153-
let expanded_crate = assign_node_ids(sess, expanded_crate);
154-
155-
// Collect defintions for def ids.
156-
let mut defs = time(sess.time_passes(),
157-
"collecting defs",
158-
|| hir_map::collect_definitions(&expanded_crate));
159-
160-
time(sess.time_passes(),
161-
"external crate/lib resolution",
162-
|| read_local_crates(sess, &cstore, &defs, &expanded_crate, &id, &sess.dep_graph));
163-
164-
time(sess.time_passes(),
165-
"early lint checks",
166-
|| lint::check_ast_crate(sess, &expanded_crate));
167-
168-
time(sess.time_passes(),
169-
"AST validation",
170-
|| ast_validation::check_crate(sess, &expanded_crate));
171-
172-
let (analysis, resolutions, mut hir_forest) = {
173-
lower_and_resolve(sess, &id, &mut defs, &expanded_crate,
174-
&sess.dep_graph, control.make_glob_map)
175-
};
176-
177-
// Discard MTWT tables that aren't required past lowering to HIR.
178-
if !keep_mtwt_tables(sess) {
179-
syntax::ext::mtwt::clear_tables();
180-
}
181-
182151
let arenas = ty::CtxtArenas::new();
183152

184153
// Construct the HIR map
185-
let hir_forest = &mut hir_forest;
186154
let hir_map = time(sess.time_passes(),
187155
"indexing hir",
188-
move || hir_map::map_crate(hir_forest, defs));
156+
|| hir_map::map_crate(&mut hir_forest, defs));
189157

190158
{
191159
let _ignore = hir_map.dep_graph.in_ignore();
@@ -577,19 +545,28 @@ fn count_nodes(krate: &ast::Crate) -> usize {
577545
// For continuing compilation after a parsed crate has been
578546
// modified
579547

548+
pub struct ExpansionResult<'a> {
549+
pub expanded_crate: ast::Crate,
550+
pub defs: hir_map::Definitions,
551+
pub analysis: ty::CrateAnalysis<'a>,
552+
pub resolutions: Resolutions,
553+
pub hir_forest: hir_map::Forest,
554+
}
555+
580556
/// Run the "early phases" of the compiler: initial `cfg` processing,
581557
/// loading compiler plugins (including those from `addl_plugins`),
582558
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
583-
/// harness if one is to be provided and injection of a dependency on the
584-
/// standard library and prelude.
559+
/// harness if one is to be provided, injection of a dependency on the
560+
/// standard library and prelude, and name resolution.
585561
///
586562
/// Returns `None` if we're aborting after handling -W help.
587-
pub fn phase_2_configure_and_expand(sess: &Session,
588-
cstore: &CStore,
589-
mut krate: ast::Crate,
590-
crate_name: &str,
591-
addl_plugins: Option<Vec<String>>)
592-
-> Result<ast::Crate, usize> {
563+
pub fn phase_2_configure_and_expand<'a>(sess: &Session,
564+
cstore: &CStore,
565+
mut krate: ast::Crate,
566+
crate_name: &'a str,
567+
addl_plugins: Option<Vec<String>>,
568+
make_glob_map: resolve::MakeGlobMap)
569+
-> Result<ExpansionResult<'a>, usize> {
593570
let time_passes = sess.time_passes();
594571

595572
// strip before anything else because crate metadata may use #[cfg_attr]
@@ -767,7 +744,39 @@ pub fn phase_2_configure_and_expand(sess: &Session,
767744
println!("Post-expansion node count: {}", count_nodes(&krate));
768745
}
769746

770-
Ok(krate)
747+
krate = assign_node_ids(sess, krate);
748+
749+
// Collect defintions for def ids.
750+
let mut defs =
751+
time(sess.time_passes(), "collecting defs", || hir_map::collect_definitions(&krate));
752+
753+
time(sess.time_passes(),
754+
"external crate/lib resolution",
755+
|| read_local_crates(sess, &cstore, &defs, &krate, crate_name, &sess.dep_graph));
756+
757+
time(sess.time_passes(),
758+
"early lint checks",
759+
|| lint::check_ast_crate(sess, &krate));
760+
761+
time(sess.time_passes(),
762+
"AST validation",
763+
|| ast_validation::check_crate(sess, &krate));
764+
765+
let (analysis, resolutions, hir_forest) =
766+
lower_and_resolve(sess, crate_name, &mut defs, &krate, &sess.dep_graph, make_glob_map);
767+
768+
// Discard MTWT tables that aren't required past lowering to HIR.
769+
if !keep_mtwt_tables(sess) {
770+
syntax::ext::mtwt::clear_tables();
771+
}
772+
773+
Ok(ExpansionResult {
774+
expanded_crate: krate,
775+
defs: defs,
776+
analysis: analysis,
777+
resolutions: resolutions,
778+
hir_forest: hir_forest
779+
})
771780
}
772781

773782
pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {

0 commit comments

Comments
 (0)