Skip to content

Commit bab9238

Browse files
authored
Rollup merge of rust-lang#36438 - jseyfried:node_ids_in_expansion, r=nrc
Assign node ids during macro expansion After this PR, - The `ExtCtxt` can access `resolve`'s `Resolver` through the trait object `ext::base::Resolver`. - The `Resolver` trait object can load macros and replaces today's `MacroLoader` trait object. - The macro expander uses the `Resolver` trait object to resolve macro invocations. - The macro expander assigns node ids and builds the `Resolver`'s `macros_at_scope` map. - This is groundwork for merging import resolution and expansion. - Performance of expansion together with node id assignment improves by ~5%. **EDIT:** Since Github is reordering the commits, here is `git log`: - b54e1e3: Differentiate between monotonic and non-monotonic expansion and only assign node ids during monotonic expansion. - 78c0039: Expand generated test harnesses and macro registries. - f3c2dca: Remove scope placeholders from the crate root. - c86c8d4: Perform node id assignment and `macros_at_scope` construction during the `InvocationCollector` and `PlaceholderExpander` folds. - 72a6369: Move macro resolution into `librustc_resolve`. - 20b43b2: Rewrite the unit tests in `ext/expand.rs` as a `compile-fail` test. - a9821e1: Refactor `ExtCtxt` to use a `Resolver` instead of a `MacroLoader`. - 60440b2: Refactor `noop_fold_stmt_kind` out of `noop_fold_stmt`. - 50f94f6: Avoid needless reexpansions. r? @nrc
2 parents 23e0c24 + b54e1e3 commit bab9238

File tree

23 files changed

+696
-722
lines changed

23 files changed

+696
-722
lines changed

src/librustc/middle/cstore.rs

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use std::rc::Rc;
3939
use std::path::PathBuf;
4040
use syntax::ast;
4141
use syntax::attr;
42+
use syntax::ext::base::LoadedMacro;
4243
use syntax::ptr::P;
4344
use syntax::parse::token::InternedString;
4445
use syntax_pos::Span;
@@ -492,6 +493,9 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
492493
fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
493494
}
494495

496+
pub trait MacroLoader {
497+
fn load_crate(&mut self, extern_crate: &ast::Item, allows_macros: bool) -> Vec<LoadedMacro>;
498+
}
495499

496500
/// Metadata encoding and decoding can make use of thread-local encoding and
497501
/// decoding contexts. These allow implementers of serialize::Encodable and

src/librustc/session/mod.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use util::nodemap::{NodeMap, FnvHashMap};
2121
use util::common::duration_to_secs_str;
2222
use mir::transform as mir_pass;
2323

24-
use syntax::ast::{NodeId, Name};
24+
use syntax::ast::NodeId;
2525
use errors::{self, DiagnosticBuilder};
2626
use errors::emitter::{Emitter, EmitterWriter};
2727
use syntax::json::JsonEmitter;
@@ -39,7 +39,7 @@ use llvm;
3939

