Auto merge of #81557 - GuillaumeGomez:primitive-search, r=ollie27

Fix primitive search in parameters and returned values

Part of #60485.
Fixes #74780.

Replacing #74879.

cc `@camelid` `@jyn514` `@CraftSpider`
r? `@ollie27`
This commit is contained in:
bors 2021-02-02 00:15:06 +00:00
commit 461cbe42d0
7 changed files with 61 additions and 14 deletions

View File

@ -1301,6 +1301,7 @@ crate enum TypeKind {
Attr, Attr,
Derive, Derive,
TraitAlias, TraitAlias,
Primitive,
} }
crate trait GetDefId { crate trait GetDefId {
@ -1403,6 +1404,16 @@ impl Type {
matches!(self, Type::Generic(_)) matches!(self, Type::Generic(_))
} }
crate fn is_primitive(&self) -> bool {
match self {
Self::Primitive(_) => true,
Self::BorrowedRef { ref type_, .. } | Self::RawPointer(_, ref type_) => {
type_.is_primitive()
}
_ => false,
}
}
crate fn projection(&self) -> Option<(&Type, DefId, Symbol)> { crate fn projection(&self) -> Option<(&Type, DefId, Symbol)> {
let (self_, trait_, name) = match self { let (self_, trait_, name) = match self {
QPath { self_type, trait_, name } => (self_type, trait_, name), QPath { self_type, trait_, name } => (self_type, trait_, name),

View File

@ -171,11 +171,20 @@ crate fn get_real_types(
cx: &DocContext<'_>, cx: &DocContext<'_>,
recurse: i32, recurse: i32,
) -> FxHashSet<(Type, TypeKind)> { ) -> FxHashSet<(Type, TypeKind)> {
fn insert(res: &mut FxHashSet<(Type, TypeKind)>, cx: &DocContext<'_>, ty: Type) {
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
res.insert((ty, kind));
} else if ty.is_primitive() {
// This is a primitive, let's store it as such.
res.insert((ty, TypeKind::Primitive));
}
}
let mut res = FxHashSet::default(); let mut res = FxHashSet::default();
if recurse >= 10 { if recurse >= 10 {
// FIXME: remove this whole recurse thing when the recursion bug is fixed // FIXME: remove this whole recurse thing when the recursion bug is fixed
return res; return res;
} }
if arg.is_full_generic() { if arg.is_full_generic() {
let arg_s = Symbol::intern(&arg.print(&cx.cache).to_string()); let arg_s = Symbol::intern(&arg.print(&cx.cache).to_string());
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g { if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
@ -194,11 +203,7 @@ crate fn get_real_types(
if !adds.is_empty() { if !adds.is_empty() {
res.extend(adds); res.extend(adds);
} else if !ty.is_full_generic() { } else if !ty.is_full_generic() {
if let Some(kind) = insert(&mut res, cx, ty);
ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx))
{
res.insert((ty, kind));
}
} }
} }
} }
@ -212,17 +217,13 @@ crate fn get_real_types(
if !adds.is_empty() { if !adds.is_empty() {
res.extend(adds); res.extend(adds);
} else if !ty.is_full_generic() { } else if !ty.is_full_generic() {
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) { insert(&mut res, cx, ty);
res.insert((ty.clone(), kind));
}
} }
} }
} }
} }
} else { } else {
if let Some(kind) = arg.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) { insert(&mut res, cx, arg.clone());
res.insert((arg.clone(), kind));
}
if let Some(gens) = arg.generics() { if let Some(gens) = arg.generics() {
for gen in gens.iter() { for gen in gens.iter() {
if gen.is_full_generic() { if gen.is_full_generic() {
@ -230,8 +231,8 @@ crate fn get_real_types(
if !adds.is_empty() { if !adds.is_empty() {
res.extend(adds); res.extend(adds);
} }
} else if let Some(kind) = gen.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) { } else {
res.insert((gen.clone(), kind)); insert(&mut res, cx, gen.clone());
} }
} }
} }

View File

@ -119,6 +119,7 @@ impl From<clean::TypeKind> for ItemType {
clean::TypeKind::Attr => ItemType::ProcAttribute, clean::TypeKind::Attr => ItemType::ProcAttribute,
clean::TypeKind::Derive => ItemType::ProcDerive, clean::TypeKind::Derive => ItemType::ProcDerive,
clean::TypeKind::TraitAlias => ItemType::TraitAlias, clean::TypeKind::TraitAlias => ItemType::TraitAlias,
clean::TypeKind::Primitive => ItemType::Primitive,
} }
} }
} }

View File

@ -78,7 +78,7 @@ crate fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
desc: item.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s)), desc: item.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s)),
parent: Some(did), parent: Some(did),
parent_idx: None, parent_idx: None,
search_type: get_index_search_type(&item, None), search_type: get_index_search_type(&item, Some(cache)),
}); });
for alias in item.attrs.get_doc_aliases() { for alias in item.attrs.get_doc_aliases() {
cache cache

View File

@ -0,0 +1,25 @@
// exact-check
const QUERY = [
"i32",
"str",
"TotoIsSomewhere",
];
const EXPECTED = [
{
'in_args': [
{ 'path': 'primitive', 'name': 'foo' },
],
},
{
'returned': [
{ 'path': 'primitive', 'name': 'foo' },
],
},
{
'others': [],
'in_args': [],
'returned': [],
},
];

View File

@ -0,0 +1,5 @@
pub fn foo(i: i32) -> &'static str {
"hello"
}
pub fn foo2<TotoIsSomewhere>(i: &TotoIsSomewhere, j: TotoIsSomewhere) {}

View File

@ -203,6 +203,10 @@ function betterLookingDiff(entry, data) {
if (!entry.hasOwnProperty(key)) { if (!entry.hasOwnProperty(key)) {
continue; continue;
} }
if (!data || !data.hasOwnProperty(key)) {
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
continue;
}
let value = data[key]; let value = data[key];
if (value !== entry[key]) { if (value !== entry[key]) {
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n'; output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';