Skip to content

Commit 1bdd2f6

Browse files
committed
Conditionally skip two passes if their related attributes were not found
1 parent ec504de commit 1bdd2f6

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,7 @@ dependencies = [
27222722
"rustc_errors 0.0.0",
27232723
"rustc_mir 0.0.0",
27242724
"syntax 0.0.0",
2725+
"syntax_ext 0.0.0",
27252726
"syntax_pos 0.0.0",
27262727
]
27272728

src/librustc_driver/driver.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,10 @@ where
10171017
krate = ReplaceBodyWithLoop::new(sess).fold_crate(krate);
10181018
}
10191019

1020+
let (has_proc_macro_decls, has_global_allocator) = time(sess, "AST validation", || {
1021+
ast_validation::check_crate(sess, &krate)
1022+
});
1023+
10201024
// If we're in rustdoc we're always compiling as an rlib, but that'll trip a
10211025
// bunch of checks in the `modify` function below. For now just skip this
10221026
// step entirely if we're rustdoc as it's not too useful anyway.
@@ -1031,23 +1035,26 @@ where
10311035
&mut resolver,
10321036
krate,
10331037
is_proc_macro_crate,
1038+
has_proc_macro_decls,
10341039
is_test_crate,
10351040
num_crate_types,
10361041
sess.diagnostic(),
10371042
)
10381043
});
10391044
}
10401045

1041-
// Expand global allocators, which are treated as an in-tree proc macro
1042-
krate = time(sess, "creating allocators", || {
1043-
allocator::expand::modify(
1044-
&sess.parse_sess,
1045-
&mut resolver,
1046-
krate,
1047-
crate_name.to_string(),
1048-
sess.diagnostic(),
1049-
)
1050-
});
1046+
if has_global_allocator {
1047+
// Expand global allocators, which are treated as an in-tree proc macro
1048+
krate = time(sess, "creating allocators", || {
1049+
allocator::expand::modify(
1050+
&sess.parse_sess,
1051+
&mut resolver,
1052+
krate,
1053+
crate_name.to_string(),
1054+
sess.diagnostic(),
1055+
)
1056+
});
1057+
}
10511058

10521059
// Done with macro expansion!
10531060

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

1068-
time(sess, "AST validation", || {
1069-
ast_validation::check_crate(sess, &krate)
1070-
});
1071-
10721075
time(sess, "name resolution", || {
10731076
resolver.resolve_crate(&krate);
10741077
});

src/librustc_passes/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ rustc = { path = "../librustc" }
1414
rustc_mir = { path = "../librustc_mir"}
1515
rustc_data_structures = { path = "../librustc_data_structures" }
1616
syntax = { path = "../libsyntax" }
17+
syntax_ext = { path = "../libsyntax_ext" }
1718
syntax_pos = { path = "../libsyntax_pos" }
1819
rustc_errors = { path = "../librustc_errors" }

src/librustc_passes/ast_validation.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ use syntax::source_map::Spanned;
1515
use syntax::symbol::keywords;
1616
use syntax::ptr::P;
1717
use syntax::visit::{self, Visitor};
18+
use syntax_ext::proc_macro_decls::is_proc_macro_attr;
1819
use syntax_pos::Span;
1920
use errors;
2021
use errors::Applicability;
2122

2223
struct AstValidator<'a> {
2324
session: &'a Session,
25+
has_proc_macro_decls: bool,
26+
has_global_allocator: bool,
2427

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

369372
fn visit_item(&mut self, item: &'a Item) {
373+
if item.attrs.iter().any(|attr| is_proc_macro_attr(attr) ) {
374+
self.has_proc_macro_decls = true;
375+
}
376+
377+
if attr::contains_name(&item.attrs, "global_allocator") {
378+
self.has_global_allocator = true;
379+
}
380+
370381
match item.node {
371382
ItemKind::Impl(unsafety, polarity, _, _, Some(..), ref ty, ref impl_items) => {
372383
self.invalid_visibility(&item.vis, None);
@@ -590,10 +601,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
590601
}
591602
}
592603

593-
pub fn check_crate(session: &Session, krate: &Crate) {
594-
visit::walk_crate(&mut AstValidator {
604+
pub fn check_crate(session: &Session, krate: &Crate) -> (bool, bool) {
605+
let mut validator = AstValidator {
595606
session,
607+
has_proc_macro_decls: false,
608+
has_global_allocator: false,
596609
outer_impl_trait: None,
597610
is_impl_trait_banned: false,
598-
}, krate)
611+
};
612+
visit::walk_crate(&mut validator, krate);
613+
614+
(validator.has_proc_macro_decls, validator.has_global_allocator)
599615
}

src/librustc_passes/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern crate rustc_data_structures;
2222
extern crate log;
2323
#[macro_use]
2424
extern crate syntax;
25+
extern crate syntax_ext;
2526
extern crate syntax_pos;
2627
extern crate rustc_errors as errors;
2728

src/libsyntax_ext/proc_macro_decls.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub fn modify(sess: &ParseSess,
4848
resolver: &mut dyn (::syntax::ext::base::Resolver),
4949
mut krate: ast::Crate,
5050
is_proc_macro_crate: bool,
51+
has_proc_macro_decls: bool,
5152
is_test_crate: bool,
5253
num_crate_types: usize,
5354
handler: &errors::Handler) -> ast::Crate {
@@ -64,7 +65,9 @@ pub fn modify(sess: &ParseSess,
6465
is_proc_macro_crate,
6566
is_test_crate,
6667
};
67-
visit::walk_crate(&mut collect, &krate);
68+
if has_proc_macro_decls || is_proc_macro_crate {
69+
visit::walk_crate(&mut collect, &krate);
70+
}
6871
(collect.derives, collect.attr_macros, collect.bang_macros)
6972
};
7073

@@ -85,7 +88,7 @@ pub fn modify(sess: &ParseSess,
8588
krate
8689
}
8790

88-
fn is_proc_macro_attr(attr: &ast::Attribute) -> bool {
91+
pub fn is_proc_macro_attr(attr: &ast::Attribute) -> bool {
8992
PROC_MACRO_KINDS.iter().any(|kind| attr.check_name(kind))
9093
}
9194

0 commit comments

Comments
 (0)