Skip to content

Commit 9fd5a67

Browse files
Remove CACHE_KEY global
1 parent 78e2206 commit 9fd5a67

File tree

18 files changed

+728
-663
lines changed

18 files changed

+728
-663
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ crate fn build_impl(
368368
// Only inline impl if the implementing type is
369369
// reachable in rustdoc generated documentation
370370
if !did.is_local() {
371-
if let Some(did) = for_.def_id() {
371+
if let Some(did) = for_.def_id(&cx.cache) {
372372
if !cx.renderinfo.borrow().access_levels.is_public(did) {
373373
return;
374374
}
@@ -410,19 +410,19 @@ crate fn build_impl(
410410
clean::GenericBound::TraitBound(polyt, _) => polyt.trait_,
411411
clean::GenericBound::Outlives(..) => unreachable!(),
412412
});
413-
if trait_.def_id() == tcx.lang_items().deref_trait() {
413+
if trait_.def_id(&cx.cache) == tcx.lang_items().deref_trait() {
414414
super::build_deref_target_impls(cx, &trait_items, ret);
415415
}
416-
if let Some(trait_did) = trait_.def_id() {
416+
if let Some(trait_did) = trait_.def_id(&cx.cache) {
417417
record_extern_trait(cx, trait_did);
418418
}
419419

420420
let provided = trait_
421-
.def_id()
421+
.def_id(&cx.cache)
422422
.map(|did| tcx.provided_trait_methods(did).map(|meth| meth.ident.name).collect())
423423
.unwrap_or_default();
424424

425-
debug!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id());
425+
debug!("build_impl: impl {:?} for {:?}", trait_.def_id(&cx.cache), for_.def_id(&cx.cache));
426426

427427
let mut item = clean::Item::from_def_id_and_parts(
428428
did,

src/librustdoc/clean/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,17 +2089,17 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &DocContext<'_>) ->
20892089

20902090
// If this impl block is an implementation of the Deref trait, then we
20912091
// need to try inlining the target's inherent impl blocks as well.
2092-
if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
2092+
if trait_.def_id(&cx.cache) == cx.tcx.lang_items().deref_trait() {
20932093
build_deref_target_impls(cx, &items, &mut ret);
20942094
}
20952095

20962096
let provided: FxHashSet<Symbol> = trait_
2097-
.def_id()
2097+
.def_id(&cx.cache)
20982098
.map(|did| cx.tcx.provided_trait_methods(did).map(|meth| meth.ident.name).collect())
20992099
.unwrap_or_default();
21002100

21012101
let for_ = impl_.self_ty.clean(cx);
2102-
let type_alias = for_.def_id().and_then(|did| match cx.tcx.def_kind(did) {
2102+
let type_alias = for_.def_id(&cx.cache).and_then(|did| match cx.tcx.def_kind(did) {
21032103
DefKind::TyAlias => Some(cx.tcx.type_of(did).clean(cx)),
21042104
_ => None,
21052105
});
@@ -2329,14 +2329,14 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
23292329
if matchers.len() <= 1 {
23302330
format!(
23312331
"{}macro {}{} {{\n ...\n}}",
2332-
vis.print_with_space(cx.tcx, def_id),
2332+
vis.print_with_space(cx.tcx, def_id, &cx.cache),
23332333
name,
23342334
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
23352335
)
23362336
} else {
23372337
format!(
23382338
"{}macro {} {{\n{}}}",
2339-
vis.print_with_space(cx.tcx, def_id),
2339+
vis.print_with_space(cx.tcx, def_id, &cx.cache),
23402340
name,
23412341
matchers
23422342
.iter()

src/librustdoc/clean/types.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ use crate::clean::inline;
3737
use crate::clean::types::Type::{QPath, ResolvedPath};
3838
use crate::clean::Clean;
3939
use crate::core::DocContext;
40-
use crate::formats::cache::cache;
40+
use crate::doctree;
41+
use crate::formats::cache::Cache;
4142
use crate::formats::item_type::ItemType;
4243
use crate::html::render::cache::ExternalLocation;
4344

@@ -169,8 +170,8 @@ impl Item {
169170
self.attrs.collapsed_doc_value()
170171
}
171172

172-
crate fn links(&self) -> Vec<RenderedLink> {
173-
self.attrs.links(&self.def_id.krate)
173+
crate fn links(&self, cache: &Cache) -> Vec<RenderedLink> {
174+
self.attrs.links(&self.def_id.krate, cache)
174175
}
175176

176177
crate fn is_crate(&self) -> bool {
@@ -826,7 +827,7 @@ impl Attributes {
826827
/// Gets links as a vector
827828
///
828829
/// Cache must be populated before call
829-
crate fn links(&self, krate: &CrateNum) -> Vec<RenderedLink> {
830+
crate fn links(&self, krate: &CrateNum, cache: &Cache) -> Vec<RenderedLink> {
830831
use crate::html::format::href;
831832
use crate::html::render::CURRENT_DEPTH;
832833

@@ -835,7 +836,7 @@ impl Attributes {
835836
.filter_map(|ItemLink { link: s, link_text, did, fragment }| {
836837
match *did {
837838
Some(did) => {
838-
if let Some((mut href, ..)) = href(did) {
839+
if let Some((mut href, ..)) = href(did, cache) {
839840
if let Some(ref fragment) = *fragment {
840841
href.push('#');
841842
href.push_str(fragment);
@@ -851,7 +852,6 @@ impl Attributes {
851852
}
852853
None => {
853854
if let Some(ref fragment) = *fragment {
854-
let cache = cache();
855855
let url = match cache.extern_locations.get(krate) {
856856
Some(&(_, _, ExternalLocation::Local)) => {
857857
let depth = CURRENT_DEPTH.with(|l| l.get());
@@ -958,7 +958,7 @@ impl GenericBound {
958958
crate fn is_sized_bound(&self, cx: &DocContext<'_>) -> bool {
959959
use rustc_hir::TraitBoundModifier as TBM;
960960
if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
961-
if trait_.def_id() == cx.tcx.lang_items().sized_trait() {
961+
if trait_.def_id(&cx.cache) == cx.tcx.lang_items().sized_trait() {
962962
return true;
963963
}
964964
}
@@ -1171,9 +1171,9 @@ crate enum FnRetTy {
11711171
}
11721172

11731173
impl GetDefId for FnRetTy {
1174-
fn def_id(&self) -> Option<DefId> {
1174+
fn def_id(&self, cache: &Cache) -> Option<DefId> {
11751175
match *self {
1176-
Return(ref ty) => ty.def_id(),
1176+
Return(ref ty) => ty.def_id(cache),
11771177
DefaultReturn => None,
11781178
}
11791179
}
@@ -1299,12 +1299,12 @@ crate enum TypeKind {
12991299
}
13001300

13011301
crate trait GetDefId {
1302-
fn def_id(&self) -> Option<DefId>;
1302+
fn def_id(&self, cache: &Cache) -> Option<DefId>;
13031303
}
13041304

13051305
impl<T: GetDefId> GetDefId for Option<T> {
1306-
fn def_id(&self) -> Option<DefId> {
1307-
self.as_ref().and_then(|d| d.def_id())
1306+
fn def_id(&self, cache: &Cache) -> Option<DefId> {
1307+
self.as_ref().and_then(|d| d.def_id(cache))
13081308
}
13091309
}
13101310

@@ -1394,27 +1394,27 @@ impl Type {
13941394
}
13951395

13961396
impl GetDefId for Type {
1397-
fn def_id(&self) -> Option<DefId> {
1397+
fn def_id(&self, cache: &Cache) -> Option<DefId> {
13981398
match *self {
13991399
ResolvedPath { did, .. } => Some(did),
1400-
Primitive(p) => cache().primitive_locations.get(&p).cloned(),
1400+
Primitive(p) => cache.primitive_locations.get(&p).cloned(),
14011401
BorrowedRef { type_: box Generic(..), .. } => {
1402-
Primitive(PrimitiveType::Reference).def_id()
1402+
Primitive(PrimitiveType::Reference).def_id(cache)
14031403
}
1404-
BorrowedRef { ref type_, .. } => type_.def_id(),
1404+
BorrowedRef { ref type_, .. } => type_.def_id(cache),
14051405
Tuple(ref tys) => {
14061406
if tys.is_empty() {
1407-
Primitive(PrimitiveType::Unit).def_id()
1407+
Primitive(PrimitiveType::Unit).def_id(cache)
14081408
} else {
1409-
Primitive(PrimitiveType::Tuple).def_id()
1409+
Primitive(PrimitiveType::Tuple).def_id(cache)
14101410
}
14111411
}
1412-
BareFunction(..) => Primitive(PrimitiveType::Fn).def_id(),
1413-
Never => Primitive(PrimitiveType::Never).def_id(),
1414-
Slice(..) => Primitive(PrimitiveType::Slice).def_id(),
1415-
Array(..) => Primitive(PrimitiveType::Array).def_id(),
1416-
RawPointer(..) => Primitive(PrimitiveType::RawPointer).def_id(),
1417-
QPath { ref self_type, .. } => self_type.def_id(),
1412+
BareFunction(..) => Primitive(PrimitiveType::Fn).def_id(cache),
1413+
Never => Primitive(PrimitiveType::Never).def_id(cache),
1414+
Slice(..) => Primitive(PrimitiveType::Slice).def_id(cache),
1415+
Array(..) => Primitive(PrimitiveType::Array).def_id(cache),
1416+
RawPointer(..) => Primitive(PrimitiveType::RawPointer).def_id(cache),
1417+
QPath { ref self_type, .. } => self_type.def_id(cache),
14181418
_ => None,
14191419
}
14201420
}
@@ -1814,8 +1814,8 @@ crate struct Typedef {
18141814
}
18151815

18161816
impl GetDefId for Typedef {
1817-
fn def_id(&self) -> Option<DefId> {
1818-
self.type_.def_id()
1817+
fn def_id(&self, cache: &Cache) -> Option<DefId> {
1818+
self.type_.def_id(cache)
18191819
}
18201820
}
18211821

src/librustdoc/clean/utils.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,11 @@ crate fn get_real_types(
177177
return res;
178178
}
179179
if arg.is_full_generic() {
180-
let arg_s = Symbol::intern(&arg.print().to_string());
180+
let arg_s = Symbol::intern(&arg.print(&cx.cache).to_string());
181181
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
182-
WherePredicate::BoundPredicate { ty, .. } => ty.def_id() == arg.def_id(),
182+
WherePredicate::BoundPredicate { ty, .. } => {
183+
ty.def_id(&cx.cache) == arg.def_id(&cx.cache)
184+
}
183185
_ => false,
184186
}) {
185187
let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
@@ -195,7 +197,7 @@ crate fn get_real_types(
195197
res.extend(adds);
196198
} else if !ty.is_full_generic() {
197199
if let Some(kind) =
198-
ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx))
200+
ty.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
199201
{
200202
res.insert((ty, kind));
201203
}
@@ -212,15 +214,17 @@ crate fn get_real_types(
212214
if !adds.is_empty() {
213215
res.extend(adds);
214216
} else if !ty.is_full_generic() {
215-
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
217+
if let Some(kind) =
218+
ty.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
219+
{
216220
res.insert((ty.clone(), kind));
217221
}
218222
}
219223
}
220224
}
221225
}
222226
} else {
223-
if let Some(kind) = arg.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
227+
if let Some(kind) = arg.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx)) {
224228
res.insert((arg.clone(), kind));
225229
}
226230
if let Some(gens) = arg.generics() {
@@ -230,7 +234,9 @@ crate fn get_real_types(
230234
if !adds.is_empty() {
231235
res.extend(adds);
232236
}
233-
} else if let Some(kind) = gen.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
237+
} else if let Some(kind) =
238+
gen.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
239+
{
234240
res.insert((gen.clone(), kind));
235241
}
236242
}
@@ -257,7 +263,9 @@ crate fn get_all_types(
257263
if !args.is_empty() {
258264
all_types.extend(args);
259265
} else {
260-
if let Some(kind) = arg.type_.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
266+
if let Some(kind) =
267+
arg.type_.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
268+
{
261269
all_types.insert((arg.type_.clone(), kind));
262270
}
263271
}
@@ -267,7 +275,9 @@ crate fn get_all_types(
267275
FnRetTy::Return(ref return_type) => {
268276
let mut ret = get_real_types(generics, &return_type, cx, 0);
269277
if ret.is_empty() {
270-
if let Some(kind) = return_type.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
278+
if let Some(kind) =
279+
return_type.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
280+
{
271281
ret.insert((return_type.clone(), kind));
272282
}
273283
}
@@ -473,7 +483,7 @@ crate fn resolve_type(cx: &DocContext<'_>, path: Path, id: hir::HirId) -> Type {
473483
return Generic(kw::SelfUpper);
474484
}
475485
Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => {
476-
return Generic(Symbol::intern(&format!("{:#}", path.print())));
486+
return Generic(Symbol::intern(&format!("{:#}", path.print(&cx.cache))));
477487
}
478488
Res::SelfTy(..) | Res::Def(DefKind::TyParam | DefKind::AssocTy, _) => true,
479489
_ => false,

src/librustdoc/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ crate struct RenderOptions {
261261
}
262262

263263
/// Temporary storage for data obtained during `RustdocVisitor::clean()`.
264-
/// Later on moved into `CACHE_KEY`.
264+
/// Later on moved into `cache`.
265265
#[derive(Default, Clone)]
266266
crate struct RenderInfo {
267267
crate inlined: FxHashSet<DefId>,

src/librustdoc/core.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::clean;
3232
use crate::clean::{AttributesExt, MAX_DEF_ID};
3333
use crate::config::{Options as RustdocOptions, RenderOptions};
3434
use crate::config::{OutputFormat, RenderInfo};
35+
use crate::formats::cache::Cache;
3536
use crate::passes::{self, Condition::*, ConditionalPass};
3637

3738
crate use rustc_session::config::{DebuggingOptions, Input, Options};
@@ -45,9 +46,9 @@ crate struct DocContext<'tcx> {
4546
///
4647
/// Most of this logic is copied from rustc_lint::late.
4748
crate param_env: Cell<ParamEnv<'tcx>>,
48-
/// Later on moved into `CACHE_KEY`
49+
/// Later on moved into `cache`
4950
crate renderinfo: RefCell<RenderInfo>,
50-
/// Later on moved through `clean::Crate` into `CACHE_KEY`
51+
/// Later on moved through `clean::Crate` into `cache`
5152
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
5253
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
5354
/// the same time.
@@ -75,6 +76,8 @@ crate struct DocContext<'tcx> {
7576
/// See `collect_intra_doc_links::traits_implemented_by` for more details.
7677
/// `map<module, set<trait>>`
7778
crate module_trait_cache: RefCell<FxHashMap<DefId, FxHashSet<DefId>>>,
79+
/// Fake empty cache used when cache is required as parameter.
80+
crate cache: Cache,
7881
}
7982

8083
impl<'tcx> DocContext<'tcx> {
@@ -524,6 +527,7 @@ crate fn run_global_ctxt(
524527
.collect(),
525528
render_options,
526529
module_trait_cache: RefCell::new(FxHashMap::default()),
530+
cache: Cache::default(),
527531
};
528532
debug!("crate: {:?}", tcx.hir().krate());
529533

0 commit comments

Comments
 (0)