Skip to content

Commit ce7bc51

Browse files
committed
Auto merge of rust-lang#30300 - sanxiyn:syntax-ext, r=nikomatsakis
This reduces iteration time (`make rustc-stage1`) for moved syntax extensions from 11 minutes to 3 minutes on my machine. Because of the signature change, this is a [breaking-change] for people directly calling `expand_crate`. I think it is rare: from GitHub search, only case I found is [glassful](https://github.com/kmcallister/glassful).
2 parents ac2c5ff + 0883f10 commit ce7bc51

32 files changed

+337
-329
lines changed

mk/crates.mk

+5-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ RUSTC_CRATES := rustc rustc_typeck rustc_mir rustc_borrowck rustc_resolve rustc_
5858
rustc_trans rustc_back rustc_llvm rustc_privacy rustc_lint \
5959
rustc_data_structures rustc_front rustc_platform_intrinsics \
6060
rustc_plugin rustc_metadata
61-
HOST_CRATES := syntax $(RUSTC_CRATES) rustdoc fmt_macros
61+
HOST_CRATES := syntax syntax_ext $(RUSTC_CRATES) rustdoc fmt_macros
6262
TOOLS := compiletest rustdoc rustc rustbook error-index-generator
6363

6464
DEPS_core :=
@@ -86,17 +86,18 @@ DEPS_serialize := std log
8686
DEPS_term := std log
8787
DEPS_test := std getopts serialize rbml term native:rust_test_helpers
8888

89-
DEPS_syntax := std term serialize log fmt_macros arena libc rustc_bitflags
89+
DEPS_syntax := std term serialize log arena libc rustc_bitflags
90+
DEPS_syntax_ext := syntax fmt_macros
9091

91-
DEPS_rustc := syntax flate arena serialize getopts rbml rustc_front\
92+
DEPS_rustc := syntax fmt_macros flate arena serialize getopts rbml rustc_front\
9293
log graphviz rustc_llvm rustc_back rustc_data_structures
9394
DEPS_rustc_back := std syntax rustc_llvm rustc_front flate log libc
9495
DEPS_rustc_borrowck := rustc rustc_front log graphviz syntax
9596
DEPS_rustc_data_structures := std log serialize
9697
DEPS_rustc_driver := arena flate getopts graphviz libc rustc rustc_back rustc_borrowck \
9798
rustc_typeck rustc_mir rustc_resolve log syntax serialize rustc_llvm \
9899
rustc_trans rustc_privacy rustc_lint rustc_front rustc_plugin \
99-
rustc_metadata
100+
rustc_metadata syntax_ext
100101
DEPS_rustc_front := std syntax log serialize
101102
DEPS_rustc_lint := rustc log syntax
102103
DEPS_rustc_llvm := native:rustllvm libc std rustc_bitflags

src/librustc_driver/driver.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use syntax::parse::token;
5454
use syntax::util::node_count::NodeCounter;
5555
use syntax::visit;
5656
use syntax;
57+
use syntax_ext;
5758

5859
pub fn compile_input(sess: Session,
5960
cstore: &CStore,
@@ -563,12 +564,15 @@ pub fn phase_2_configure_and_expand(sess: &Session,
563564
recursion_limit: sess.recursion_limit.get(),
564565
trace_mac: sess.opts.debugging_opts.trace_macros,
565566
};
566-
let (ret, macro_names) = syntax::ext::expand::expand_crate(&sess.parse_sess,
567-
cfg,
568-
macros,
569-
syntax_exts,
570-
&mut feature_gated_cfgs,
571-
krate);
567+
let mut ecx = syntax::ext::base::ExtCtxt::new(&sess.parse_sess,
568+
krate.config.clone(),
569+
cfg,
570+
&mut feature_gated_cfgs);
571+
syntax_ext::register_builtins(&mut ecx.syntax_env);
572+
let (ret, macro_names) = syntax::ext::expand::expand_crate(ecx,
573+
macros,
574+
syntax_exts,
575+
krate);
572576
if cfg!(windows) {
573577
env::set_var("PATH", &_old_path);
574578
}

src/librustc_driver/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extern crate rustc_llvm as llvm;
5757
extern crate log;
5858
#[macro_use]
5959
extern crate syntax;
60+
extern crate syntax_ext;
6061

6162
pub use syntax::diagnostic;
6263

src/libsyntax/ext/base.rs

-29
Original file line numberDiff line numberDiff line change
@@ -464,26 +464,6 @@ fn initial_syntax_expander_table<'feat>(ecfg: &expand::ExpansionConfig<'feat>)
464464

465465
let mut syntax_expanders = SyntaxEnv::new();
466466
syntax_expanders.insert(intern("macro_rules"), MacroRulesTT);
467-
syntax_expanders.insert(intern("format_args"),
468-
// format_args uses `unstable` things internally.
469-
NormalTT(Box::new(ext::format::expand_format_args), None, true));
470-
syntax_expanders.insert(intern("env"),
471-
builtin_normal_expander(
472-
ext::env::expand_env));
473-
syntax_expanders.insert(intern("option_env"),
474-
builtin_normal_expander(
475-
ext::env::expand_option_env));
476-
syntax_expanders.insert(intern("concat_idents"),
477-
builtin_normal_expander(
478-
ext::concat_idents::expand_syntax_ext));
479-
syntax_expanders.insert(intern("concat"),
480-
builtin_normal_expander(
481-
ext::concat::expand_syntax_ext));
482-
syntax_expanders.insert(intern("log_syntax"),
483-
builtin_normal_expander(
484-
ext::log_syntax::expand_syntax_ext));
485-
486-
ext::deriving::register_all(&mut syntax_expanders);
487467

488468
if ecfg.enable_quotes() {
489469
// Quasi-quoting expanders
@@ -552,15 +532,6 @@ fn initial_syntax_expander_table<'feat>(ecfg: &expand::ExpansionConfig<'feat>)
552532
syntax_expanders.insert(intern("module_path"),
553533
builtin_normal_expander(
554534
ext::source_util::expand_mod));
555-
syntax_expanders.insert(intern("asm"),
556-
builtin_normal_expander(
557-
ext::asm::expand_asm));
558-
syntax_expanders.insert(intern("cfg"),
559-
builtin_normal_expander(
560-
ext::cfg::expand_cfg));
561-
syntax_expanders.insert(intern("trace_macros"),
562-
builtin_normal_expander(
563-
ext::trace_macros::expand_trace_macros));
564535
syntax_expanders
565536
}
566537

