Skip to content

Commit 19630ea

Browse files
Remove cache usage wherever possible
1 parent c448270 commit 19630ea

File tree

12 files changed

+179
-166
lines changed

12 files changed

+179
-166
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(&cx.cache) {
371+
if let Some(did) = for_.def_id() {
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(&cx.cache) == tcx.lang_items().deref_trait() {
413+
if trait_.def_id() == 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(&cx.cache) {
416+
if let Some(trait_did) = trait_.def_id() {
417417
record_extern_trait(cx, trait_did);
418418
}
419419

420420
let provided = trait_
421-
.def_id(&cx.cache)
421+
.def_id()
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(&cx.cache), for_.def_id(&cx.cache));
425+
debug!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id());
426426

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

src/librustdoc/clean/mod.rs

Lines changed: 3 additions & 3 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.cache) == cx.tcx.lang_items().deref_trait() {
2092+
if trait_.def_id() == 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(&cx.cache)
2097+
.def_id()
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(&cx.cache).and_then(|did| match cx.tcx.def_kind(did) {
2102+
let type_alias = for_.def_id().and_then(|did| match cx.tcx.def_kind(did) {
21032103
DefKind::TyAlias => Some(cx.tcx.type_of(did).clean(cx)),
21042104
_ => None,
21052105
});

src/librustdoc/clean/types.rs

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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.cache) == cx.tcx.lang_items().sized_trait() {
961+
if trait_.def_id() == cx.tcx.lang_items().sized_trait() {
962962
return true;
963963
}
964964
}
@@ -1171,9 +1171,16 @@ crate enum FnRetTy {
11711171
}
11721172

11731173
impl GetDefId for FnRetTy {
1174-
fn def_id(&self, cache: &Cache) -> Option<DefId> {
1174+
fn def_id(&self) -> Option<DefId> {
11751175
match *self {
1176-
Return(ref ty) => ty.def_id(cache),
1176+
Return(ref ty) => ty.def_id(),
1177+
DefaultReturn => None,
1178+
}
1179+
}
1180+
1181+
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1182+
match *self {
1183+
Return(ref ty) => ty.def_id_full(cache),
11771184
DefaultReturn => None,
11781185
}
11791186
}
@@ -1299,12 +1306,20 @@ crate enum TypeKind {
12991306
}
13001307

13011308
crate trait GetDefId {
1302-
fn def_id(&self, cache: &Cache) -> Option<DefId>;
1309+
/// Doesn't retrieve primitive types `DefId`. Use `def_id_full` if you want it.
1310+
fn def_id(&self) -> Option<DefId>;
1311+
/// Retrieves all types' `DefId` (including primitives). If you're not interested about
1312+
/// primitives, use `def_id`.
1313+
fn def_id_full(&self, cache: &Cache) -> Option<DefId>;
13031314
}
13041315

13051316
impl<T: GetDefId> GetDefId for Option<T> {
1306-
fn def_id(&self, cache: &Cache) -> Option<DefId> {
1307-
self.as_ref().and_then(|d| d.def_id(cache))
1317+
fn def_id(&self) -> Option<DefId> {
1318+
self.as_ref().and_then(|d| d.def_id())
1319+
}
1320+
1321+
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1322+
self.as_ref().and_then(|d| d.def_id_full(cache))
13081323
}
13091324
}
13101325

@@ -1393,33 +1408,50 @@ impl Type {
13931408
}
13941409
}
13951410

1396-
impl GetDefId for Type {
1397-
fn def_id(&self, cache: &Cache) -> Option<DefId> {
1411+
impl Type {
1412+
fn inner_def_id(&self, cache: Option<&Cache>) -> Option<DefId> {
1413+
fn inner<T: GetDefId>(t: &T, cache: Option<&Cache>) -> Option<DefId> {
1414+
match cache {
1415+
Some(c) => t.def_id_full(c),
1416+
None => t.def_id(),
1417+
}
1418+
}
1419+
13981420
match *self {
13991421
ResolvedPath { did, .. } => Some(did),
1400-
Primitive(p) => cache.primitive_locations.get(&p).cloned(),
1422+
Primitive(p) => cache.and_then(|c| c.primitive_locations.get(&p).cloned()),
14011423
BorrowedRef { type_: box Generic(..), .. } => {
1402-
Primitive(PrimitiveType::Reference).def_id(cache)
1424+
inner(&Primitive(PrimitiveType::Reference), cache)
14031425
}
1404-
BorrowedRef { ref type_, .. } => type_.def_id(cache),
1426+
BorrowedRef { ref type_, .. } => inner(&**type_, cache),
14051427
Tuple(ref tys) => {
14061428
if tys.is_empty() {
1407-
Primitive(PrimitiveType::Unit).def_id(cache)
1429+
inner(&Primitive(PrimitiveType::Unit), cache)
14081430
} else {
1409-
Primitive(PrimitiveType::Tuple).def_id(cache)
1431+
inner(&Primitive(PrimitiveType::Tuple), cache)
14101432
}
14111433
}
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),
1434+
BareFunction(..) => inner(&Primitive(PrimitiveType::Fn), cache),
1435+
Never => inner(&Primitive(PrimitiveType::Never), cache),
1436+
Slice(..) => inner(&Primitive(PrimitiveType::Slice), cache),
1437+
Array(..) => inner(&Primitive(PrimitiveType::Array), cache),
1438+
RawPointer(..) => inner(&Primitive(PrimitiveType::RawPointer), cache),
1439+
QPath { ref self_type, .. } => inner(&**self_type, cache),
14181440
_ => None,
14191441
}
14201442
}
14211443
}
14221444

1445+
impl GetDefId for Type {
1446+
fn def_id(&self) -> Option<DefId> {
1447+
self.inner_def_id(None)
1448+
}
1449+
1450+
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1451+
self.inner_def_id(Some(cache))
1452+
}
1453+
}
1454+
14231455
impl PrimitiveType {
14241456
crate fn from_hir(prim: hir::PrimTy) -> PrimitiveType {
14251457
match prim {
@@ -1814,8 +1846,12 @@ crate struct Typedef {
18141846
}
18151847

18161848
impl GetDefId for Typedef {
1817-
fn def_id(&self, cache: &Cache) -> Option<DefId> {
1818-
self.type_.def_id(cache)
1849+
fn def_id(&self) -> Option<DefId> {
1850+
self.type_.def_id()
1851+
}
1852+
1853+
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1854+
self.type_.def_id_full(cache)
18191855
}
18201856
}
18211857

src/librustdoc/clean/utils.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ crate fn get_real_types(
179179
if arg.is_full_generic() {
180180
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, .. } => {
183-
ty.def_id(&cx.cache) == arg.def_id(&cx.cache)
184-
}
182+
WherePredicate::BoundPredicate { ty, .. } => ty.def_id() == arg.def_id(),
185183
_ => false,
186184
}) {
187185
let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
@@ -197,7 +195,7 @@ crate fn get_real_types(
197195
res.extend(adds);
198196
} else if !ty.is_full_generic() {
199197
if let Some(kind) =
200-
ty.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
198+
ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx))
201199
{
202200
res.insert((ty, kind));
203201
}
@@ -214,17 +212,15 @@ crate fn get_real_types(
214212
if !adds.is_empty() {
215213
res.extend(adds);
216214
} else if !ty.is_full_generic() {
217-
if let Some(kind) =
218-
ty.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
219-
{
215+
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
220216
res.insert((ty.clone(), kind));
221217
}
222218
}
223219
}
224220
}
225221
}
226222
} else {
227-
if let Some(kind) = arg.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx)) {
223+
if let Some(kind) = arg.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
228224
res.insert((arg.clone(), kind));
229225
}
230226
if let Some(gens) = arg.generics() {
@@ -234,9 +230,7 @@ crate fn get_real_types(
234230
if !adds.is_empty() {
235231
res.extend(adds);
236232
}
237-
} else if let Some(kind) =
238-
gen.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
239-
{
233+
} else if let Some(kind) = gen.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
240234
res.insert((gen.clone(), kind));
241235
}
242236
}
@@ -263,9 +257,7 @@ crate fn get_all_types(
263257
if !args.is_empty() {
264258
all_types.extend(args);
265259
} else {
266-
if let Some(kind) =
267-
arg.type_.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
268-
{
260+
if let Some(kind) = arg.type_.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
269261
all_types.insert((arg.type_.clone(), kind));
270262
}
271263
}
@@ -275,9 +267,7 @@ crate fn get_all_types(
275267
FnRetTy::Return(ref return_type) => {
276268
let mut ret = get_real_types(generics, &return_type, cx, 0);
277269
if ret.is_empty() {
278-
if let Some(kind) =
279-
return_type.def_id(&cx.cache).map(|did| cx.tcx.def_kind(did).clean(cx))
280-
{
270+
if let Some(kind) = return_type.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
281271
ret.insert((return_type.clone(), kind));
282272
}
283273
}

0 commit comments

Comments
 (0)