Skip to content

Commit fd45e6e

Browse files
authored
Rollup merge of rust-lang#34459 - jseyfried:expansion_cleanup, r=nrc
Miscellaneous macro expansion cleanup and groundwork r? @nrc
2 parents 470c519 + e58963d commit fd45e6e

File tree

13 files changed

+121
-197
lines changed

13 files changed

+121
-197
lines changed

src/librustc/hir/lowering.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use session::Session;
5050
use std::collections::BTreeMap;
5151
use std::iter;
5252
use syntax::ast::*;
53+
use syntax::errors;
5354
use syntax::ptr::P;
5455
use syntax::codemap::{respan, Spanned};
5556
use syntax::parse::token;
@@ -60,7 +61,7 @@ use syntax_pos::Span;
6061
pub struct LoweringContext<'a> {
6162
crate_root: Option<&'static str>,
6263
// Use to assign ids to hir nodes that do not directly correspond to an ast node
63-
id_assigner: &'a NodeIdAssigner,
64+
sess: Option<&'a Session>,
6465
// As we walk the AST we must keep track of the current 'parent' def id (in
6566
// the form of a DefIndex) so that if we create a new node which introduces
6667
// a definition, then we can properly create the def id.
@@ -99,7 +100,6 @@ impl Resolver for DummyResolver {
99100

100101
pub fn lower_crate(sess: &Session,
101102
krate: &Crate,
102-
id_assigner: &NodeIdAssigner,
103103
resolver: &mut Resolver)
104104
-> hir::Crate {
105105
// We're constructing the HIR here; we don't care what we will
@@ -115,17 +115,17 @@ pub fn lower_crate(sess: &Session,
115115
} else {
116116
Some("std")
117117
},
118-
id_assigner: id_assigner,
118+
sess: Some(sess),
119119
parent_def: None,
120120
resolver: resolver,
121121
}.lower_crate(krate)
122122
}
123123

