Skip to content

Commit 21b4369

Browse files
committed
Refactor away ext::expand::{expand_crate, expand_crate_with_expander}.
1 parent 09e41b6 commit 21b4369

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

src/librustc_driver/driver.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,11 +686,17 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
686686
..syntax::ext::expand::ExpansionConfig::default(crate_name.to_string())
687687
};
688688
let mut ecx = ExtCtxt::new(&sess.parse_sess, krate.config.clone(), cfg, &mut resolver);
689-
let ret = syntax::ext::expand::expand_crate(&mut ecx, krate);
689+
let err_count = ecx.parse_sess.span_diagnostic.err_count();
690+
691+
let krate = ecx.monotonic_expander().expand_crate(krate);
692+
693+
if ecx.parse_sess.span_diagnostic.err_count() - ecx.resolve_err_count > err_count {
694+
ecx.parse_sess.span_diagnostic.abort_if_errors();
695+
}
690696
if cfg!(windows) {
691697
env::set_var("PATH", &old_path);
692698
}
693-
ret
699+
krate
694700
});
695701

696702
krate.exported_macros = mem::replace(&mut resolver.exported_macros, Vec::new());

src/libsyntax/ext/base.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use parse::{self, parser};
2222
use parse::token;
2323
use parse::token::{InternedString, str_to_ident};
2424
use ptr::P;
25-
use std_inject;
2625
use util::small_vector::SmallVector;
2726

2827
use std::path::PathBuf;
@@ -737,17 +736,6 @@ impl<'a> ExtCtxt<'a> {
737736
pub fn name_of(&self, st: &str) -> ast::Name {
738737
token::intern(st)
739738
}
740-
741-
pub fn initialize(&mut self, krate: &ast::Crate) {
742-
self.crate_root = std_inject::injected_crate_name(krate);
743-
744-
let mut module = ModuleData {
745-
mod_path: vec![token::str_to_ident(&self.ecfg.crate_name)],
746-
directory: PathBuf::from(self.parse_sess.codemap().span_to_filename(krate.span)),
747-
};
748-
module.directory.pop();
749-
self.current_expansion.module = Rc::new(module);
750-
}
751739
}
752740

753741
/// Extract a string literal from the macro expanded version of `expr`,

src/libsyntax/ext/expand.rs

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

11-
use ast::{Block, Crate, Ident, Mac_, PatKind};
11+
use ast::{Block, Ident, Mac_, PatKind};
1212
use ast::{Name, MacStmtStyle, StmtKind, ItemKind};
1313
use ast;
1414
use ext::hygiene::Mark;
@@ -26,6 +26,7 @@ use parse::parser::Parser;
2626
use parse::token::{self, intern, keywords};
2727
use print::pprust;
2828
use ptr::P;
29+
use std_inject;
2930
use tokenstream::{TokenTree, TokenStream};
3031
use util::small_vector::SmallVector;
3132
use visit::Visitor;
@@ -186,8 +187,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
186187
MacroExpander { cx: cx, monotonic: monotonic }
187188
}
188189

189-
fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
190-
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
190+
pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
191+
self.cx.crate_root = std_inject::injected_crate_name(&krate);
192+
let mut module = ModuleData {
193+
mod_path: vec![token::str_to_ident(&self.cx.ecfg.crate_name)],
194+
directory: PathBuf::from(self.cx.codemap().span_to_filename(krate.span)),
195+
};
196+
module.directory.pop();
197+
self.cx.current_expansion.module = Rc::new(module);
191198

192199
let krate_item = Expansion::Items(SmallVector::one(P(ast::Item {
193200
attrs: krate.attrs,
@@ -206,10 +213,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
206213
_ => unreachable!(),
207214
};
208215

209-
if self.cx.parse_sess.span_diagnostic.err_count() - self.cx.resolve_err_count > err_count {
210-
self.cx.parse_sess.span_diagnostic.abort_if_errors();
211-
}
212-
213216
krate
214217
}
215218

@@ -866,18 +869,6 @@ impl<'feat> ExpansionConfig<'feat> {
866869
}
867870
}
868871

869-
pub fn expand_crate(cx: &mut ExtCtxt, c: Crate) -> Crate {
870-
cx.initialize(&c);
871-
cx.monotonic_expander().expand_crate(c)
872-
}
873-
874-
// Expands crate using supplied MacroExpander - allows for
875-
// non-standard expansion behaviour (e.g. step-wise).
876-
pub fn expand_crate_with_expander(expander: &mut MacroExpander, c: Crate) -> Crate {
877-
expander.cx.initialize(&c);
878-
expander.expand_crate(c)
879-
}
880-
881872
// A Marker adds the given mark to the syntax context and
882873
// sets spans' `expn_id` to the given expn_id (unless it is `None`).
883874
struct Marker { mark: Mark, expn_id: Option<ExpnId> }

0 commit comments

Comments
 (0)