1
Fork 0

Add bounds for return types as well

This commit is contained in:
Guillaume Gomez 2019-03-07 16:47:40 +01:00
parent 6ae73e2ff6
commit aefe75095a
4 changed files with 59 additions and 30 deletions

View file

@ -214,7 +214,7 @@ fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
let predicates = cx.tcx.predicates_of(did); let predicates = cx.tcx.predicates_of(did);
let generics = (cx.tcx.generics_of(did), &predicates).clean(cx); let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
let decl = (did, sig).clean(cx); let decl = (did, sig).clean(cx);
let all_types = clean::get_all_types(&generics, &decl, cx); let (all_types, ret_types) = clean::get_all_types(&generics, &decl, cx);
clean::Function { clean::Function {
decl, decl,
generics, generics,
@ -225,6 +225,7 @@ fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
asyncness: hir::IsAsync::NotAsync, asyncness: hir::IsAsync::NotAsync,
}, },
all_types, all_types,
ret_types,
} }
} }

View file

@ -1751,7 +1751,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
} }
// The point is to replace bounds with types. // The point is to replace bounds with types.
pub fn get_real_types( fn get_real_types(
generics: &Generics, generics: &Generics,
arg: &Type, arg: &Type,
cx: &DocContext<'_, '_, '_>, cx: &DocContext<'_, '_, '_>,
@ -1822,7 +1822,7 @@ pub fn get_all_types(
generics: &Generics, generics: &Generics,
decl: &FnDecl, decl: &FnDecl,
cx: &DocContext<'_, '_, '_>, cx: &DocContext<'_, '_, '_>,
) -> Vec<Type> { ) -> (Vec<Type>, Vec<Type>) {
let mut all_types = Vec::new(); let mut all_types = Vec::new();
for arg in decl.inputs.values.iter() { for arg in decl.inputs.values.iter() {
if arg.type_.is_self_type() { if arg.type_.is_self_type() {
@ -1837,7 +1837,23 @@ pub fn get_all_types(
// FIXME: use a HashSet instead? // FIXME: use a HashSet instead?
all_types.sort_unstable_by(|a, b| a.to_string().partial_cmp(&b.to_string()).unwrap()); all_types.sort_unstable_by(|a, b| a.to_string().partial_cmp(&b.to_string()).unwrap());
all_types.dedup(); all_types.dedup();
all_types
let mut ret_types = match decl.output {
FunctionRetTy::Return(ref return_type) => {
let mut ret = Vec::new();
if let Some(mut args) = get_real_types(generics, &return_type, cx) {
ret.append(&mut args);
} else {
ret.push(return_type.clone());
}
ret
}
_ => Vec::new(),
};
// FIXME: use a HashSet instead?
ret_types.sort_unstable_by(|a, b| a.to_string().partial_cmp(&b.to_string()).unwrap());
ret_types.dedup();
(all_types, ret_types)
} }
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@ -1847,6 +1863,7 @@ pub struct Method {
pub header: hir::FnHeader, pub header: hir::FnHeader,
pub defaultness: Option<hir::Defaultness>, pub defaultness: Option<hir::Defaultness>,
pub all_types: Vec<Type>, pub all_types: Vec<Type>,
pub ret_types: Vec<Type>,
} }
impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId, impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId,
@ -1855,13 +1872,14 @@ impl<'a> Clean<Method> for (&'a hir::MethodSig, &'a hir::Generics, hir::BodyId,
let (generics, decl) = enter_impl_trait(cx, || { let (generics, decl) = enter_impl_trait(cx, || {
(self.1.clean(cx), (&*self.0.decl, self.2).clean(cx)) (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
}); });
let all_types = get_all_types(&generics, &decl, cx); let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
Method { Method {
decl, decl,
generics, generics,
header: self.0.header, header: self.0.header,
defaultness: self.3, defaultness: self.3,
all_types, all_types,
ret_types,
} }
} }
} }
@ -1872,6 +1890,7 @@ pub struct TyMethod {
pub decl: FnDecl, pub decl: FnDecl,
pub generics: Generics, pub generics: Generics,
pub all_types: Vec<Type>, pub all_types: Vec<Type>,
pub ret_types: Vec<Type>,
} }
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@ -1880,6 +1899,7 @@ pub struct Function {
pub generics: Generics, pub generics: Generics,
pub header: hir::FnHeader, pub header: hir::FnHeader,
pub all_types: Vec<Type>, pub all_types: Vec<Type>,
pub ret_types: Vec<Type>,
} }
impl Clean<Item> for doctree::Function { impl Clean<Item> for doctree::Function {
@ -1894,7 +1914,7 @@ impl Clean<Item> for doctree::Function {
} else { } else {
hir::Constness::NotConst hir::Constness::NotConst
}; };
let all_types = get_all_types(&generics, &decl, cx); let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
Item { Item {
name: Some(self.name.clean(cx)), name: Some(self.name.clean(cx)),
attrs: self.attrs.clean(cx), attrs: self.attrs.clean(cx),
@ -1908,6 +1928,7 @@ impl Clean<Item> for doctree::Function {
generics, generics,
header: hir::FnHeader { constness, ..self.header }, header: hir::FnHeader { constness, ..self.header },
all_types, all_types,
ret_types,
}), }),
} }
} }
@ -2177,12 +2198,13 @@ impl Clean<Item> for hir::TraitItem {
let (generics, decl) = enter_impl_trait(cx, || { let (generics, decl) = enter_impl_trait(cx, || {
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx)) (self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
}); });
let all_types = get_all_types(&generics, &decl, cx); let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
TyMethodItem(TyMethod { TyMethodItem(TyMethod {
header: sig.header, header: sig.header,
decl, decl,
generics, generics,
all_types, all_types,
ret_types,
}) })
} }
hir::TraitItemKind::Type(ref bounds, ref default) => { hir::TraitItemKind::Type(ref bounds, ref default) => {
@ -2280,7 +2302,7 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
ty::ImplContainer(_) => true, ty::ImplContainer(_) => true,
ty::TraitContainer(_) => self.defaultness.has_value() ty::TraitContainer(_) => self.defaultness.has_value()
}; };
let all_types = get_all_types(&generics, &decl, cx); let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
if provided { if provided {
let constness = if cx.tcx.is_min_const_fn(self.def_id) { let constness = if cx.tcx.is_min_const_fn(self.def_id) {
hir::Constness::Const hir::Constness::Const
@ -2298,6 +2320,7 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
}, },
defaultness: Some(self.defaultness), defaultness: Some(self.defaultness),
all_types, all_types,
ret_types,
}) })
} else { } else {
TyMethodItem(TyMethod { TyMethodItem(TyMethod {
@ -2310,6 +2333,7 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
asyncness: hir::IsAsync::NotAsync, asyncness: hir::IsAsync::NotAsync,
}, },
all_types, all_types,
ret_types,
}) })
} }
} }
@ -3976,7 +4000,7 @@ impl Clean<Item> for hir::ForeignItem {
let (generics, decl) = enter_impl_trait(cx, || { let (generics, decl) = enter_impl_trait(cx, || {
(generics.clean(cx), (&**decl, &names[..]).clean(cx)) (generics.clean(cx), (&**decl, &names[..]).clean(cx))
}); });
let all_types = get_all_types(&generics, &decl, cx); let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
ForeignFunctionItem(Function { ForeignFunctionItem(Function {
decl, decl,
generics, generics,
@ -3987,6 +4011,7 @@ impl Clean<Item> for hir::ForeignItem {
asyncness: hir::IsAsync::NotAsync, asyncness: hir::IsAsync::NotAsync,
}, },
all_types, all_types,
ret_types,
}) })
} }
hir::ForeignItemKind::Static(ref ty, mutbl) => { hir::ForeignItemKind::Static(ref ty, mutbl) => {

View file

@ -446,7 +446,7 @@ impl ToJson for Type {
} }
Json::Array(data) Json::Array(data)
} }
None => Json::Null None => Json::Null,
} }
} }
} }
@ -455,7 +455,7 @@ impl ToJson for Type {
#[derive(Debug)] #[derive(Debug)]
struct IndexItemFunctionType { struct IndexItemFunctionType {
inputs: Vec<Type>, inputs: Vec<Type>,
output: Option<Type>, output: Vec<Type>,
} }
impl ToJson for IndexItemFunctionType { impl ToJson for IndexItemFunctionType {
@ -466,8 +466,8 @@ impl ToJson for IndexItemFunctionType {
} else { } else {
let mut data = Vec::with_capacity(2); let mut data = Vec::with_capacity(2);
data.push(self.inputs.to_json()); data.push(self.inputs.to_json());
if let Some(ref output) = self.output { if !self.output.is_empty() {
data.push(output.to_json()); data.push(self.output.to_json());
} }
Json::Array(data) Json::Array(data)
} }
@ -5025,24 +5025,21 @@ fn make_item_keywords(it: &clean::Item) -> String {
} }
fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> { fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
let (decl, all_types) = match item.inner { let (all_types, ret_types) = match item.inner {
clean::FunctionItem(ref f) => (&f.decl, &f.all_types), clean::FunctionItem(ref f) => (&f.all_types, &f.ret_types),
clean::MethodItem(ref m) => (&m.decl, &m.all_types), clean::MethodItem(ref m) => (&m.all_types, &m.ret_types),
clean::TyMethodItem(ref m) => (&m.decl, &m.all_types), clean::TyMethodItem(ref m) => (&m.all_types, &m.ret_types),
_ => return None, _ => return None,
}; };
let inputs = all_types.iter().map(|arg| { let inputs = all_types.iter().map(|arg| {
get_index_type(&arg) get_index_type(&arg)
}).collect(); }).collect();
let output = match decl.output { let output = ret_types.iter().map(|arg| {
clean::FunctionRetTy::Return(ref return_type) => { get_index_type(&arg)
Some(get_index_type(return_type)) }).collect();
},
_ => None,
};
Some(IndexItemFunctionType { inputs: inputs, output: output }) Some(IndexItemFunctionType { inputs, output })
} }
fn get_index_type(clean_type: &clean::Type) -> Type { fn get_index_type(clean_type: &clean::Type) -> Type {

View file

@ -755,7 +755,12 @@ if (!DOMTokenList.prototype.remove) {
var lev_distance = MAX_LEV_DISTANCE + 1; var lev_distance = MAX_LEV_DISTANCE + 1;
if (obj && obj.type && obj.type.length > OUTPUT_DATA) { if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
var tmp = checkType(obj.type[OUTPUT_DATA], val, literalSearch); var ret = obj.type[OUTPUT_DATA];
if (!obj.type[OUTPUT_DATA].length) {
ret = [ret];
}
for (var x = 0; x < ret.length; ++x) {
var tmp = checkType(ret[x], val, literalSearch);
if (literalSearch === true && tmp === true) { if (literalSearch === true && tmp === true) {
return true; return true;
} }
@ -764,6 +769,7 @@ if (!DOMTokenList.prototype.remove) {
return 0; return 0;
} }
} }
}
return literalSearch === true ? false : lev_distance; return literalSearch === true ? false : lev_distance;
} }