Skip to content

Commit 1ceaa86

Browse files
committed
Auto merge of #33997 - jseyfried:resolve_in_phase_2, r=nrc
Move name resolution into phase 2 r? @nrc
2 parents 9552bcd + 3fc0407 commit 1ceaa86

File tree

9 files changed

+87
-153
lines changed

9 files changed

+87
-153
lines changed

src/librustc_driver/driver.rs

+66-61
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]
@@ -747,10 +724,6 @@ pub fn phase_2_configure_and_expand(sess: &Session,
747724
"prelude injection",
748725
|| syntax::std_inject::maybe_inject_prelude(&sess.parse_sess, krate));
749726

750-
time(time_passes,
751-
"checking that all macro invocations are gone",
752-
|| syntax::ext::expand::check_for_macros(&sess.parse_sess, &krate));
753-
754727
time(time_passes,
755728
"checking for inline asm in case the target doesn't support it",
756729
|| no_asm::check_crate(sess, &krate));
@@ -771,7 +744,39 @@ pub fn phase_2_configure_and_expand(sess: &Session,
771744
println!("Post-expansion node count: {}", count_nodes(&krate));
772745
}
773746

774-
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+
})
775780
}
776781

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

src/librustc_driver/test.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc::traits::ProjectionMode;
2626
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
2727
use rustc::infer::{self, InferOk, InferResult, TypeOrigin};
2828
use rustc_metadata::cstore::CStore;
29-
use rustc_metadata::creader::read_local_crates;
3029
use rustc::hir::map as hir_map;
3130
use rustc::session::{self, config};
3231
use std::rc::Rc;
@@ -116,19 +115,11 @@ fn test_env<F>(source_string: &str,
116115
input: source_string.to_string(),
117116
};
118117
let krate = driver::phase_1_parse_input(&sess, krate_config, &input).unwrap();
119-
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
120-
.expect("phase 2 aborted");
121-
122-
let krate = driver::assign_node_ids(&sess, krate);
123-
let mut defs = hir_map::collect_definitions(&krate);
124-
read_local_crates(&sess, &cstore, &defs, &krate, "test_crate", &dep_graph);
118+
let driver::ExpansionResult { defs, resolutions, mut hir_forest, .. } =
119+
driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None, MakeGlobMap::No)
120+
.expect("phase 2 aborted");
125121
let _ignore = dep_graph.in_ignore();
126122

127-
let (_, resolutions, mut hir_forest) = {
128-
driver::lower_and_resolve(&sess, "test-crate", &mut defs, &krate,
129-
&sess.dep_graph, MakeGlobMap::No)
130-
};
131-
132123
let arenas = ty::CtxtArenas::new();
133124
let ast_map = hir_map::map_crate(&mut hir_forest, defs);
134125

src/librustdoc/core.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc::lint;
2121
use rustc_trans::back::link;
2222
use rustc_resolve as resolve;
2323
use rustc_metadata::cstore::CStore;
24-
use rustc_metadata::creader::read_local_crates;
2524

2625
use syntax::{ast, codemap, errors};
2726
use syntax::errors::emitter::ColorConfig;
@@ -146,21 +145,12 @@ pub fn run_core(search_paths: SearchPaths,
146145

147146
let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, &input));
148147

149-
let name = link::find_crate_name(Some(&sess), &krate.attrs,
150-
&input);
148+
let name = link::find_crate_name(Some(&sess), &krate.attrs, &input);
151149

