Skip to content

Commit b416e38

Browse files
committed
Auto merge of #90385 - mfrw:mfrw/librustdoc, r=GuillaumeGomez
rustdoc: use Type::def_id() instead of Type::def_id_no_primitives() For: #90187 r? `@jyn514`
2 parents d212d90 + 5cfc7ce commit b416e38

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

src/librustdoc/formats/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
303303
desc,
304304
parent,
305305
parent_idx: None,
306-
search_type: get_index_search_type(&item, self.tcx),
306+
search_type: get_index_search_type(&item, self.tcx, self.cache),
307307
aliases: item.attrs.get_doc_aliases(),
308308
});
309309
}

src/librustdoc/html/render/cache.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
4242
desc,
4343
parent: Some(did),
4444
parent_idx: None,
45-
search_type: get_index_search_type(item, tcx),
45+
search_type: get_index_search_type(item, tcx, cache),
4646
aliases: item.attrs.get_doc_aliases(),
4747
});
4848
}
@@ -194,11 +194,12 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
194194
crate fn get_index_search_type<'tcx>(
195195
item: &clean::Item,
196196
tcx: TyCtxt<'tcx>,
197+
cache: &Cache,
197198
) -> Option<IndexItemFunctionType> {
198199
let (mut inputs, mut output) = match *item.kind {
199-
clean::FunctionItem(ref f) => get_all_types(&f.generics, &f.decl, tcx),
200-
clean::MethodItem(ref m, _) => get_all_types(&m.generics, &m.decl, tcx),
201-
clean::TyMethodItem(ref m) => get_all_types(&m.generics, &m.decl, tcx),
200+
clean::FunctionItem(ref f) => get_all_types(&f.generics, &f.decl, tcx, cache),
201+
clean::MethodItem(ref m, _) => get_all_types(&m.generics, &m.decl, tcx, cache),
202+
clean::TyMethodItem(ref m) => get_all_types(&m.generics, &m.decl, tcx, cache),
202203
_ => return None,
203204
};
204205

@@ -254,12 +255,14 @@ crate fn get_real_types<'tcx>(
254255
tcx: TyCtxt<'tcx>,
255256
recurse: usize,
256257
res: &mut Vec<TypeWithKind>,
258+
cache: &Cache,
257259
) {
258260
fn insert_ty(
259261
res: &mut Vec<TypeWithKind>,
260262
tcx: TyCtxt<'_>,
261263
ty: Type,
262264
mut generics: Vec<TypeWithKind>,
265+
_cache: &Cache,
263266
) {
264267
let is_full_generic = ty.is_full_generic();
265268

@@ -350,23 +353,30 @@ crate fn get_real_types<'tcx>(
350353
continue;
351354
}
352355
if let Some(ty) = x.get_type() {
353-
get_real_types(generics, &ty, tcx, recurse + 1, &mut ty_generics);
356+
get_real_types(
357+
generics,
358+
&ty,
359+
tcx,
360+
recurse + 1,
361+
&mut ty_generics,
362+
cache,
363+
);
354364
}
355365
}
356366
}
357367
}
358-
insert_ty(res, tcx, arg.clone(), ty_generics);
368+
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
359369
}
360370
// Otherwise we check if the trait bounds are "inlined" like `T: Option<u32>`...
361371
if let Some(bound) = generics.params.iter().find(|g| g.is_type() && g.name == arg_s) {
362372
let mut ty_generics = Vec::new();
363373
for bound in bound.get_bounds().unwrap_or(&[]) {
364374
if let Some(path) = bound.get_trait_path() {
365375
let ty = Type::ResolvedPath { did: path.def_id(), path };
366-
get_real_types(generics, &ty, tcx, recurse + 1, &mut ty_generics);
376+
get_real_types(generics, &ty, tcx, recurse + 1, &mut ty_generics, cache);
367377
}
368378
}
369-
insert_ty(res, tcx, arg.clone(), ty_generics);
379+
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
370380
}
371381
} else {
372382
// This is not a type parameter. So for example if we have `T, U: Option<T>`, and we're
@@ -377,10 +387,10 @@ crate fn get_real_types<'tcx>(
377387
let mut ty_generics = Vec::new();
378388
if let Some(arg_generics) = arg.generics() {
379389
for gen in arg_generics.iter() {
380-
get_real_types(generics, gen, tcx, recurse + 1, &mut ty_generics);
390+
get_real_types(generics, gen, tcx, recurse + 1, &mut ty_generics, cache);
381391
}
382392
}
383-
insert_ty(res, tcx, arg.clone(), ty_generics);
393+
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
384394
}
385395
}
386396

