Skip to content

Commit 08df8b8

Browse files
committed
Auto merge of #92711 - zredb:issue-90187-fix, r=notriddle
rustdoc: Remove `def_id_no_primitives` Fixes #90187.
2 parents 8c7f2bf + 5cc32e7 commit 08df8b8

File tree

7 files changed

+51
-49
lines changed

7 files changed

+51
-49
lines changed

src/librustdoc/clean/types.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ impl ExternalCrate {
338338
}
339339

340340
/// Indicates where an external crate can be found.
341+
#[derive(Debug)]
341342
crate enum ExternalLocation {
342343
/// Remote URL root of the external crate
343344
Remote(String),
@@ -1539,23 +1540,10 @@ impl Type {
15391540

15401541
/// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
15411542
///
1542-
/// See [`Self::def_id_no_primitives`] for more.
1543-
///
15441543
/// [clean]: crate::clean
15451544
crate fn def_id(&self, cache: &Cache) -> Option<DefId> {
15461545
self.inner_def_id(Some(cache))
15471546
}
1548-
1549-
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
1550-
/// This will return [`None`] when called on a primitive [`clean::Type`].
1551-
/// Use [`Self::def_id`] if you want to include primitives.
1552-
///
1553-
/// [`clean`]: crate::clean
1554-
/// [`clean::Type`]: crate::clean::Type
1555-
// FIXME: get rid of this function and always use `def_id`
1556-
crate fn def_id_no_primitives(&self) -> Option<DefId> {
1557-
self.inner_def_id(None)
1558-
}
15591547
}
15601548

15611549
/// A primitive (aka, builtin) type.

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_function_type_for_search(&item, self.tcx),
306+
search_type: get_function_type_for_search(&item, self.tcx, self.cache),
307307
aliases: item.attrs.get_doc_aliases(),
308308
});
309309
}

src/librustdoc/html/render/search_index.rs

+34-24
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::BTreeMap;
33

44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_middle::ty::TyCtxt;
6-
use rustc_span::symbol::Symbol;
6+
use rustc_span::symbol::{kw, Symbol};
77
use serde::ser::{Serialize, SerializeStruct, Serializer};
88

