Skip to content

Commit aefe750

Browse files
Add bounds for return types as well
1 parent 6ae73e2 commit aefe750

File tree

4 files changed

+59
-30
lines changed

4 files changed

+59
-30
lines changed

src/librustdoc/clean/inline.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
214214
let predicates = cx.tcx.predicates_of(did);
215215
let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
216216
let decl = (did, sig).clean(cx);
217-
let all_types = clean::get_all_types(&generics, &decl, cx);
217+
let (all_types, ret_types) = clean::get_all_types(&generics, &decl, cx);
218218
clean::Function {
219219
decl,
220220
generics,
@@ -225,6 +225,7 @@ fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
225225
asyncness: hir::IsAsync::NotAsync,
226226
},
227227
all_types,
228+
ret_types,
228229
}
229230
}
230231

src/librustdoc/clean/mod.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
17511751
}
17521752

17531753
// The point is to replace bounds with types.
1754-
pub fn get_real_types(
1754+
fn get_real_types(
17551755
generics: &Generics,
17561756
arg: &Type,
17571757
cx: &DocContext<'_, '_, '_>,
@@ -1822,7 +1822,7 @@ pub fn get_all_types(
18221822
generics: &Generics,
18231823
decl: &FnDecl,
18241824
cx: &DocContext<'_, '_, '_>,
1825-
) -> Vec<Type> {
1825+
) -> (Vec<Type>, Vec<Type>) {
18261826
let mut all_types = Vec::new();
18271827
for arg in decl.inputs.values.iter() {
18281828
if arg.type_.is_self_type() {
@@ -1837,7 +1837,23 @@ pub fn get_all_types(
18371837
// FIXME: use a HashSet instead?
18381838
all_types.sort_unstable_by(|a, b| a.to_string().partial_cmp(&b.to_string()).unwrap());
18391839
all_types.dedup();
1840-
all_types
1840+
1841+
let mut ret_types = match decl.output {
1842+
FunctionRetTy::Return(ref return_type) => {
1843+
let mut ret = Vec::new();
1844+
if let Some(mut args) = get_real_types(generics, &return_type, cx) {
1845+
ret.append(&mut args);
1846+
} else {
1847+
ret.push(return_type.clone());
1848+
}
1849+
ret
1850+
}
1851+
_ => Vec::new(),
1852+
};
1853+
// FIXME: use a HashSet instead?
1854+
ret_types.sort_unstable_by(|a, b| a.to_string().partial_cmp(&b.to_string()).unwrap());
1855+
ret_types.dedup();
1856+
(all_types, ret_types)
18411857
}
18421858

18431859
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -1847,6 +1863,7 @@ pub struct Method {
18471863
pub header: hir::FnHeader,
18481864
pub defaultness: Option<hir::Defaultness>,
18491865
pub all_types: Vec<Type>,
1866+
pub ret_types: Vec<Type>,
18501867
}
18511868

18521869
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,
18551872
let (generics, decl) = enter_impl_trait(cx, || {
18561873
(self.1.clean(cx), (&*self.0.decl, self.2).clean(cx))
18571874
});
1858-
let all_types = get_all_types(&generics, &decl, cx);
1875+
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
18591876
Method {
18601877
decl,
18611878
generics,
18621879
header: self.0.header,
18631880
defaultness: self.3,
18641881
all_types,
1882+
ret_types,
18651883
}
18661884
}
18671885
}
@@ -1872,6 +1890,7 @@ pub struct TyMethod {
18721890
pub decl: FnDecl,
18731891
pub generics: Generics,
18741892
pub all_types: Vec<Type>,
1893+
pub ret_types: Vec<Type>,
18751894
}
18761895

18771896
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -1880,6 +1899,7 @@ pub struct Function {
18801899
pub generics: Generics,
18811900
pub header: hir::FnHeader,
18821901
pub all_types: Vec<Type>,
1902+
pub ret_types: Vec<Type>,
18831903
}
18841904

18851905
impl Clean<Item> for doctree::Function {
@@ -1894,7 +1914,7 @@ impl Clean<Item> for doctree::Function {
18941914
} else {
18951915
hir::Constness::NotConst
18961916
};
1897-
let all_types = get_all_types(&generics, &decl, cx);
1917+
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
18981918
Item {
18991919
name: Some(self.name.clean(cx)),
19001920
attrs: self.attrs.clean(cx),
@@ -1908,6 +1928,7 @@ impl Clean<Item> for doctree::Function {
19081928
generics,
19091929
header: hir::FnHeader { constness, ..self.header },
19101930
all_types,
1931+
ret_types,
19111932
}),
19121933
}
19131934
}
@@ -2177,12 +2198,13 @@ impl Clean<Item> for hir::TraitItem {
21772198
let (generics, decl) = enter_impl_trait(cx, || {
21782199
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
21792200
});
2180-
let all_types = get_all_types(&generics, &decl, cx);
2201+
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
21812202
TyMethodItem(TyMethod {
21822203
header: sig.header,
21832204
decl,
21842205
generics,
21852206
all_types,
2207+
ret_types,
21862208
})
21872209
}
21882210
hir::TraitItemKind::Type(ref bounds, ref default) => {
@@ -2280,7 +2302,7 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
22802302
ty::ImplContainer(_) => true,
22812303
ty::TraitContainer(_) => self.defaultness.has_value()
22822304
};
2283-
let all_types = get_all_types(&generics, &decl, cx);
2305+
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
22842306
if provided {
22852307
let constness = if cx.tcx.is_min_const_fn(self.def_id) {
22862308
hir::Constness::Const
@@ -2298,6 +2320,7 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
22982320
},
22992321
defaultness: Some(self.defaultness),
23002322
all_types,
2323+
ret_types,
23012324
})
23022325
} else {
23032326
TyMethodItem(TyMethod {
@@ -2310,6 +2333,7 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
23102333
asyncness: hir::IsAsync::NotAsync,
23112334
},
23122335
all_types,
2336+
ret_types,
23132337
})
23142338
}
23152339
}
@@ -3976,7 +4000,7 @@ impl Clean<Item> for hir::ForeignItem {
39764000
let (generics, decl) = enter_impl_trait(cx, || {
39774001
(generics.clean(cx), (&**decl, &names[..]).clean(cx))
39784002
});
3979-
let all_types = get_all_types(&generics, &decl, cx);
4003+
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
39804004
ForeignFunctionItem(Function {
39814005
decl,
39824006
generics,
@@ -3987,6 +4011,7 @@ impl Clean<Item> for hir::ForeignItem {
39874011
asyncness: hir::IsAsync::NotAsync,
39884012
},
39894013
all_types,
4014+
ret_types,
39904015
})
39914016
}
39924017
hir::ForeignItemKind::Static(ref ty, mutbl) => {

src/librustdoc/html/render.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl ToJson for Type {
446446
}
447447
Json::Array(data)
448448
}
449-
None => Json::Null
449+
None => Json::Null,
450450
}
451451
}
452452
}
@@ -455,7 +455,7 @@ impl ToJson for Type {
455455
#[derive(Debug)]
456456
struct IndexItemFunctionType {
457457
inputs: Vec<Type>,
458-
output: Option<Type>,
458+
output: Vec<Type>,
459459
}
460460

461461
impl ToJson for IndexItemFunctionType {
@@ -466,8 +466,8 @@ impl ToJson for IndexItemFunctionType {
466466
} else {
467467
let mut data = Vec::with_capacity(2);
468468
data.push(self.inputs.to_json());
469-
if let Some(ref output) = self.output {
470-
data.push(output.to_json());
469+
if !self.output.is_empty() {
470+
data.push(self.output.to_json());
471471
}
472472
Json::Array(data)
473473
}
@@ -5025,24 +5025,21 @@ fn make_item_keywords(it: &clean::Item) -> String {
50255025
}
50265026

50275027
fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
5028-
let (decl, all_types) = match item.inner {
5029-
clean::FunctionItem(ref f) => (&f.decl, &f.all_types),
5030-
clean::MethodItem(ref m) => (&m.decl, &m.all_types),
5031-
clean::TyMethodItem(ref m) => (&m.decl, &m.all_types),
5028+
let (all_types, ret_types) = match item.inner {
5029+
clean::FunctionItem(ref f) => (&f.all_types, &f.ret_types),
5030+
clean::MethodItem(ref m) => (&m.all_types, &m.ret_types),
5031+
clean::TyMethodItem(ref m) => (&m.all_types, &m.ret_types),
50325032
_ => return None,
50335033
};
50345034

50355035
let inputs = all_types.iter().map(|arg| {
50365036
get_index_type(&arg)
50375037
}).collect();
5038-
let output = match decl.output {
5039-
clean::FunctionRetTy::Return(ref return_type) => {
5040-
Some(get_index_type(return_type))
5041-
},
5042-
_ => None,
5043-
};
5038+
let output = ret_types.iter().map(|arg| {
5039+
get_index_type(&arg)
5040+
}).collect();
50445041

5045-
Some(IndexItemFunctionType { inputs: inputs, output: output })
5042+
Some(IndexItemFunctionType { inputs, output })
50465043
}
50475044

50485045
fn get_index_type(clean_type: &clean::Type) -> Type {

src/librustdoc/html/static/main.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -755,13 +755,19 @@ if (!DOMTokenList.prototype.remove) {
755755
var lev_distance = MAX_LEV_DISTANCE + 1;
756756

757757
if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
758-
var tmp = checkType(obj.type[OUTPUT_DATA], val, literalSearch);
759-
if (literalSearch === true && tmp === true) {
760-
return true;
758+
var ret = obj.type[OUTPUT_DATA];
759+
if (!obj.type[OUTPUT_DATA].length) {
760+
ret = [ret];
761761
}
762-
lev_distance = Math.min(tmp, lev_distance);
763-
if (lev_distance === 0) {
764-
return 0;
762+
for (var x = 0; x < ret.length; ++x) {
763+
var tmp = checkType(ret[x], val, literalSearch);
764+
if (literalSearch === true && tmp === true) {
765+
return true;
766+
}
767+
lev_distance = Math.min(tmp, lev_distance);
768+
if (lev_distance === 0) {
769+
return 0;
770+
}
765771
}
766772
}
767773
return literalSearch === true ? false : lev_distance;

0 commit comments

Comments
 (0)