src/libsyntax/ext/expand.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use attr::{AttrMetaMethods, WithAttrs};
2121
use codemap;
2222
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
2323
use ext::base::*;
24-
use feature_gate::{self, Features, GatedCfgAttr};
24+
use feature_gate::{self, Features};
2525
use fold;
2626
use fold::*;
2727
use util::move_map::MoveMap;
@@ -1276,15 +1276,11 @@ impl<'feat> ExpansionConfig<'feat> {
12761276
}
12771277
}
12781278

1279-
pub fn expand_crate<'feat>(parse_sess: &parse::ParseSess,
1280-
cfg: ExpansionConfig<'feat>,
1281-
// these are the macros being imported to this crate:
1282-
imported_macros: Vec<ast::MacroDef>,
1283-
user_exts: Vec<NamedSyntaxExtension>,
1284-
feature_gated_cfgs: &mut Vec<GatedCfgAttr>,
1285-
c: Crate) -> (Crate, HashSet<Name>) {
1286-
let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg,
1287-
feature_gated_cfgs);
1279+
pub fn expand_crate(mut cx: ExtCtxt,
1280+
// these are the macros being imported to this crate:
1281+
imported_macros: Vec<ast::MacroDef>,
1282+
user_exts: Vec<NamedSyntaxExtension>,
1283+
c: Crate) -> (Crate, HashSet<Name>) {
12881284
if std_inject::no_core(&c) {
12891285
cx.crate_root = None;
12901286
} else if std_inject::no_std(&c) {
@@ -1305,7 +1301,7 @@ pub fn expand_crate<'feat>(parse_sess: &parse::ParseSess,
13051301

13061302
let mut ret = expander.fold_crate(c);
13071303
ret.exported_macros = expander.cx.exported_macros.clone();
1308-
parse_sess.span_diagnostic.handler().abort_if_errors();
1304+
cx.parse_sess.span_diagnostic.handler().abort_if_errors();
13091305
ret
13101306
};
13111307
return (ret, cx.syntax_env.names);
@@ -1401,6 +1397,7 @@ mod tests {
14011397
use ast;
14021398
use ast::Name;
14031399
use codemap;
1400+
use ext::base::ExtCtxt;
14041401
use ext::mtwt;
14051402
use fold::Folder;
14061403
use parse;
@@ -1471,7 +1468,9 @@ mod tests {
14711468
src,
14721469
Vec::new(), &sess);
14731470
// should fail:
1474-
expand_crate(&sess,test_ecfg(),vec!(),vec!(), &mut vec![], crate_ast);
1471+
let mut gated_cfgs = vec![];
1472+
let ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut gated_cfgs);
1473+
expand_crate(ecx, vec![], vec![], crate_ast);
14751474
}
14761475