124124
impl<'a> LoweringContext<'a> {
125-
pub fn testing_context(id_assigner: &'a NodeIdAssigner, resolver: &'a mut Resolver) -> Self {
125+
pub fn testing_context(resolver: &'a mut Resolver) -> Self {
126126
LoweringContext {
127127
crate_root: None,
128-
id_assigner: id_assigner,
128+
sess: None,
129129
parent_def: None,
130130
resolver: resolver,
131131
}
@@ -161,7 +161,12 @@ impl<'a> LoweringContext<'a> {
161161
}
162162

163163
fn next_id(&self) -> NodeId {
164-
self.id_assigner.next_node_id()
164+
self.sess.map(Session::next_node_id).unwrap_or(0)
165+
}
166+
167+
fn diagnostic(&self) -> &errors::Handler {
168+
self.sess.map(Session::diagnostic)
169+
.unwrap_or_else(|| panic!("this lowerer cannot emit diagnostics"))
165170
}
166171

167172
fn str_to_ident(&self, s: &'static str) -> Name {
@@ -786,7 +791,7 @@ impl<'a> LoweringContext<'a> {
786791
if let Some(SelfKind::Explicit(..)) = sig.decl.get_self().map(|eself| eself.node) {
787792
match hir_sig.decl.get_self().map(|eself| eself.node) {
788793
Some(hir::SelfKind::Value(..)) | Some(hir::SelfKind::Region(..)) => {
789-
self.id_assigner.diagnostic().span_err(sig.decl.inputs[0].ty.span,
794+
self.diagnostic().span_err(sig.decl.inputs[0].ty.span,
790795
"the type placeholder `_` is not allowed within types on item signatures");
791796
}
792797
_ => {}
@@ -1212,7 +1217,7 @@ impl<'a> LoweringContext<'a> {
12121217
make_struct(self, e, &["RangeInclusive", "NonEmpty"],
12131218
&[("start", e1), ("end", e2)]),
12141219

1215-
_ => panic!(self.id_assigner.diagnostic()
1220+
_ => panic!(self.diagnostic()
12161221
.span_fatal(e.span, "inclusive range with no end")),
12171222
};
12181223
}

src/librustc/hir/map/def_collector.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ pub struct DefCollector<'ast> {
2525
// If we are walking HIR (c.f., AST), we need to keep a reference to the
2626
// crate.
2727
hir_crate: Option<&'ast hir::Crate>,
28-
pub definitions: Definitions,
28+
definitions: &'ast mut Definitions,
2929
parent_def: Option<DefIndex>,
3030
}
3131

3232
impl<'ast> DefCollector<'ast> {
33-
pub fn root() -> DefCollector<'ast> {
33+
pub fn root(definitions: &'ast mut Definitions) -> DefCollector<'ast> {
3434
let mut collector = DefCollector {
3535
hir_crate: None,
36-
definitions: Definitions::new(),
36+
definitions: definitions,
3737
parent_def: None,
3838
};
3939
let root = collector.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot);
@@ -48,7 +48,7 @@ impl<'ast> DefCollector<'ast> {
4848
pub fn extend(parent_node: NodeId,
4949
parent_def_path: DefPath,
5050
parent_def_id: DefId,
51-
definitions: Definitions)
51+
definitions: &'ast mut Definitions)
5252
-> DefCollector<'ast> {
5353
let mut collector = DefCollector {
5454
hir_crate: None,

src/librustc/hir/map/definitions.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
use middle::cstore::LOCAL_CRATE;
1212
use hir::def_id::{DefId, DefIndex};
13+
use hir::map::def_collector::DefCollector;
1314
use rustc_data_structures::fnv::FnvHashMap;
14-
use syntax::ast;
15+
use syntax::{ast, visit};
1516
use syntax::parse::token::InternedString;
1617
use util::nodemap::NodeMap;
1718

@@ -189,6 +190,11 @@ impl Definitions {
189190
}
190191
}
191192

193+
pub fn collect(&mut self, krate: &ast::Crate) {
194+
let mut def_collector = DefCollector::root(self);
195+
visit::walk_crate(&mut def_collector, krate);
196+
}
197+
192198
/// Get the number of definitions.
193199
pub fn len(&self) -> usize {
194200
self.data.len()

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
2424
use syntax::abi::Abi;
2525
use syntax::ast::{self, Name, NodeId, DUMMY_NODE_ID, };
2626
use syntax::codemap::Spanned;
27-
use syntax::visit;
2827
use syntax_pos::Span;
2928

3029
use hir::*;
@@ -780,12 +779,6 @@ impl<F: FoldOps> Folder for IdAndSpanUpdater<F> {
780779
}
781780
}
782781

783-
pub fn collect_definitions<'ast>(krate: &'ast ast::Crate) -> Definitions {
784-
let mut def_collector = DefCollector::root();
785-
visit::walk_crate(&mut def_collector, krate);
786-
def_collector.definitions
787-
}
788-
789782
pub fn map_crate<'ast>(forest: &'ast mut Forest,
790783
definitions: Definitions)
791784
-> Map<'ast> {
@@ -842,13 +835,12 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
842835
let ii = map.forest.inlined_items.alloc(ii);
843836
let ii_parent_id = fld.new_id(DUMMY_NODE_ID);
844837

845-
let defs = mem::replace(&mut *map.definitions.borrow_mut(), Definitions::new());
838+
let defs = &mut *map.definitions.borrow_mut();
846839
let mut def_collector = DefCollector::extend(ii_parent_id,
847840
parent_def_path.clone(),
848841
parent_def_id,
849842
defs);
850843
def_collector.walk_item(ii, map.krate());
851-
*map.definitions.borrow_mut() = def_collector.definitions;
852844

853845
let mut collector = NodeCollector::extend(map.krate(),
854846
ii,

src/librustc/session/mod.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ty::tls;
2020
use util::nodemap::{NodeMap, FnvHashMap};
2121
use mir::transform as mir_pass;
2222

23-
use syntax::ast::{NodeId, NodeIdAssigner, Name};
23+
use syntax::ast::{NodeId, Name};
2424
use errors::{self, DiagnosticBuilder};
2525
use errors::emitter::{Emitter, BasicEmitter, EmitterWriter};
2626
use syntax::json::JsonEmitter;
@@ -272,6 +272,9 @@ impl Session {
272272

273273
id
274274
}
275+
pub fn next_node_id(&self) -> NodeId {
276+
self.reserve_node_ids(1)
277+
}
275278
pub fn diagnostic<'a>(&'a self) -> &'a errors::Handler {
276279
&self.parse_sess.span_diagnostic
277280
}
@@ -345,20 +348,6 @@ impl Session {
345348
}
346349
}
347350

348-
impl NodeIdAssigner for Session {
349-
fn next_node_id(&self) -> NodeId {
350-
self.reserve_node_ids(1)
351-
}
352-
353-
fn peek_node_id(&self) -> NodeId {
354-
self.next_node_id.get().checked_add(1).unwrap()
355-
}
356-
357-
fn diagnostic(&self) -> &errors::Handler {
358-
self.diagnostic()
359-
}
360-
}
361-
362351
fn split_msg_into_multilines(msg: &str) -> Option<String> {
363352
// Conditions for enabling multi-line errors:
364353
if !msg.contains("mismatched types") &&

src/librustc_driver/driver.rs

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use rustc::dep_graph::DepGraph;
1211
use rustc::hir;
1312
use rustc::hir::{map as hir_map, FreevarMap, TraitMap};
1413
use rustc::hir::def::DefMap;
@@ -27,7 +26,7 @@ use rustc::util::nodemap::NodeSet;
2726
use rustc_back::sha2::{Sha256, Digest};
2827
use rustc_borrowck as borrowck;
2928
use rustc_incremental;
30-
use rustc_resolve as resolve;
29+
use rustc_resolve::{MakeGlobMap, Resolver};
3130
use rustc_metadata::macro_import;
3231
use rustc_metadata::creader::read_local_crates;
3332
use rustc_metadata::cstore::CStore;
@@ -49,13 +48,11 @@ use std::ffi::{OsString, OsStr};
4948
use std::fs;
5049
use std::io::{self, Write};
5150
use std::path::{Path, PathBuf};
52-
use syntax::ast::{self, NodeIdAssigner};
51+
use syntax::{ast, diagnostics, visit};
5352
use syntax::attr::{self, AttrMetaMethods};
54-
use syntax::diagnostics;
5553
use syntax::fold::Folder;
5654
use syntax::parse::{self, PResult, token};
5755
use syntax::util::node_count::NodeCounter;
58-
use syntax::visit;
5956
use syntax;
6057
use syntax_ext;
6158

@@ -293,7 +290,7 @@ pub struct CompileController<'a> {
293290
pub after_analysis: PhaseController<'a>,
294291
pub after_llvm: PhaseController<'a>,
295292

296-
pub make_glob_map: resolve::MakeGlobMap,
293+
pub make_glob_map: MakeGlobMap,
297294
}
298295

299296
impl<'a> CompileController<'a> {
@@ -305,7 +302,7 @@ impl<'a> CompileController<'a> {
305302
after_hir_lowering: PhaseController::basic(),
306303
after_analysis: PhaseController::basic(),
307304
after_llvm: PhaseController::basic(),
308-
make_glob_map: resolve::MakeGlobMap::No,
305+
make_glob_map: MakeGlobMap::No,
309306
}
310307
}
311308
}
@@ -564,7 +561,7 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session,
564561
mut krate: ast::Crate,
565562
crate_name: &'a str,
566563
addl_plugins: Option<Vec<String>>,
567-
make_glob_map: resolve::MakeGlobMap)
564+
make_glob_map: MakeGlobMap)
568565
-> Result<ExpansionResult<'a>, usize> {
569566
let time_passes = sess.time_passes();
570567

@@ -729,13 +726,16 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session,
729726

730727
krate = assign_node_ids(sess, krate);
731728

729+
let resolver_arenas = Resolver::arenas();
730+
let mut resolver = Resolver::new(sess, make_glob_map, &resolver_arenas);
731+
732732
// Collect defintions for def ids.
733-
let mut defs =
734-
time(sess.time_passes(), "collecting defs", || hir_map::collect_definitions(&krate));
733+
time(sess.time_passes(), "collecting defs", || resolver.definitions.collect(&krate));
735734

736-
time(sess.time_passes(),
737-
"external crate/lib resolution",
738-
|| read_local_crates(sess, &cstore, &defs, &krate, crate_name, &sess.dep_graph));
735+
time(sess.time_passes(), "external crate/lib resolution", || {
736+
let defs = &resolver.definitions;
737+
read_local_crates(sess, &cstore, defs, &krate, crate_name, &sess.dep_graph)
738+
});
739739

740740
time(sess.time_passes(),
741741
"early lint checks",
@@ -745,8 +745,14 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session,
745745
"AST validation",
746746
|| ast_validation::check_crate(sess, &krate));
747747

748-
let (analysis, resolutions, hir_forest) =
749-
lower_and_resolve(sess, crate_name, &mut defs, &krate, &sess.dep_graph, make_glob_map);
748+
time(sess.time_passes(), "name resolution", || {
749+
resolver.resolve_crate(&krate);
750+
});
751+
752+
// Lower ast -> hir.
753+
let hir_forest = time(sess.time_passes(), "lowering ast -> hir", || {
754+
hir_map::Forest::new(lower_crate(sess, &krate, &mut resolver), &sess.dep_graph)
755+
});
750756

751757
// Discard MTWT tables that aren't required past lowering to HIR.
752758
if !keep_mtwt_tables(sess) {
@@ -755,9 +761,20 @@ pub fn phase_2_configure_and_expand<'a>(sess: &Session,
755761

756762
Ok(ExpansionResult {
757763
expanded_crate: krate,
758-
defs: defs,
759-
analysis: analysis,
760-
resolutions: resolutions,
764+
defs: resolver.definitions,
765+
analysis: ty::CrateAnalysis {
766+
export_map: resolver.export_map,
767+
access_levels: AccessLevels::default(),
768+
reachable: NodeSet(),
769+
name: crate_name,
770+
glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
771+
},
772+
resolutions: Resolutions {
773+
def_map: resolver.def_map,
774+
freevars: resolver.freevars,
775+
trait_map: resolver.trait_map,
776+
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
777+
},
761778
hir_forest: hir_forest
762779
})
763780
}
@@ -809,38 +826,6 @@ pub fn assign_node_ids(sess: &Session, krate: ast::Crate) -> ast::Crate {
809826
krate
810827
}
811828

812-
pub fn lower_and_resolve<'a>(sess: &Session,
813-
id: &'a str,
814-
defs: &mut hir_map::Definitions,
815-
krate: &ast::Crate,
816-
dep_graph: &DepGraph,
817-
make_glob_map: resolve::MakeGlobMap)
818-
-> (ty::CrateAnalysis<'a>, Resolutions, hir_map::Forest) {
819-
resolve::with_resolver(sess, defs, make_glob_map, |mut resolver| {
820-
time(sess.time_passes(), "name resolution", || {
821-
resolve::resolve_crate(&mut resolver, krate);
822-
});
823-
824-
// Lower ast -> hir.
825-
let hir_forest = time(sess.time_passes(), "lowering ast -> hir", || {
826-
hir_map::Forest::new(lower_crate(sess, krate, sess, &mut resolver), dep_graph)
827-
});
828-
829-
(ty::CrateAnalysis {
830-
export_map: resolver.export_map,
831-
access_levels: AccessLevels::default(),
832-
reachable: NodeSet(),
833-
name: &id,
834-
glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
835-
}, Resolutions {
836-
def_map: resolver.def_map,
837-
freevars: resolver.freevars,
838-
trait_map: resolver.trait_map,
839-
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
840-
}, hir_forest)
841-
})
842-
}
843-
844829
/// Run the resolution, typechecking, region checking and other
845830
/// miscellaneous analysis passes on the crate. Return various
846831
/// structures carrying the results of the analysis.

0 commit comments

Comments
 (0)