Skip to content

Commit ea83349

Browse files
committed
Retire rustdocs ANALYSISKEY
The thread-local isn't needed and consists of mostly empty fields which were just used to move the data into `html::render::CACHE_KEY`.
1 parent 6136a86 commit ea83349

File tree

9 files changed

+90
-111
lines changed

9 files changed

+90
-111
lines changed

src/librustc/middle/privacy.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
use util::nodemap::{DefIdSet, FnvHashMap};
1616

1717
use std::hash::Hash;
18+
use std::fmt;
1819
use syntax::ast::NodeId;
1920

2021
// Accessibility levels, sorted in ascending order
21-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
22+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
2223
pub enum AccessLevel {
2324
// Exported items + items participating in various kinds of public interfaces,
2425
// but not directly nameable. For example, if function `fn f() -> T {...}` is
@@ -56,6 +57,12 @@ impl<Id: Hash + Eq> Default for AccessLevels<Id> {
5657
}
5758
}
5859

60+
impl<Id: Hash + Eq + fmt::Debug> fmt::Debug for AccessLevels<Id> {
61+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62+
fmt::Debug::fmt(&self.map, f)
63+
}
64+
}
65+
5966
/// A set containing all exported definitions from external crates.
6067
/// The set does not contain any entries from local crates.
6168
pub type ExternalExports = DefIdSet;

src/librustdoc/clean/inline.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn try_inline_def(cx: &DocContext, tcx: &TyCtxt,
116116
}
117117
_ => return None,
118118
};
119-
cx.inlined.borrow_mut().as_mut().unwrap().insert(did);
119+
cx.renderinfo.borrow_mut().inlined.insert(did);
120120
ret.push(clean::Item {
121121
source: clean::Span::empty(),
122122
name: Some(tcx.item_name(did).to_string()),
@@ -146,7 +146,7 @@ pub fn record_extern_fqn(cx: &DocContext, did: DefId, kind: clean::TypeKind) {
146146
elem.data.to_string()
147147
});
148148
let fqn = once(crate_name).chain(relative).collect();
149-
cx.external_paths.borrow_mut().as_mut().unwrap().insert(did, (fqn, kind));
149+
cx.renderinfo.borrow_mut().external_paths.insert(did, (fqn, kind));
150150
}
151151
}
152152

@@ -295,7 +295,7 @@ pub fn build_impl(cx: &DocContext,
295295
tcx: &TyCtxt,
296296
did: DefId,
297297
ret: &mut Vec<clean::Item>) {
298-
if !cx.inlined.borrow_mut().as_mut().unwrap().insert(did) {
298+
if !cx.renderinfo.borrow_mut().inlined.insert(did) {
299299
return
300300
}
301301

src/librustdoc/clean/mod.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use syntax::ptr::P;
3535

3636
use rustc_trans::back::link;
3737
use rustc::middle::cstore::{self, CrateStore};
38+
use rustc::middle::privacy::AccessLevels;
3839
use rustc::hir::def::Def;
3940
use rustc::hir::def_id::{DefId, DefIndex};
4041
use rustc::ty::subst::{self, ParamSpace, VecPerParamSpace};
@@ -46,8 +47,10 @@ use rustc::hir;
4647
use std::collections::{HashMap, HashSet};
4748
use std::path::PathBuf;
4849
use std::rc::Rc;
50+
use std::sync::Arc;
4951
use std::u32;
5052
use std::env::current_dir;
53+
use std::mem;
5154

5255
use core::DocContext;
5356
use doctree;
@@ -112,13 +115,16 @@ impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
112115
}
113116
}
114117

115-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
118+
#[derive(Clone, Debug)]
116119
pub struct Crate {
117120
pub name: String,
118121
pub src: PathBuf,
119122
pub module: Option<Item>,
120123
pub externs: Vec<(ast::CrateNum, ExternalCrate)>,
121124
pub primitives: Vec<PrimitiveType>,
125+
pub access_levels: Arc<AccessLevels<DefId>>,
126+
// These are later on moved into `CACHEKEY`, leaving the map empty.
127+
// Only here so that they can be filtered through the rustdoc passes.
122128
pub external_traits: HashMap<DefId, Trait>,
123129
}
124130