152-
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, &name, None)
153-
.expect("phase_2_configure_and_expand aborted in rustdoc!");
154-
155-
let krate = driver::assign_node_ids(&sess, krate);
156-
157-
let mut defs = hir_map::collect_definitions(&krate);
158-
read_local_crates(&sess, &cstore, &defs, &krate, &name, &dep_graph);
159-
160-
// Lower ast -> hir and resolve.
161-
let (analysis, resolutions, mut hir_forest) = {
162-
driver::lower_and_resolve(&sess, &name, &mut defs, &krate,
163-
&sess.dep_graph, resolve::MakeGlobMap::No)
150+
let driver::ExpansionResult { defs, analysis, resolutions, mut hir_forest, .. } = {
151+
let make_glob_map = resolve::MakeGlobMap::No;
152+
driver::phase_2_configure_and_expand(&sess, &cstore, krate, &name, None, make_glob_map)
153+
.expect("phase_2_configure_and_expand aborted in rustdoc!")
164154
};
165155

166156
let arenas = ty::CtxtArenas::new();

src/librustdoc/test.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ use rustc::hir::map as hir_map;
2828
use rustc::session::{self, config};
2929
use rustc::session::config::{get_unstable_features_setting, OutputType};
3030
use rustc::session::search_paths::{SearchPaths, PathKind};
31-
use rustc::hir::lowering::{lower_crate, DummyResolver};
3231
use rustc_back::dynamic_lib::DynamicLibrary;
3332
use rustc_back::tempdir::TempDir;
3433
use rustc_driver::{driver, Compilation};
34+
use rustc_driver::driver::phase_2_configure_and_expand;
3535
use rustc_metadata::cstore::CStore;
36+
use rustc_resolve::MakeGlobMap;
3637
use syntax::codemap::CodeMap;
3738
use syntax::errors;
3839
use syntax::errors::emitter::ColorConfig;
@@ -93,21 +94,16 @@ pub fn run(input: &str,
9394
let mut cfg = config::build_configuration(&sess);
9495
cfg.extend(config::parse_cfgspecs(cfgs.clone()));
9596
let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, &input));
96-
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate,
97-
"rustdoc-test", None)
98-
.expect("phase_2_configure_and_expand aborted in rustdoc!");
99-
let krate = driver::assign_node_ids(&sess, krate);
100-
let dep_graph = DepGraph::new(false);
101-
let defs = hir_map::collect_definitions(&krate);
102-
103-
let mut dummy_resolver = DummyResolver;
104-
let krate = lower_crate(&sess, &krate, &sess, &mut dummy_resolver);
105-
106-
let opts = scrape_test_config(&krate);
97+
let driver::ExpansionResult { defs, mut hir_forest, .. } = {
98+
let make_glob_map = MakeGlobMap::No;
99+
phase_2_configure_and_expand(&sess, &cstore, krate, "rustdoc-test", None, make_glob_map)
100+
.expect("phase_2_configure_and_expand aborted in rustdoc!")
101+
};
107102

103+
let dep_graph = DepGraph::new(false);
104+
let opts = scrape_test_config(hir_forest.krate());
108105
let _ignore = dep_graph.in_ignore();
109-
let mut forest = hir_map::Forest::new(krate, &dep_graph);
110-
let map = hir_map::map_crate(&mut forest, defs);
106+
let map = hir_map::map_crate(&mut hir_forest, defs);
111107

112108
let ctx = core::DocContext {
113109
map: &map,

src/libsyntax/ext/expand.rs

-19
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use feature_gate::{self, Features};
2525
use fold;
2626
use fold::*;
2727
use util::move_map::MoveMap;
28-
use parse;
2928
use parse::token::{fresh_mark, fresh_name, intern, keywords};
3029
use ptr::P;
3130
use util::small_vector::SmallVector;
@@ -1212,24 +1211,6 @@ fn mark_tts(tts: &[TokenTree], m: Mrk) -> Vec<TokenTree> {
12121211
noop_fold_tts(tts, &mut Marker{mark:m, expn_id: None})
12131212
}
12141213

1215-
/// Check that there are no macro invocations left in the AST:
1216-
pub fn check_for_macros(sess: &parse::ParseSess, krate: &ast::Crate) {
1217-
visit::walk_crate(&mut MacroExterminator{sess:sess}, krate);
1218-
}
1219-
1220-
/// A visitor that ensures that no macro invocations remain in an AST.
1221-
struct MacroExterminator<'a>{
1222-
sess: &'a parse::ParseSess
1223-
}
1224-
1225-
impl<'a, 'v> Visitor<'v> for MacroExterminator<'a> {
1226-
fn visit_mac(&mut self, mac: &ast::Mac) {
1227-
self.sess.span_diagnostic.span_bug(mac.span,
1228-
"macro exterminator: expected AST \
1229-
with no macro invocations");
1230-
}
1231-
}
1232-
12331214

12341215
#[cfg(test)]
12351216
mod tests {

src/test/run-make/dep-info-no-analysis/Makefile

-6
This file was deleted.

src/test/run-make/dep-info-no-analysis/input.dd

-3
This file was deleted.

src/test/run-make/dep-info-no-analysis/input.rs

-14
This file was deleted.

src/test/run-make/execution-engine/test.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,9 @@ fn compile_program(input: &str, sysroot: PathBuf)
238238

239239
let krate = panictry!(driver::phase_1_parse_input(&sess, cfg, &input));
240240

241-
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, &id, None)
242-
.expect("phase_2 returned `None`");
243-
244-
let krate = driver::assign_node_ids(&sess, krate);
245-
let mut defs = ast_map::collect_definitions(&krate);
246-
read_local_crates(&sess, &cstore, &defs, &krate, &id, &dep_graph);
247-
let (analysis, resolutions, mut hir_forest) = {
248-
driver::lower_and_resolve(&sess, &id, &mut defs, &krate,
249-
&sess.dep_graph, MakeGlobMap::No)
241+
let driver::ExpansionResult { defs, analysis, resolutions, mut hir_forest, .. } = {
242+
driver::phase_2_configure_and_expand(&sess, &cstore, krate, &id, None, MakeGlobMap::No)
243+
.expect("phase_2 returned `None`")
250244
};
251245

252246
let arenas = ty::CtxtArenas::new();

0 commit comments

Comments
 (0)