Skip to content

Commit 9c514a1

Browse files
committed
Refactor away CrateLoader::load_macros.
1 parent 4968600 commit 9c514a1

File tree

4 files changed

+23
-33
lines changed

4 files changed

+23
-33
lines changed

src/librustc/middle/cstore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ pub enum LoadedMacros {
423423
}
424424

425425
pub trait CrateLoader {
426-
fn load_macros(&mut self, extern_crate: &ast::Item) -> LoadedMacros;
427-
fn process_item(&mut self, item: &ast::Item, defs: &Definitions);
426+
fn process_item(&mut self, item: &ast::Item, defs: &Definitions, load_macros: bool)
427+
-> Option<LoadedMacros>;
428428
fn postprocess(&mut self, krate: &ast::Crate);
429429
}

src/librustc_metadata/creader.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc::session::search_paths::PathKind;
2323
use rustc::middle;
2424
use rustc::middle::cstore::{CrateStore, validate_crate_name, ExternCrate};
2525
use rustc::util::nodemap::{FnvHashMap, FnvHashSet};
26-
use rustc::hir::map as hir_map;
26+
use rustc::hir::map::Definitions;
2727

2828
use std::cell::{RefCell, Cell};
2929
use std::ops::Deref;
@@ -631,8 +631,6 @@ impl<'a> CrateLoader<'a> {
631631
use rustc_back::dynamic_lib::DynamicLibrary;
632632
use syntax_ext::deriving::custom::CustomDerive;
633633

634-
self.cstore.add_used_for_derive_macros(item);
635-
636634
// Make sure the path contains a / or the linker will search for it.
637635
let path = env::current_dir().unwrap().join(path);
638636
let lib = match DynamicLibrary::open(Some(&path)) {
@@ -1020,29 +1018,33 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10201018
self.register_statically_included_foreign_items();
10211019
}
10221020

1023-
fn process_item(&mut self, item: &ast::Item, definitions: &hir_map::Definitions) {
1021+
fn process_item(&mut self, item: &ast::Item, definitions: &Definitions, load_macros: bool)
1022+
-> Option<LoadedMacros> {
10241023
match item.node {
10251024
ast::ItemKind::ExternCrate(_) => {}
1026-
ast::ItemKind::ForeignMod(ref fm) => return self.process_foreign_mod(item, fm),
1027-
_ => return,
1025+
ast::ItemKind::ForeignMod(ref fm) => {
1026+
self.process_foreign_mod(item, fm);
1027+
return None;
1028+
}
1029+
_ => return None,
10281030
}
10291031

1032+
let loaded_macros = if load_macros { Some(self.read_macros(item)) } else { None };
1033+
10301034
// If this `extern crate` item has `#[macro_use]` then we can safely skip it.
10311035
// These annotations were processed during macro expansion and are already loaded
10321036
// (if necessary) into our crate store.
10331037
//
10341038
// Note that it's important we *don't* fall through below as some `#[macro_use]`
10351039
// crates are explicitly not linked (e.g. macro crates) so we want to ensure
10361040
// we avoid `resolve_crate` with those.
1037-
if attr::contains_name(&item.attrs, "macro_use") {
1038-
if self.cstore.was_used_for_derive_macros(item) {
1039-
return
1040-
}
1041+
if let Some(LoadedMacros::CustomDerives(..)) = loaded_macros {
1042+
return loaded_macros;
10411043
}
10421044

10431045
if let Some(info) = self.extract_crate_info(item) {
10441046
if !info.should_link {
1045-
return;
1047+
return loaded_macros;
10461048
}
10471049

10481050
let (cnum, ..) = self.resolve_crate(
@@ -1058,9 +1060,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10581060

10591061
self.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
10601062
}
1061-
}
10621063

1063-
fn load_macros(&mut self, extern_crate: &ast::Item) -> LoadedMacros {
1064-
self.read_macros(extern_crate)
1064+
loaded_macros
10651065
}
10661066
}

src/librustc_metadata/cstore.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ use rustc::hir::svh::Svh;
2121
use rustc::middle::cstore::ExternCrate;
2222
use rustc_back::PanicStrategy;
2323
use rustc_data_structures::indexed_vec::IndexVec;
24-
use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap, FnvHashSet};
24+
use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap};
2525

2626
use std::cell::{RefCell, Cell};
2727
use std::rc::Rc;
2828
use std::path::PathBuf;
2929
use flate::Bytes;
30-
use syntax::ast::{self, Ident};
31-
use syntax::attr;
30+
use syntax::{ast, attr};
3231
use syntax_pos;
3332

3433
pub use rustc::middle::cstore::{NativeLibraryKind, LinkagePreference};
@@ -105,7 +104,6 @@ pub struct CStore {
105104
pub inlined_item_cache: RefCell<DefIdMap<Option<CachedInlinedItem>>>,
106105
pub defid_for_inlined_node: RefCell<NodeMap<DefId>>,
107106
pub visible_parent_map: RefCell<DefIdMap<DefId>>,
108-
pub used_for_derive_macro: RefCell<FnvHashSet<Ident>>,
109107
}
110108

111109
impl CStore {
@@ -121,7 +119,6 @@ impl CStore {
121119
visible_parent_map: RefCell::new(FnvHashMap()),
122120
inlined_item_cache: RefCell::new(FnvHashMap()),
123121
defid_for_inlined_node: RefCell::new(FnvHashMap()),
124-
used_for_derive_macro: RefCell::new(FnvHashSet()),
125122
}
126123
}
127124

@@ -277,14 +274,6 @@ impl CStore {
277274
{
278275
self.extern_mod_crate_map.borrow().get(&emod_id).cloned()
279276
}
280-
281-
pub fn was_used_for_derive_macros(&self, i: &ast::Item) -> bool {
282-
self.used_for_derive_macro.borrow().contains(&i.ident)
283-
}
284-
285-
pub fn add_used_for_derive_macros(&self, i: &ast::Item) {
286-
self.used_for_derive_macro.borrow_mut().insert(i.ident);
287-
}
288277
}
289278

290279
impl CrateMetadata {

src/librustc_resolve/build_reduced_graph.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,11 @@ impl<'b> Resolver<'b> {
215215
}
216216

217217
let loaded_macros = if legacy_imports != LegacyMacroImports::default() {
218-
Some(self.crate_loader.load_macros(item))
218+
self.crate_loader.process_item(item, &self.definitions, true)
219219
} else {
220-
None
220+
self.crate_loader.process_item(item, &self.definitions, false)
221221
};
222222

223-
self.crate_loader.process_item(item, &self.definitions);
224223
// n.b. we don't need to look at the path option here, because cstore already did
225224
let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id);
226225
let module = if let Some(crate_id) = crate_id {
@@ -270,7 +269,9 @@ impl<'b> Resolver<'b> {
270269
self.current_module = module;
271270
}
272271

273-
ItemKind::ForeignMod(..) => self.crate_loader.process_item(item, &self.definitions),
272+
ItemKind::ForeignMod(..) => {
273+
self.crate_loader.process_item(item, &self.definitions, false);
274+
}
274275

275276
// These items live in the value namespace.
276277
ItemKind::Static(_, m, _) => {

0 commit comments

Comments
 (0)