@@ -130,6 +136,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
130136

131137
if let Some(t) = cx.tcx_opt() {
132138
cx.deref_trait_did.set(t.lang_items.deref_trait());
139+
cx.renderinfo.borrow_mut().deref_trait_did = cx.deref_trait_did.get();
133140
}
134141

135142
let mut externs = Vec::new();
@@ -204,14 +211,17 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
204211
Input::Str { ref name, .. } => PathBuf::from(name.clone()),
205212
};
206213

214+
let mut access_levels = cx.access_levels.borrow_mut();
215+
let mut external_traits = cx.external_traits.borrow_mut();
216+
207217
Crate {
208218
name: name.to_string(),
209219
src: src,
210220
module: Some(module),
211221
externs: externs,
212222
primitives: primitives,
213-
external_traits: cx.external_traits.borrow_mut().take()
214-
.unwrap_or(HashMap::new()),
223+
access_levels: Arc::new(mem::replace(&mut access_levels, Default::default())),
224+
external_traits: mem::replace(&mut external_traits, Default::default()),
215225
}
216226
}
217227
}
@@ -540,8 +550,7 @@ impl Clean<TyParam> for hir::TyParam {
540550

541551
impl<'tcx> Clean<TyParam> for ty::TypeParameterDef<'tcx> {
542552
fn clean(&self, cx: &DocContext) -> TyParam {
543-
cx.external_typarams.borrow_mut().as_mut().unwrap()
544-
.insert(self.def_id, self.name.clean(cx));
553+
cx.renderinfo.borrow_mut().external_typarams.insert(self.def_id, self.name.clean(cx));
545554
TyParam {
546555
name: self.name.clean(cx),
547556
did: self.def_id,
@@ -2668,7 +2677,7 @@ fn register_def(cx: &DocContext, def: Def) -> DefId {
26682677
inline::record_extern_fqn(cx, did, kind);
26692678
if let TypeTrait = kind {
26702679
let t = inline::build_external_trait(cx, tcx, did);
2671-
cx.external_traits.borrow_mut().as_mut().unwrap().insert(did, t);
2680+
cx.external_traits.borrow_mut().insert(did, t);
26722681
}
26732682
did
26742683
}

src/librustdoc/core.rs

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ use syntax::feature_gate::UnstableFeatures;
2929
use syntax::parse::token;
3030

3131
use std::cell::{RefCell, Cell};
32-
use std::collections::{HashMap, HashSet};
32+
use std::collections::HashMap;
3333
use std::rc::Rc;
3434

3535
use visit_ast::RustdocVisitor;
3636
use clean;
3737
use clean::Clean;
38+
use html::render::RenderInfo;
3839

3940
pub use rustc::session::config::Input;
4041
pub use rustc::session::search_paths::SearchPaths;
@@ -45,19 +46,20 @@ pub enum MaybeTyped<'a, 'tcx: 'a> {
4546
NotTyped(&'a session::Session)
4647
}
4748

48-
pub type ExternalPaths = RefCell<Option<HashMap<DefId,
49-
(Vec<String>, clean::TypeKind)>>>;
49+
pub type ExternalPaths = HashMap<DefId, (Vec<String>, clean::TypeKind)>;
5050

5151
pub struct DocContext<'a, 'tcx: 'a> {
5252
pub map: &'a hir_map::Map<'tcx>,
5353
pub maybe_typed: MaybeTyped<'a, 'tcx>,
5454
pub input: Input,
55-
pub external_paths: ExternalPaths,
56-
pub external_traits: RefCell<Option<HashMap<DefId, clean::Trait>>>,
57-
pub external_typarams: RefCell<Option<HashMap<DefId, String>>>,
58-
pub inlined: RefCell<Option<HashSet<DefId>>>,
5955
pub all_crate_impls: RefCell<HashMap<ast::CrateNum, Vec<clean::Item>>>,
56+
// Later on moved into `clean::Crate`
57+
pub access_levels: RefCell<AccessLevels<DefId>>,
58+
// Later on moved into `html::render::CACHE_KEY`
59+
pub renderinfo: RefCell<RenderInfo>,
6060
pub deref_trait_did: Cell<Option<DefId>>,
61+
// Later on moved through `clean::Crate` into `html::render::CACHE_KEY`
62+
pub external_traits: RefCell<HashMap<DefId, clean::Trait>>,
6163
}
6264

6365
impl<'b, 'tcx> DocContext<'b, 'tcx> {
@@ -81,20 +83,14 @@ impl<'b, 'tcx> DocContext<'b, 'tcx> {
8183
}
8284
}
8385

84-
pub struct CrateAnalysis {
85-
pub access_levels: AccessLevels<DefId>,
86-
pub external_paths: ExternalPaths,
87-
pub external_typarams: RefCell<Option<HashMap<DefId, String>>>,
88-
pub inlined: RefCell<Option<HashSet<DefId>>>,
89-
pub deref_trait_did: Option<DefId>,
90-
}
91-
9286
pub type Externs = HashMap<String, Vec<String>>;
9387

94-
pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
95-
input: Input, triple: Option<String>)
96-
-> (clean::Crate, CrateAnalysis) {
97-
88+
pub fn run_core(search_paths: SearchPaths,
89+
cfgs: Vec<String>,
90+
externs: Externs,
91+
input: Input,
92+
triple: Option<String>) -> (clean::Crate, RenderInfo)
93+
{
9894
// Parse, resolve, and typecheck the given crate.
9995

10096
let cpath = match input {
@@ -148,7 +144,7 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
148144
let arenas = ty::CtxtArenas::new();
149145
let hir_map = driver::make_map(&sess, &mut hir_forest);
150146

151-
let krate_and_analysis = abort_on_err(driver::phase_3_run_analysis_passes(&sess,
147+
abort_on_err(driver::phase_3_run_analysis_passes(&sess,
152148
&cstore,
153149
hir_map,
154150
&arenas,
@@ -175,42 +171,20 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
175171
map: &tcx.map,
176172
maybe_typed: Typed(tcx),
177173
input: input,
178-
external_traits: RefCell::new(Some(HashMap::new())),
179-
external_typarams: RefCell::new(Some(HashMap::new())),
180-
external_paths: RefCell::new(Some(HashMap::new())),
181-
inlined: RefCell::new(Some(HashSet::new())),
182174
all_crate_impls: RefCell::new(HashMap::new()),
183175
deref_trait_did: Cell::new(None),
176+
access_levels: RefCell::new(access_levels),
177+
external_traits: RefCell::new(HashMap::new()),
178+
renderinfo: RefCell::new(Default::default()),
184179
};
185180
debug!("crate: {:?}", ctxt.map.krate());
186181

187-
let mut analysis = CrateAnalysis {
188-
access_levels: access_levels,
189-
external_paths: RefCell::new(None),
190-
external_typarams: RefCell::new(None),
191-
inlined: RefCell::new(None),
192-
deref_trait_did: None,
193-
};
194-
195182
let krate = {
196-
let mut v = RustdocVisitor::new(&ctxt, Some(&analysis));
183+
let mut v = RustdocVisitor::new(&ctxt);
197184
v.visit(ctxt.map.krate());
198185
v.clean(&ctxt)
199186
};
200187

201-
let external_paths = ctxt.external_paths.borrow_mut().take();
202-
*analysis.external_paths.borrow_mut() = external_paths;
203-
204-
let map = ctxt.external_typarams.borrow_mut().take();
205-
*analysis.external_typarams.borrow_mut() = map;
206-
207-
let map = ctxt.inlined.borrow_mut().take();
208-
*analysis.inlined.borrow_mut() = map;
209-
210-
analysis.deref_trait_did = ctxt.deref_trait_did.get();
211-
212-
Some((krate, analysis))
213-
}), &sess);
214-
215-
krate_and_analysis.unwrap()
188+
Some((krate, ctxt.renderinfo.into_inner()))
189+
}), &sess).unwrap()
216190
}

src/librustdoc/html/render.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub struct Cache {
253253
parent_is_trait_impl: bool,
254254
search_index: Vec<IndexItem>,
255255
stripped_mod: bool,
256-
access_levels: AccessLevels<DefId>,
256+
access_levels: Arc<AccessLevels<DefId>>,
257257
deref_trait_did: Option<DefId>,
258258

259259
// In rare case where a structure is defined in one module but implemented
@@ -264,6 +264,16 @@ pub struct Cache {
264264
orphan_methods: Vec<(DefId, clean::Item)>,
265265
}
266266

267+
/// Temporary storage for data obtained during `RustdocVisitor::clean()`.
268+
/// Later on moved into `CACHE_KEY`.
269+
#[derive(Default)]
270+
pub struct RenderInfo {
271+
pub inlined: HashSet<DefId>,
272+
pub external_paths: ::core::ExternalPaths,
273+
pub external_typarams: HashMap<DefId, String>,
274+
pub deref_trait_did: Option<DefId>,
275+
}
276+
267277
/// Helper struct to render all source code to HTML pages
268278
struct SourceCollector<'a> {
269279
scx: &'a mut SharedContext,
@@ -415,7 +425,8 @@ pub fn run(mut krate: clean::Crate,
415425
external_html: &ExternalHtml,
416426
dst: PathBuf,
417427
passes: HashSet<String>,
418-
css_file_extension: Option<PathBuf>) -> Result<(), Error> {
428+
css_file_extension: Option<PathBuf>,
429+
renderinfo: RenderInfo) -> Result<(), Error> {
419430
let src_root = match krate.src.parent() {
420431
Some(p) => p.to_path_buf(),
421432
None => PathBuf::new(),
@@ -482,19 +493,20 @@ pub fn run(mut krate: clean::Crate,
482493
};
483494

484495
// Crawl the crate to build various caches used for the output
485-
let analysis = ::ANALYSISKEY.with(|a| a.clone());
486-
let analysis = analysis.borrow();
487-
let access_levels = analysis.as_ref().map(|a| a.access_levels.clone());
488-
let access_levels = access_levels.unwrap_or(Default::default());
489-
let paths: HashMap<DefId, (Vec<String>, ItemType)> =
490-
analysis.as_ref().map(|a| {
491-
let paths = a.external_paths.borrow_mut().take().unwrap();
492-
paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from_type_kind(t)))).collect()
493-
}).unwrap_or(HashMap::new());
496+
let RenderInfo {
497+
inlined,
498+
external_paths,
499+
external_typarams,
500+
deref_trait_did,
501+
} = renderinfo;
502+
503+
let paths = external_paths.into_iter()
504+
.map(|(k, (v, t))| (k, (v, ItemType::from_type_kind(t))))
505+
.collect::<HashMap<_, _>>();
506+
494507
let mut cache = Cache {
495508
impls: HashMap::new(),
496-
external_paths: paths.iter().map(|(&k, v)| (k, v.0.clone()))
497-
.collect(),
509+
external_paths: paths.iter().map(|(&k, v)| (k, v.0.clone())).collect(),
498510
paths: paths,
499511
implementors: HashMap::new(),
500512
stack: Vec::new(),
@@ -504,16 +516,12 @@ pub fn run(mut krate: clean::Crate,
504516
extern_locations: HashMap::new(),
505517
primitive_locations: HashMap::new(),
506518
stripped_mod: false,
507-
access_levels: access_levels,
519+
access_levels: krate.access_levels.clone(),
508520
orphan_methods: Vec::new(),
509521
traits: mem::replace(&mut krate.external_traits, HashMap::new()),
510-
deref_trait_did: analysis.as_ref().and_then(|a| a.deref_trait_did),
511-
typarams: analysis.as_ref().map(|a| {
512-
a.external_typarams.borrow_mut().take().unwrap()
513-
}).unwrap_or(HashMap::new()),
514-
inlined: analysis.as_ref().map(|a| {
515-
a.inlined.borrow_mut().take().unwrap()
516-
}).unwrap_or(HashSet::new()),
522+
deref_trait_did: deref_trait_did,
523+
typarams: external_typarams,
524+
inlined: inlined,
517525
};
518526

519527
// Cache where all our extern crates are located

0 commit comments

Comments
 (0)