@@ -392,6 +402,7 @@ crate fn get_all_types<'tcx>(
392402
generics: &Generics,
393403
decl: &FnDecl,
394404
tcx: TyCtxt<'tcx>,
405+
cache: &Cache,
395406
) -> (Vec<TypeWithKind>, Vec<TypeWithKind>) {
396407
let mut all_types = Vec::new();
397408
for arg in decl.inputs.values.iter() {
@@ -401,7 +412,7 @@ crate fn get_all_types<'tcx>(
401412
// FIXME: performance wise, it'd be much better to move `args` declaration outside of the
402413
// loop and replace this line with `args.clear()`.
403414
let mut args = Vec::new();
404-
get_real_types(generics, &arg.type_, tcx, 0, &mut args);
415+
get_real_types(generics, &arg.type_, tcx, 0, &mut args, cache);
405416
if !args.is_empty() {
406417
// FIXME: once back to performance improvements, replace this line with:
407418
// `all_types.extend(args.drain(..));`.
@@ -417,7 +428,7 @@ crate fn get_all_types<'tcx>(
417428
let mut ret_types = Vec::new();
418429
match decl.output {
419430
FnRetTy::Return(ref return_type) => {
420-
get_real_types(generics, return_type, tcx, 0, &mut ret_types);
431+
get_real_types(generics, return_type, tcx, 0, &mut ret_types, cache);
421432
if ret_types.is_empty() {
422433
if let Some(kind) =
423434
return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into())

src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ fn sidebar_deref_methods(
21662166
}
21672167

21682168
// Recurse into any further impls that might exist for `target`
2169-
if let Some(target_did) = target.def_id_no_primitives() {
2169+
if let Some(target_did) = target.def_id(c) {
21702170
if let Some(target_impls) = c.impls.get(&target_did) {
21712171
if let Some(target_deref_impl) = target_impls.iter().find(|i| {
21722172
i.inner_impl()

src/librustdoc/passes/collect_trait_impls.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ crate fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate
5757

5858
// Follow all `Deref` targets of included items and recursively add them as valid
5959
fn add_deref_target(
60+
cx: &DocContext<'_>,
6061
map: &FxHashMap<DefId, &Type>,
6162
cleaner: &mut BadImplStripper,
6263
type_did: DefId,
@@ -65,14 +66,14 @@ crate fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate
6566
debug!("add_deref_target: type {:?}, target {:?}", type_did, target);
6667
if let Some(target_prim) = target.primitive_type() {
6768
cleaner.prims.insert(target_prim);
68-
} else if let Some(target_did) = target.def_id_no_primitives() {
69+
} else if let Some(target_did) = target.def_id(&cx.cache) {
6970
// `impl Deref<Target = S> for S`
7071
if target_did == type_did {
7172
// Avoid infinite cycles
7273
return;
7374
}
7475
cleaner.items.insert(target_did.into());
75-
add_deref_target(map, cleaner, target_did);
76+
add_deref_target(cx, map, cleaner, target_did);
7677
}
7778
}
7879
}
@@ -102,7 +103,7 @@ crate fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate
102103
// `Deref` target type and the impl for type positions, this map of types is keyed by
103104
// `DefId` and for convenience uses a special cleaner that accepts `DefId`s directly.
104105
if cleaner.keep_impl_with_def_id(for_did.into()) {
105-
add_deref_target(&type_did_to_deref_target, &mut cleaner, for_did);
106+
add_deref_target(cx, &type_did_to_deref_target, &mut cleaner, for_did);
106107
}
107108
}
108109
}

0 commit comments

Comments
 (0)