99
use crate::clean;
@@ -33,7 +33,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
3333
desc,
3434
parent: Some(did),
3535
parent_idx: None,
36-
search_type: get_function_type_for_search(item, tcx),
36+
search_type: get_function_type_for_search(item, tcx, &cache),
3737
aliases: item.attrs.get_doc_aliases(),
3838
});
3939
}
@@ -188,11 +188,12 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
188188
crate fn get_function_type_for_search<'tcx>(
189189
item: &clean::Item,
190190
tcx: TyCtxt<'tcx>,
191+
cache: &Cache,
191192
) -> Option<IndexItemFunctionType> {
192193
let (mut inputs, mut output) = match *item.kind {
193-
clean::FunctionItem(ref f) => get_fn_inputs_and_outputs(f, tcx),
194-
clean::MethodItem(ref m, _) => get_fn_inputs_and_outputs(m, tcx),
195-
clean::TyMethodItem(ref m) => get_fn_inputs_and_outputs(m, tcx),
194+
clean::FunctionItem(ref f) => get_fn_inputs_and_outputs(f, tcx, cache),
195+
clean::MethodItem(ref m, _) => get_fn_inputs_and_outputs(m, tcx, cache),
196+
clean::TyMethodItem(ref m) => get_fn_inputs_and_outputs(m, tcx, cache),
196197
_ => return None,
197198
};
198199

@@ -219,7 +220,8 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
219220
let path = &bounds[0].trait_;
220221
Some(path.segments.last().unwrap().name)
221222
}
222-
clean::Generic(s) => Some(s),
223+
// We return an empty name because we don't care about the generic name itself.
224+
clean::Generic(_) => Some(kw::Empty),
223225
clean::Primitive(ref p) => Some(p.as_sym()),
224226
clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_),
225227
clean::BareFunction(_)
@@ -240,24 +242,27 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
240242
///
241243
/// Important note: It goes through generics recursively. So if you have
242244
/// `T: Option<Result<(), ()>>`, it'll go into `Option` and then into `Result`.
243-
#[instrument(level = "trace", skip(tcx, res))]
245+
#[instrument(level = "trace", skip(tcx, res, cache))]
244246
fn add_generics_and_bounds_as_types<'tcx>(
245247
generics: &Generics,
246248
arg: &Type,
247249
tcx: TyCtxt<'tcx>,
248250
recurse: usize,
249251
res: &mut Vec<TypeWithKind>,
252+
cache: &Cache,
250253
) {
251254
fn insert_ty(
252255
res: &mut Vec<TypeWithKind>,
253256
tcx: TyCtxt<'_>,
254257
ty: Type,
255258
mut generics: Vec<TypeWithKind>,
259+
cache: &Cache,
256260
) {
257261
let is_full_generic = ty.is_full_generic();
262+
let generics_empty = generics.is_empty();
258263

259264
if is_full_generic {
260-
if generics.is_empty() {
265+
if generics_empty {
261266
// This is a type parameter with no trait bounds (for example: `T` in
262267
// `fn f<T>(p: T)`, so not useful for the rustdoc search because we would end up
263268
// with an empty type with an empty name. Let's just discard it.
@@ -304,14 +309,14 @@ fn add_generics_and_bounds_as_types<'tcx>(
304309
}
305310
}
306311
let mut index_ty = get_index_type(&ty, generics);
307-
if index_ty.name.as_ref().map(|s| s.is_empty()).unwrap_or(true) {
312+
if index_ty.name.as_ref().map(|s| s.is_empty() && generics_empty).unwrap_or(true) {
308313
return;
309314
}
310315
if is_full_generic {
311316
// We remove the name of the full generic because we have no use for it.
312317
index_ty.name = Some(String::new());
313318
res.push(TypeWithKind::from((index_ty, ItemType::Generic)));
314-
} else if let Some(kind) = ty.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) {
319+
} else if let Some(kind) = ty.def_id(cache).map(|did| tcx.def_kind(did).into()) {
315320
res.push(TypeWithKind::from((index_ty, kind)));
316321
} else if ty.is_primitive() {
317322
// This is a primitive, let's store it as such.
@@ -330,9 +335,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
330335
if let Type::Generic(arg_s) = *arg {
331336
// First we check if the bounds are in a `where` predicate...
332337
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
333-
WherePredicate::BoundPredicate { ty, .. } => {
334-
ty.def_id_no_primitives() == arg.def_id_no_primitives()
335-
}
338+
WherePredicate::BoundPredicate { ty, .. } => ty.def_id(cache) == arg.def_id(cache),
336339
_ => false,
337340
}) {
338341
let mut ty_generics = Vec::new();
@@ -348,14 +351,15 @@ fn add_generics_and_bounds_as_types<'tcx>(
348351
tcx,
349352
recurse + 1,
350353
&mut ty_generics,
354+
cache,
351355
)
352356
}
353357
_ => {}
354358
}
355359
}
356360
}
357361
}
358-
insert_ty(res, tcx, arg.clone(), ty_generics);
362+
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
359363
}
360364
// Otherwise we check if the trait bounds are "inlined" like `T: Option<u32>`...
361365
if let Some(bound) = generics.params.iter().find(|g| g.is_type() && g.name == arg_s) {
@@ -369,10 +373,11 @@ fn add_generics_and_bounds_as_types<'tcx>(
369373
tcx,
370374
recurse + 1,
371375
&mut ty_generics,
376+
cache,
372377
);
373378
}
374379
}
375-
insert_ty(res, tcx, arg.clone(), ty_generics);
380+
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
376381
}
377382
} else {
378383
// This is not a type parameter. So for example if we have `T, U: Option<T>`, and we're
@@ -383,10 +388,17 @@ fn add_generics_and_bounds_as_types<'tcx>(
383388
let mut ty_generics = Vec::new();
384389
if let Some(arg_generics) = arg.generics() {
385390
for gen in arg_generics.iter() {
386-
add_generics_and_bounds_as_types(generics, gen, tcx, recurse + 1, &mut ty_generics);
391+
add_generics_and_bounds_as_types(
392+
generics,
393+
gen,
394+
tcx,
395+
recurse + 1,
396+
&mut ty_generics,
397+
cache,
398+
);
387399
}
388400
}
389-
insert_ty(res, tcx, arg.clone(), ty_generics);
401+
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
390402
}
391403
}
392404