14771476
// make sure that macros can't escape modules
@@ -1484,7 +1483,9 @@ mod tests {
14841483
"<test>".to_string(),
14851484
src,
14861485
Vec::new(), &sess);
1487-
expand_crate(&sess,test_ecfg(),vec!(),vec!(), &mut vec![], crate_ast);
1486+
let mut gated_cfgs = vec![];
1487+
let ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut gated_cfgs);
1488+
expand_crate(ecx, vec![], vec![], crate_ast);
14881489
}
14891490

14901491
// macro_use modules should allow macros to escape
@@ -1496,14 +1497,18 @@ mod tests {
14961497
"<test>".to_string(),
14971498
src,
14981499
Vec::new(), &sess);
1499-
expand_crate(&sess, test_ecfg(), vec!(), vec!(), &mut vec![], crate_ast);
1500+
let mut gated_cfgs = vec![];
1501+
let ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut gated_cfgs);
1502+
expand_crate(ecx, vec![], vec![], crate_ast);
15001503
}
15011504

15021505
fn expand_crate_str(crate_str: String) -> ast::Crate {
15031506
let ps = parse::ParseSess::new();
15041507
let crate_ast = panictry!(string_to_parser(&ps, crate_str).parse_crate_mod());
15051508
// the cfg argument actually does matter, here...
1506-
expand_crate(&ps,test_ecfg(),vec!(),vec!(), &mut vec![], crate_ast).0
1509+
let mut gated_cfgs = vec![];
1510+
let ecx = ExtCtxt::new(&ps, vec![], test_ecfg(), &mut gated_cfgs);
1511+
expand_crate(ecx, vec![], vec![], crate_ast).0
15071512
}
15081513

15091514
// find the pat_ident paths in a crate

src/libsyntax/lib.rs

-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#![feature(str_escape)]
3838
#![feature(unicode)]
3939

40-
extern crate fmt_macros;
4140
extern crate serialize;
4241
extern crate term;
4342
extern crate libc;
@@ -110,21 +109,12 @@ pub mod print {
110109
}
111110

