Skip to content

Commit

Permalink
Auto merge of #57691 - Zoxc:pass-skip, r=petrochenkov
Browse files Browse the repository at this point in the history
Conditionally skip two passes if their related attributes were not found
  • Loading branch information
bors committed Jan 28, 2019
2 parents c325155 + 1bdd2f6 commit cc4d1e5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2722,6 +2722,7 @@ dependencies = [
"rustc_errors 0.0.0",
"rustc_mir 0.0.0",
"syntax 0.0.0",
"syntax_ext 0.0.0",
"syntax_pos 0.0.0",
]

Expand Down
31 changes: 17 additions & 14 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,10 @@ where
krate = ReplaceBodyWithLoop::new(sess).fold_crate(krate);
}

let (has_proc_macro_decls, has_global_allocator) = time(sess, "AST validation", || {
ast_validation::check_crate(sess, &krate)
});

// If we're in rustdoc we're always compiling as an rlib, but that'll trip a
// bunch of checks in the `modify` function below. For now just skip this
// step entirely if we're rustdoc as it's not too useful anyway.
Expand All @@ -1031,23 +1035,26 @@ where
&mut resolver,
krate,
is_proc_macro_crate,
has_proc_macro_decls,
is_test_crate,
num_crate_types,
sess.diagnostic(),
)
});
}

// Expand global allocators, which are treated as an in-tree proc macro
krate = time(sess, "creating allocators", || {
allocator::expand::modify(
&sess.parse_sess,
&mut resolver,
krate,
crate_name.to_string(),
sess.diagnostic(),
)
});
if has_global_allocator {
// Expand global allocators, which are treated as an in-tree proc macro
krate = time(sess, "creating allocators", || {
allocator::expand::modify(
&sess.parse_sess,
&mut resolver,
krate,
crate_name.to_string(),
sess.diagnostic(),
)
});
}

// Done with macro expansion!

Expand All @@ -1065,10 +1072,6 @@ where
println!("{}", json::as_json(&krate));
}

time(sess, "AST validation", || {
ast_validation::check_crate(sess, &krate)
});

time(sess, "name resolution", || {
resolver.resolve_crate(&krate);
});
Expand Down
1 change: 1 addition & 0 deletions src/librustc_passes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ rustc = { path = "../librustc" }
rustc_mir = { path = "../librustc_mir"}
rustc_data_structures = { path = "../librustc_data_structures" }
syntax = { path = "../libsyntax" }
syntax_ext = { path = "../libsyntax_ext" }
syntax_pos = { path = "../libsyntax_pos" }
rustc_errors = { path = "../librustc_errors" }
22 changes: 19 additions & 3 deletions src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ use syntax::source_map::Spanned;
use syntax::symbol::keywords;
use syntax::ptr::P;
use syntax::visit::{self, Visitor};
use syntax_ext::proc_macro_decls::is_proc_macro_attr;
use syntax_pos::Span;
use errors;
use errors::Applicability;

struct AstValidator<'a> {
session: &'a Session,
has_proc_macro_decls: bool,
has_global_allocator: bool,

// Used to ban nested `impl Trait`, e.g., `impl Into<impl Debug>`.
// Nested `impl Trait` _is_ allowed in associated type position,
Expand Down Expand Up @@ -367,6 +370,14 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}

fn visit_item(&mut self, item: &'a Item) {
if item.attrs.iter().any(|attr| is_proc_macro_attr(attr) ) {
self.has_proc_macro_decls = true;
}

if attr::contains_name(&item.attrs, "global_allocator") {
self.has_global_allocator = true;
}

match item.node {
ItemKind::Impl(unsafety, polarity, _, _, Some(..), ref ty, ref impl_items) => {
self.invalid_visibility(&item.vis, None);
Expand Down Expand Up @@ -590,10 +601,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
}

pub fn check_crate(session: &Session, krate: &Crate) {
visit::walk_crate(&mut AstValidator {
pub fn check_crate(session: &Session, krate: &Crate) -> (bool, bool) {
let mut validator = AstValidator {
session,
has_proc_macro_decls: false,
has_global_allocator: false,
outer_impl_trait: None,
is_impl_trait_banned: false,
}, krate)
};
visit::walk_crate(&mut validator, krate);

(validator.has_proc_macro_decls, validator.has_global_allocator)
}
1 change: 1 addition & 0 deletions src/librustc_passes/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern crate rustc_data_structures;
extern crate log;
#[macro_use]
extern crate syntax;
extern crate syntax_ext;
extern crate syntax_pos;
extern crate rustc_errors as errors;

Expand Down
7 changes: 5 additions & 2 deletions src/libsyntax_ext/proc_macro_decls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn modify(sess: &ParseSess,
resolver: &mut dyn (::syntax::ext::base::Resolver),
mut krate: ast::Crate,
is_proc_macro_crate: bool,
has_proc_macro_decls: bool,
is_test_crate: bool,
num_crate_types: usize,
handler: &errors::Handler) -> ast::Crate {
Expand All @@ -64,7 +65,9 @@ pub fn modify(sess: &ParseSess,
is_proc_macro_crate,
is_test_crate,
};
visit::walk_crate(&mut collect, &krate);
if has_proc_macro_decls || is_proc_macro_crate {
visit::walk_crate(&mut collect, &krate);
}
(collect.derives, collect.attr_macros, collect.bang_macros)
};

Expand All @@ -85,7 +88,7 @@ pub fn modify(sess: &ParseSess,
krate
}

fn is_proc_macro_attr(attr: &ast::Attribute) -> bool {
pub fn is_proc_macro_attr(attr: &ast::Attribute) -> bool {
PROC_MACRO_KINDS.iter().any(|kind| attr.check_name(kind))
}

Expand Down

0 comments on commit cc4d1e5

Please sign in to comment.