Skip to content

macros: stackless expansion #36214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 8, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e1e5c14
In `Parser` and `ExtCtxt`, replace fields `filename` and `mod_path_st…
jseyfried Aug 31, 2016
234d68b
Improve `expand_type`.
jseyfried Aug 30, 2016
9b3bc7a
Remove `syntax::config::strip_unconfigured`, add `syntax::config::fea…
jseyfried Aug 31, 2016
de2e678
Add `Invocation` and `Expansion`, remove `MacroGenerable`.
jseyfried Aug 27, 2016
3cba93f
Refactor `with_exts_frame` from a macro to a function.
jseyfried Aug 27, 2016
fca80c9
Generalize `Invocation` to include modifiers/decorators.
jseyfried Sep 2, 2016
8be8cf8
Refactor away `expand_item`.
jseyfried Aug 28, 2016
2a83574
Refactor out `expand_item` (with better semantics than before).
jseyfried Aug 28, 2016
503a10b
Clean up module processing.
jseyfried Aug 28, 2016
4ed2c0e
Refactor `expand_*` into `expander.fold_*`.
jseyfried Aug 30, 2016
79fa9eb
Refactor `SyntaxEnv`.
jseyfried Sep 1, 2016
7a3ae57
Refactor `expand_invoc(.., fld)` -> `self.expand_invoc(..)`.
jseyfried Sep 1, 2016
c07ff8d
Add module `ext::placeholders` with `placeholder()` and `Placeholder…
jseyfried Aug 29, 2016
d986bbe
Implement stackless expansion.
jseyfried Sep 2, 2016
2c88b4b
Load macros from `extern crate`s in the `InvocationCollector` fold.
jseyfried Sep 2, 2016
3af0c65
Refactor code out of the folder implementation for `StripUnconfigured`.
jseyfried Sep 2, 2016
d76bf3e
Strip unconfigured nodes in the `InvocationCollector` fold.
jseyfried Sep 7, 2016
2d75904
Implement stackless placeholder expansion.
jseyfried Sep 2, 2016
9ac91fa
Improve `directory` computation during invocation collection.
jseyfried Sep 5, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove syntax::config::strip_unconfigured, add `syntax::config::fea…
…tures`.
  • Loading branch information
jseyfried committed Sep 5, 2016
commit 9b3bc7a9e9e35537823bc2a10fc078c1222ee7fd
20 changes: 4 additions & 16 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ pub struct ExpansionResult<'a> {
/// Returns `None` if we're aborting after handling -W help.
pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
cstore: &CStore,
mut krate: ast::Crate,
krate: ast::Crate,
registry: Option<Registry>,
crate_name: &'a str,
addl_plugins: Option<Vec<String>>,
Expand All @@ -562,21 +562,9 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
{
let time_passes = sess.time_passes();

// strip before anything else because crate metadata may use #[cfg_attr]
// and so macros can depend on configuration variables, such as
//
// #[macro_use] #[cfg(foo)]
// mod bar { macro_rules! baz!(() => {{}}) }
//
// baz! should not use this definition unless foo is enabled.

krate = time(time_passes, "configuration", || {
let (krate, features) =
syntax::config::strip_unconfigured_items(krate, &sess.parse_sess, sess.opts.test);
// these need to be set "early" so that expansion sees `quote` if enabled.
*sess.features.borrow_mut() = features;
krate
});
let (mut krate, features) = syntax::config::features(krate, &sess.parse_sess, sess.opts.test);
// these need to be set "early" so that expansion sees `quote` if enabled.
*sess.features.borrow_mut() = features;

*sess.crate_types.borrow_mut() = collect_crate_types(sess, &krate.attrs);
*sess.crate_disambiguator.borrow_mut() =
Expand Down
63 changes: 34 additions & 29 deletions src/libsyntax/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use attr::HasAttrs;
use feature_gate::{emit_feature_err, EXPLAIN_STMT_ATTR_SYNTAX, Features, get_features, GateIssue};
use fold::Folder;
use {fold, attr};
use ast;
use codemap::{Spanned, respan};
Expand All @@ -27,6 +26,40 @@ pub struct StripUnconfigured<'a> {
pub features: Option<&'a Features>,
}

// `cfg_attr`-process the crate's attributes and compute the crate's features.
pub fn features(mut krate: ast::Crate, sess: &ParseSess, should_test: bool)
-> (ast::Crate, Features) {
let features;
{
let mut strip_unconfigured = StripUnconfigured {
config: &krate.config.clone(),
should_test: should_test,
sess: sess,
features: None,
};

let unconfigured_attrs = krate.attrs.clone();
let err_count = sess.span_diagnostic.err_count();
if let Some(attrs) = strip_unconfigured.configure(krate.attrs) {
krate.attrs = attrs;
} else { // the entire crate is unconfigured
krate.attrs = Vec::new();
krate.module.items = Vec::new();
return (krate, Features::new());
}

features = get_features(&sess.span_diagnostic, &krate.attrs);

// Avoid reconfiguring malformed `cfg_attr`s
if err_count == sess.span_diagnostic.err_count() {
strip_unconfigured.features = Some(&features);
strip_unconfigured.configure(unconfigured_attrs);
}
}

(krate, features)
}

impl<'a> StripUnconfigured<'a> {
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
let node = self.process_cfg_attrs(node);
Expand Down Expand Up @@ -125,34 +158,6 @@ impl<'a> StripUnconfigured<'a> {
}
}

// Support conditional compilation by transforming the AST, stripping out
// any items that do not belong in the current configuration
pub fn strip_unconfigured_items(mut krate: ast::Crate, sess: &ParseSess, should_test: bool)
-> (ast::Crate, Features) {
let features;
{
let mut strip_unconfigured = StripUnconfigured {
config: &krate.config.clone(),
should_test: should_test,
sess: sess,
features: None,
};

let err_count = sess.span_diagnostic.err_count();
let krate_attrs = strip_unconfigured.configure(krate.attrs.clone()).unwrap_or_default();
features = get_features(&sess.span_diagnostic, &krate_attrs);
if err_count < sess.span_diagnostic.err_count() {
krate.attrs = krate_attrs.clone(); // Avoid reconfiguring malformed `cfg_attr`s
}

strip_unconfigured.features = Some(&features);
krate = strip_unconfigured.fold_crate(krate);
krate.attrs = krate_attrs;
}

(krate, features)
}

impl<'a> fold::Folder for StripUnconfigured<'a> {
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
ast::ForeignMod {
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,9 @@ pub fn expand_crate_with_expander(expander: &mut MacroExpander,
}

let items = SmallVector::many(c.module.items);
expander.load_macros(&items);
c.module.items = items.into();
let configured = items.fold_with(&mut expander.strip_unconfigured());
expander.load_macros(&configured);
c.module.items = configured.into();

let err_count = expander.cx.parse_sess.span_diagnostic.err_count();
let mut ret = expander.fold_crate(c);
Expand Down