112111
pub mod ext {
113-
pub mod asm;
114112
pub mod base;
115113
pub mod build;
116-
pub mod cfg;
117-
pub mod concat;
118-
pub mod concat_idents;
119-
pub mod deriving;
120-
pub mod env;
121114
pub mod expand;
122-
pub mod format;
123-
pub mod log_syntax;
124115
pub mod mtwt;
125116
pub mod quote;
126117
pub mod source_util;
127-
pub mod trace_macros;
128118

129119
pub mod tt {
130120
pub mod transcribe;

src/libsyntax/ext/asm.rs renamed to src/libsyntax_ext/asm.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
*/
1414
use self::State::*;
1515

16-
use ast;
17-
use codemap;
18-
use codemap::Span;
19-
use ext::base;
20-
use ext::base::*;
21-
use feature_gate;
22-
use parse::token::{intern, InternedString};
23-
use parse::token;
24-
use ptr::P;
16+
use syntax::ast;
17+
use syntax::codemap;
18+
use syntax::codemap::Span;
19+
use syntax::ext::base;
20+
use syntax::ext::base::*;
21+
use syntax::feature_gate;
22+
use syntax::parse::token::{intern, InternedString};
23+
use syntax::parse::token;
24+
use syntax::ptr::P;
2525
use syntax::ast::AsmDialect;
2626

2727
enum State {

src/libsyntax/ext/cfg.rs renamed to src/libsyntax_ext/cfg.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
/// a literal `true` or `false` based on whether the given cfg matches the
1313
/// current compilation environment.
1414
15-
use ast;
16-
use codemap::Span;
17-
use ext::base::*;
18-
use ext::base;
19-
use ext::build::AstBuilder;
20-
use attr;
21-
use attr::*;
22-
use parse::token;
23-
use config::CfgDiagReal;
15+
use syntax::ast;
16+
use syntax::codemap::Span;
17+
use syntax::ext::base::*;
18+
use syntax::ext::base;
19+
use syntax::ext::build::AstBuilder;
20+
use syntax::attr;
21+
use syntax::attr::*;
22+
use syntax::parse::token;
23+
use syntax::config::CfgDiagReal;
2424

2525
pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
2626
sp: Span,

src/libsyntax/ext/concat.rs renamed to src/libsyntax_ext/concat.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast;
12-
use codemap;
13-
use ext::base;
14-
use ext::build::AstBuilder;
15-
use parse::token;
11+
use syntax::ast;
12+
use syntax::codemap;
13+
use syntax::ext::base;
14+
use syntax::ext::build::AstBuilder;
15+
use syntax::parse::token;
1616

1717
use std::string::String;
1818

src/libsyntax/ext/concat_idents.rs renamed to src/libsyntax_ext/concat_idents.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{self, TokenTree};
12-
use codemap::Span;
13-
use ext::base::*;
14-
use ext::base;
15-
use feature_gate;
16-
use parse::token;
17-
use parse::token::str_to_ident;
18-
use ptr::P;
11+
use syntax::ast::{self, TokenTree};
12+
use syntax::codemap::Span;
13+
use syntax::ext::base::*;
14+
use syntax::ext::base;
15+
use syntax::feature_gate;
16+
use syntax::parse::token;
17+
use syntax::parse::token::str_to_ident;
18+
use syntax::ptr::P;
1919

2020
pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
2121
-> Box<base::MacResult+'cx> {

src/libsyntax/ext/deriving/bounds.rs renamed to src/libsyntax_ext/deriving/bounds.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::MetaItem;
12-
use codemap::Span;
13-
use ext::base::{ExtCtxt, Annotatable};
14-
use ext::deriving::generic::*;
15-
use ext::deriving::generic::ty::*;
11+
use deriving::generic::*;
12+
use deriving::generic::ty::*;
13+
14+
use syntax::ast::MetaItem;
15+
use syntax::codemap::Span;
16+
use syntax::ext::base::{ExtCtxt, Annotatable};
1617

1718
pub fn expand_deriving_unsafe_bound(cx: &mut ExtCtxt,
1819
span: Span,

src/libsyntax/ext/deriving/clone.rs renamed to src/libsyntax_ext/deriving/clone.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{MetaItem, Expr};
12-
use codemap::Span;
13-
use ext::base::{ExtCtxt, Annotatable};
14-
use ext::build::AstBuilder;
15-
use ext::deriving::generic::*;
16-
use ext::deriving::generic::ty::*;
17-
use parse::token::InternedString;
18-
use ptr::P;
11+
use deriving::generic::*;
12+
use deriving::generic::ty::*;
13+
14+
use syntax::ast::{MetaItem, Expr};
15+
use syntax::codemap::Span;
16+
use syntax::ext::base::{ExtCtxt, Annotatable};
17+
use syntax::ext::build::AstBuilder;
18+
use syntax::parse::token::InternedString;
19+
use syntax::ptr::P;
1920

2021
pub fn expand_deriving_clone(cx: &mut ExtCtxt,
2122
span: Span,

src/libsyntax/ext/deriving/cmp/eq.rs renamed to src/libsyntax_ext/deriving/cmp/eq.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use ast::{MetaItem, Expr};
12-
use codemap::Span;
13-
use ext::base::{ExtCtxt, Annotatable};
14-
use ext::build::AstBuilder;
15-
use ext::deriving::generic::*;
16-
use ext::deriving::generic::ty::*;
17-
use parse::token::InternedString;
18-
use ptr::P;
11+
use deriving::generic::*;
12+
use deriving::generic::ty::*;
13+
14+
use syntax::ast::{MetaItem, Expr};
15+
use syntax::codemap::Span;
16+
use syntax::ext::base::{ExtCtxt, Annotatable};
17+
use syntax::ext::build::AstBuilder;
18+
use syntax::parse::token::InternedString;
19+
use syntax::ptr::P;
1920

2021
pub fn expand_deriving_eq(cx: &mut ExtCtxt,
2122
span: Span,

0 commit comments

Comments
 (0)