4040
use std::path::{Path, PathBuf};
4141
use std::cell::{self, Cell, RefCell};
42-
use std::collections::{HashMap, HashSet};
42+
use std::collections::HashMap;
4343
use std::env;
4444
use std::ffi::CString;
4545
use std::rc::Rc;
@@ -96,10 +96,6 @@ pub struct Session {
9696
pub injected_allocator: Cell<Option<ast::CrateNum>>,
9797
pub injected_panic_runtime: Cell<Option<ast::CrateNum>>,
9898

99-
/// Names of all bang-style macros and syntax extensions
100-
/// available in this crate
101-
pub available_macros: RefCell<HashSet<Name>>,
102-
10399
/// Map from imported macro spans (which consist of
104100
/// the localized span for the macro body) to the
105101
/// macro name and defintion span in the source crate.
@@ -552,7 +548,6 @@ pub fn build_session_(sopts: config::Options,
552548
next_node_id: Cell::new(1),
553549
injected_allocator: Cell::new(None),
554550
injected_panic_runtime: Cell::new(None),
555-
available_macros: RefCell::new(HashSet::new()),
556551
imported_macro_spans: RefCell::new(HashMap::new()),
557552
incr_comp_session: RefCell::new(IncrCompSession::NotInitialized),
558553
perf_stats: PerfStats {

src/librustc_driver/driver.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use std::io::{self, Write};
5050
use std::path::{Path, PathBuf};
5151
use syntax::{ast, diagnostics, visit};
5252
use syntax::attr;
53+
use syntax::ext::base::ExtCtxt;
5354
use syntax::parse::{self, PResult, token};
5455
use syntax::util::node_count::NodeCounter;
5556
use syntax;
@@ -638,6 +639,13 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
638639
}
639640
sess.track_errors(|| sess.lint_store.borrow_mut().process_command_line(sess))?;
640641

642+
let mut macro_loader =
643+
macro_import::MacroLoader::new(sess, &cstore, crate_name, krate.config.clone());
644+
645+
let resolver_arenas = Resolver::arenas();
646+
let mut resolver = Resolver::new(sess, make_glob_map, &mut macro_loader, &resolver_arenas);
647+
syntax_ext::register_builtins(&mut resolver, sess.features.borrow().quote);
648+
641649
krate = time(time_passes, "expansion", || {
642650
// Windows dlls do not have rpaths, so they don't know how to find their
643651
// dependencies. It's up to us to tell the system where to find all the
@@ -672,25 +680,17 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
672680
trace_mac: sess.opts.debugging_opts.trace_macros,
673681
should_test: sess.opts.test,
674682
};
675-
let mut loader = macro_import::MacroLoader::new(sess,
676-
&cstore,
677-
crate_name,
678-
krate.config.clone());
679-
let mut ecx = syntax::ext::base::ExtCtxt::new(&sess.parse_sess,
680-
krate.config.clone(),
681-
cfg,
682-
&mut loader);
683-
syntax_ext::register_builtins(&mut ecx.syntax_env);
683+
let mut ecx = ExtCtxt::new(&sess.parse_sess, krate.config.clone(), cfg, &mut resolver);
684684
let ret = syntax::ext::expand::expand_crate(&mut ecx, syntax_exts, krate);
685685
if cfg!(windows) {
686686
env::set_var("PATH", &old_path);
687687
}
688-
*sess.available_macros.borrow_mut() = ecx.syntax_env.names;
689688
ret
690689
});
691690

692691
krate = time(time_passes, "maybe building test harness", || {
693692
syntax::test::modify_for_testing(&sess.parse_sess,
693+
&mut resolver,
694694
sess.opts.test,
695695
krate,
696696
sess.diagnostic())
@@ -701,18 +701,14 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
701701
let is_rustc_macro_crate = crate_types.contains(&config::CrateTypeRustcMacro);
702702
let num_crate_types = crate_types.len();
703703
syntax_ext::rustc_macro_registrar::modify(&sess.parse_sess,
704+
&mut resolver,
704705
krate,
705706
is_rustc_macro_crate,
706707
num_crate_types,
707708
sess.diagnostic(),
708709
&sess.features.borrow())
709710
});
710711

711-
let resolver_arenas = Resolver::arenas();
712-
let mut resolver = Resolver::new(sess, make_glob_map, &resolver_arenas);
713-
714-
let krate = time(sess.time_passes(), "assigning node ids", || resolver.assign_node_ids(krate));
715-
716712
if sess.opts.debugging_opts.input_stats {
717713
println!("Post-expansion node count: {}", count_nodes(&krate));
718714
}

src/librustc_metadata/macro_import.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use creader::{CrateReader, Macros};
1818
use cstore::CStore;
1919

2020
use rustc::hir::def_id::DefIndex;
21+
use rustc::middle;
2122
use rustc::session::Session;
2223
use rustc::util::nodemap::FnvHashMap;
2324
use rustc_back::dynamic_lib::DynamicLibrary;
@@ -26,7 +27,6 @@ use rustc_macro::__internal::Registry;
2627
use syntax::ast;
2728
use syntax::attr;
2829
use syntax::ext::base::LoadedMacro;
29-
use syntax::ext;
3030
use syntax::parse::token;
3131
use syntax_ext::deriving::custom::CustomDerive;
3232
use syntax_pos::Span;
@@ -55,7 +55,7 @@ pub fn call_bad_macro_reexport(a: &Session, b: Span) {
5555

5656
pub type MacroSelection = FnvHashMap<token::InternedString, Span>;
5757

58-
impl<'a> ext::base::MacroLoader for MacroLoader<'a> {
58+
impl<'a> middle::cstore::MacroLoader for MacroLoader<'a> {
5959
fn load_crate(&mut self,
6060
extern_crate: &ast::Item,
6161
allows_macros: bool) -> Vec<LoadedMacro> {

src/librustc_resolve/assign_ids.rs

-92
This file was deleted.

src/librustc_resolve/lib.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use self::ParentLink::*;
4545

4646
use rustc::hir::map::Definitions;
4747
use rustc::hir::{self, PrimTy, TyBool, TyChar, TyFloat, TyInt, TyUint, TyStr};
48+
use rustc::middle::cstore::MacroLoader;
4849
use rustc::session::Session;
4950
use rustc::lint;
5051
use rustc::hir::def::*;
@@ -79,10 +80,10 @@ use resolve_imports::{ImportDirective, NameResolution};
7980
// registered before they are used.
8081
mod diagnostics;
8182

83+
mod macros;
8284
mod check_unused;
8385
mod build_reduced_graph;
8486
mod resolve_imports;
85-
mod assign_ids;
8687

8788
enum SuggestionType {
8889
Macro(String),
@@ -1068,6 +1069,12 @@ pub struct Resolver<'a> {
10681069
arenas: &'a ResolverArenas<'a>,
10691070
dummy_binding: &'a NameBinding<'a>,
10701071
new_import_semantics: bool, // true if `#![feature(item_like_imports)]`
1072+
1073+
macro_loader: &'a mut MacroLoader,
1074+
macro_names: FnvHashSet<Name>,
1075+
1076+
// Maps the `Mark` of an expansion to its containing module or block.
1077+
expansion_data: Vec<macros::ExpansionData>,
10711078
}
10721079

10731080
pub struct ResolverArenas<'a> {
@@ -1166,7 +1173,10 @@ impl Named for hir::PathSegment {
11661173
}
11671174

11681175
impl<'a> Resolver<'a> {
1169-
pub fn new(session: &'a Session, make_glob_map: MakeGlobMap, arenas: &'a ResolverArenas<'a>)
1176+
pub fn new(session: &'a Session,
1177+
make_glob_map: MakeGlobMap,
1178+
macro_loader: &'a mut MacroLoader,
1179+
arenas: &'a ResolverArenas<'a>)
11701180
-> Resolver<'a> {
11711181
let root_def_id = DefId::local(CRATE_DEF_INDEX);
11721182
let graph_root =
@@ -1227,6 +1237,10 @@ impl<'a> Resolver<'a> {
12271237
vis: ty::Visibility::Public,
12281238
}),
12291239
new_import_semantics: session.features.borrow().item_like_imports,
1240+
1241+
macro_loader: macro_loader,
1242+
macro_names: FnvHashSet(),
1243+
expansion_data: vec![macros::ExpansionData::default()],
12301244
}
12311245
}
12321246

@@ -2768,8 +2782,7 @@ impl<'a> Resolver<'a> {
27682782
}
27692783

27702784
fn find_best_match(&mut self, name: &str) -> SuggestionType {
2771-
if let Some(macro_name) = self.session.available_macros
2772-
.borrow().iter().find(|n| n.as_str() == name) {
2785+
if let Some(macro_name) = self.macro_names.iter().find(|n| n.as_str() == name) {
27732786
return SuggestionType::Macro(format!("{}!", macro_name));
27742787
}
27752788

0 commit comments

Comments
 (0)