1
Fork 0

Auto merge of #92711 - zredb:issue-90187-fix, r=notriddle

rustdoc: Remove `def_id_no_primitives`

Fixes #90187.
This commit is contained in:
bors 2022-01-30 20:57:34 +00:00
commit 08df8b81d6
7 changed files with 51 additions and 49 deletions

View file

@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use rustc_data_structures::fx::FxHashMap;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::Symbol;
use rustc_span::symbol::{kw, Symbol};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use crate::clean;
@ -33,7 +33,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
desc,
parent: Some(did),
parent_idx: None,
search_type: get_function_type_for_search(item, tcx),
search_type: get_function_type_for_search(item, tcx, &cache),
aliases: item.attrs.get_doc_aliases(),
});
}
@ -188,11 +188,12 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
crate fn get_function_type_for_search<'tcx>(
item: &clean::Item,
tcx: TyCtxt<'tcx>,
cache: &Cache,
) -> Option<IndexItemFunctionType> {
let (mut inputs, mut output) = match *item.kind {
clean::FunctionItem(ref f) => get_fn_inputs_and_outputs(f, tcx),
clean::MethodItem(ref m, _) => get_fn_inputs_and_outputs(m, tcx),
clean::TyMethodItem(ref m) => get_fn_inputs_and_outputs(m, tcx),
clean::FunctionItem(ref f) => get_fn_inputs_and_outputs(f, tcx, cache),
clean::MethodItem(ref m, _) => get_fn_inputs_and_outputs(m, tcx, cache),
clean::TyMethodItem(ref m) => get_fn_inputs_and_outputs(m, tcx, cache),
_ => return None,
};
@ -219,7 +220,8 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
let path = &bounds[0].trait_;
Some(path.segments.last().unwrap().name)
}
clean::Generic(s) => Some(s),
// We return an empty name because we don't care about the generic name itself.
clean::Generic(_) => Some(kw::Empty),
clean::Primitive(ref p) => Some(p.as_sym()),
clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_),
clean::BareFunction(_)
@ -240,24 +242,27 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
///
/// Important note: It goes through generics recursively. So if you have
/// `T: Option<Result<(), ()>>`, it'll go into `Option` and then into `Result`.
#[instrument(level = "trace", skip(tcx, res))]
#[instrument(level = "trace", skip(tcx, res, cache))]
fn add_generics_and_bounds_as_types<'tcx>(
generics: &Generics,
arg: &Type,
tcx: TyCtxt<'tcx>,
recurse: usize,
res: &mut Vec<TypeWithKind>,
cache: &Cache,
) {
fn insert_ty(
res: &mut Vec<TypeWithKind>,
tcx: TyCtxt<'_>,
ty: Type,
mut generics: Vec<TypeWithKind>,
cache: &Cache,
) {
let is_full_generic = ty.is_full_generic();
let generics_empty = generics.is_empty();
if is_full_generic {
if generics.is_empty() {
if generics_empty {
// This is a type parameter with no trait bounds (for example: `T` in
// `fn f<T>(p: T)`, so not useful for the rustdoc search because we would end up
// 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>(
}
}
let mut index_ty = get_index_type(&ty, generics);
if index_ty.name.as_ref().map(|s| s.is_empty()).unwrap_or(true) {
if index_ty.name.as_ref().map(|s| s.is_empty() && generics_empty).unwrap_or(true) {
return;
}
if is_full_generic {
// We remove the name of the full generic because we have no use for it.
index_ty.name = Some(String::new());
res.push(TypeWithKind::from((index_ty, ItemType::Generic)));
} else if let Some(kind) = ty.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) {
} else if let Some(kind) = ty.def_id(cache).map(|did| tcx.def_kind(did).into()) {
res.push(TypeWithKind::from((index_ty, kind)));
} else if ty.is_primitive() {
// This is a primitive, let's store it as such.
@ -330,9 +335,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
if let Type::Generic(arg_s) = *arg {
// First we check if the bounds are in a `where` predicate...
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
WherePredicate::BoundPredicate { ty, .. } => {
ty.def_id_no_primitives() == arg.def_id_no_primitives()
}
WherePredicate::BoundPredicate { ty, .. } => ty.def_id(cache) == arg.def_id(cache),
_ => false,
}) {
let mut ty_generics = Vec::new();
@ -348,6 +351,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
tcx,
recurse + 1,
&mut ty_generics,
cache,
)
}
_ => {}
@ -355,7 +359,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
}
}
}
insert_ty(res, tcx, arg.clone(), ty_generics);
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
}
// Otherwise we check if the trait bounds are "inlined" like `T: Option<u32>`...
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>(
tcx,
recurse + 1,
&mut ty_generics,
cache,
);
}
}
insert_ty(res, tcx, arg.clone(), ty_generics);
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
}
} else {
// 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>(
let mut ty_generics = Vec::new();
if let Some(arg_generics) = arg.generics() {
for gen in arg_generics.iter() {
add_generics_and_bounds_as_types(generics, gen, tcx, recurse + 1, &mut ty_generics);
add_generics_and_bounds_as_types(
generics,
gen,
tcx,
recurse + 1,
&mut ty_generics,
cache,
);
}
}
insert_ty(res, tcx, arg.clone(), ty_generics);
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
}
}
@ -397,6 +409,7 @@ fn add_generics_and_bounds_as_types<'tcx>(
fn get_fn_inputs_and_outputs<'tcx>(
func: &Function,
tcx: TyCtxt<'tcx>,
cache: &Cache,
) -> (Vec<TypeWithKind>, Vec<TypeWithKind>) {
let decl = &func.decl;
let generics = &func.generics;
@ -407,12 +420,11 @@ fn get_fn_inputs_and_outputs<'tcx>(
continue;
}
let mut args = Vec::new();
add_generics_and_bounds_as_types(generics, &arg.type_, tcx, 0, &mut args);
add_generics_and_bounds_as_types(generics, &arg.type_, tcx, 0, &mut args, cache);
if !args.is_empty() {
all_types.extend(args);
} else {
if let Some(kind) = arg.type_.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
{
if let Some(kind) = arg.type_.def_id(cache).map(|did| tcx.def_kind(did).into()) {
all_types.push(TypeWithKind::from((get_index_type(&arg.type_, vec![]), kind)));
}
}
@ -421,11 +433,9 @@ fn get_fn_inputs_and_outputs<'tcx>(
let mut ret_types = Vec::new();
match decl.output {
FnRetTy::Return(ref return_type) => {
add_generics_and_bounds_as_types(generics, return_type, tcx, 0, &mut ret_types);
add_generics_and_bounds_as_types(generics, return_type, tcx, 0, &mut ret_types, cache);
if ret_types.is_empty() {
if let Some(kind) =
return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
{
if let Some(kind) = return_type.def_id(cache).map(|did| tcx.def_kind(did).into()) {
ret_types.push(TypeWithKind::from((get_index_type(return_type, vec![]), kind)));
}
}