@@ -397,6 +409,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
397409
fn get_fn_inputs_and_outputs<'tcx>(
398410
func: &Function,
399411
tcx: TyCtxt<'tcx>,
412+
cache: &Cache,
400413
) -> (Vec<TypeWithKind>, Vec<TypeWithKind>) {
401414
let decl = &func.decl;
402415
let generics = &func.generics;
@@ -407,12 +420,11 @@ fn get_fn_inputs_and_outputs<'tcx>(
407420
continue;
408421
}
409422
let mut args = Vec::new();
410-
add_generics_and_bounds_as_types(generics, &arg.type_, tcx, 0, &mut args);
423+
add_generics_and_bounds_as_types(generics, &arg.type_, tcx, 0, &mut args, cache);
411424
if !args.is_empty() {
412425
all_types.extend(args);
413426
} else {
414-
if let Some(kind) = arg.type_.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
415-
{
427+
if let Some(kind) = arg.type_.def_id(cache).map(|did| tcx.def_kind(did).into()) {
416428
all_types.push(TypeWithKind::from((get_index_type(&arg.type_, vec![]), kind)));
417429
}
418430
}
@@ -421,11 +433,9 @@ fn get_fn_inputs_and_outputs<'tcx>(
421433
let mut ret_types = Vec::new();
422434
match decl.output {
423435
FnRetTy::Return(ref return_type) => {
424-
add_generics_and_bounds_as_types(generics, return_type, tcx, 0, &mut ret_types);
436+
add_generics_and_bounds_as_types(generics, return_type, tcx, 0, &mut ret_types, cache);
425437
if ret_types.is_empty() {
426-
if let Some(kind) =
427-
return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
428-
{
438+
if let Some(kind) = return_type.def_id(cache).map(|did| tcx.def_kind(did).into()) {
429439
ret_types.push(TypeWithKind::from((get_index_type(return_type, vec![]), kind)));
430440
}
431441
}

src/librustdoc/passes/collect_trait_impls.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use super::Pass;
55
use crate::clean::*;
66
use crate::core::DocContext;
7+
use crate::formats::cache::Cache;
78
use crate::visit::DocVisitor;
89

910
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -57,14 +58,14 @@ crate fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate
5758
}
5859
});
5960

60-
let mut cleaner = BadImplStripper { prims, items: crate_items };
61+
let mut cleaner = BadImplStripper { prims, items: crate_items, cache: &cx.cache };
6162
let mut type_did_to_deref_target: FxHashMap<DefId, &Type> = FxHashMap::default();
6263

6364
// Follow all `Deref` targets of included items and recursively add them as valid
6465
fn add_deref_target(
6566
cx: &DocContext<'_>,
6667
map: &FxHashMap<DefId, &Type>,
67-
cleaner: &mut BadImplStripper,
68+
cleaner: &mut BadImplStripper<'_>,
6869
type_did: DefId,
6970
) {
7071
if let Some(target) = map.get(&type_did) {
@@ -102,7 +103,7 @@ crate fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) -> Crate
102103
} else if let Some(did) = target.def_id(&cx.cache) {
103104
cleaner.items.insert(did.into());
104105
}
105-
if let Some(for_did) = for_.def_id_no_primitives() {
106+
if let Some(for_did) = for_.def_id(&cx.cache) {
106107
if type_did_to_deref_target.insert(for_did, target).is_none() {
107108
// Since only the `DefId` portion of the `Type` instances is known to be same for both the
108109
// `Deref` target type and the impl for type positions, this map of types is keyed by
@@ -204,19 +205,20 @@ impl DocVisitor for ItemCollector {
204205
}
205206
}
206207

207-
struct BadImplStripper {
208+
struct BadImplStripper<'a> {
208209
prims: FxHashSet<PrimitiveType>,
209210
items: FxHashSet<ItemId>,
211+
cache: &'a Cache,
210212
}
211213

212-
impl BadImplStripper {
214+
impl<'a> BadImplStripper<'a> {
213215
fn keep_impl(&self, ty: &Type, is_deref: bool) -> bool {
214216
if let Generic(_) = ty {
215217
// keep impls made on generics
216218
true
217219
} else if let Some(prim) = ty.primitive_type() {
218220
self.prims.contains(&prim)
219-
} else if let Some(did) = ty.def_id_no_primitives() {
221+
} else if let Some(did) = ty.def_id(self.cache) {
220222
is_deref || self.keep_impl_with_def_id(did.into())
221223
} else {
222224
false

src/librustdoc/passes/strip_hidden.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ crate const STRIP_HIDDEN: Pass = Pass {
1515
};
1616

1717
/// Strip items marked `#[doc(hidden)]`
18-
crate fn strip_hidden(krate: clean::Crate, _: &mut DocContext<'_>) -> clean::Crate {
18+
crate fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
1919
let mut retained = ItemIdSet::default();
2020

2121
// strip all #[doc(hidden)] items
@@ -25,7 +25,7 @@ crate fn strip_hidden(krate: clean::Crate, _: &mut DocContext<'_>) -> clean::Cra
2525
};
2626

2727
// strip all impls referencing stripped items
28-
let mut stripper = ImplStripper { retained: &retained };
28+
let mut stripper = ImplStripper { retained: &retained, cache: &cx.cache };
2929
stripper.fold_crate(krate)
3030
}
3131

src/librustdoc/passes/strip_private.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ crate fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) -> clea
2929
}
3030

3131
// strip all impls referencing private items
32-
let mut stripper = ImplStripper { retained: &retained };
32+
let mut stripper = ImplStripper { retained: &retained, cache: &cx.cache };
3333
stripper.fold_crate(krate)
3434
}

src/librustdoc/passes/stripper.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::mem;
55

66
use crate::clean::{self, Item, ItemIdSet};
77
use crate::fold::{strip_item, DocFolder};
8+
use crate::formats::cache::Cache;
89

910
crate struct Stripper<'a> {
1011
crate retained: &'a mut ItemIdSet,
@@ -118,6 +119,7 @@ impl<'a> DocFolder for Stripper<'a> {
118119
/// This stripper discards all impls which reference stripped items
119120
crate struct ImplStripper<'a> {
120121
crate retained: &'a ItemIdSet,
122+
crate cache: &'a Cache,
121123
}
122124

123125
impl<'a> DocFolder for ImplStripper<'a> {
@@ -127,7 +129,7 @@ impl<'a> DocFolder for ImplStripper<'a> {
127129
if imp.trait_.is_none() && imp.items.is_empty() {
128130
return None;
129131
}
130-
if let Some(did) = imp.for_.def_id_no_primitives() {
132+
if let Some(did) = imp.for_.def_id(self.cache) {
131133
if did.is_local() && !imp.for_.is_assoc_ty() && !self.retained.contains(&did.into())
132134
{
133135
debug!("ImplStripper: impl item for stripped type; removing");
@@ -142,7 +144,7 @@ impl<'a> DocFolder for ImplStripper<'a> {
142144
}
143145
if let Some(generics) = imp.trait_.as_ref().and_then(|t| t.generics()) {
144146
for typaram in generics {
145-
if let Some(did) = typaram.def_id_no_primitives() {
147+
if let Some(did) = typaram.def_id(self.cache) {
146148
if did.is_local() && !self.retained.contains(&did.into()) {
147149
debug!(
148150
"ImplStripper: stripped item in trait's generics; removing impl"

0 commit comments

Comments
 (0)