Skip to content

Commit 6bce61c

Browse files
Fix invalid returned types generation
1 parent aefe750 commit 6bce61c

File tree

3 files changed

+63
-46
lines changed

3 files changed

+63
-46
lines changed

src/librustdoc/clean/mod.rs

+40-40
Original file line numberDiff line numberDiff line change
@@ -1755,55 +1755,59 @@ fn get_real_types(
17551755
generics: &Generics,
17561756
arg: &Type,
17571757
cx: &DocContext<'_, '_, '_>,
1758-
) -> Option<Vec<Type>> {
1758+
) -> Vec<Type> {
1759+
let arg_s = arg.to_string();
17591760
let mut res = Vec::new();
1760-
if let Some(where_pred) = generics.where_predicates.iter().find(|g| {
1761-
match g {
1762-
&WherePredicate::BoundPredicate { ref ty, .. } => ty.def_id() == arg.def_id(),
1763-
_ => false,
1764-
}
1765-
}) {
1766-
let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
1767-
for bound in bounds.iter() {
1768-
match *bound {
1769-
GenericBound::TraitBound(ref poly_trait, _) => {
1770-
for x in poly_trait.generic_params.iter() {
1771-
if !x.is_type() {
1772-
continue
1773-
}
1774-
if let Some(ty) = x.get_type(cx) {
1775-
if let Some(mut adds) = get_real_types(generics, &ty, cx) {
1776-
res.append(&mut adds);
1777-
} else if !ty.is_full_generic() {
1778-
res.push(ty);
1761+
if arg.is_full_generic() {
1762+
if let Some(where_pred) = generics.where_predicates.iter().find(|g| {
1763+
match g {
1764+
&WherePredicate::BoundPredicate { ref ty, .. } => ty.def_id() == arg.def_id(),
1765+
_ => false,
1766+
}
1767+
}) {
1768+
let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
1769+
for bound in bounds.iter() {
1770+
match *bound {
1771+
GenericBound::TraitBound(ref poly_trait, _) => {
1772+
for x in poly_trait.generic_params.iter() {
1773+
if !x.is_type() {
1774+
continue
1775+
}
1776+
if let Some(ty) = x.get_type(cx) {
1777+
let mut adds = get_real_types(generics, &ty, cx);
1778+
if !adds.is_empty() {
1779+
res.append(&mut adds);
1780+
} else if !ty.is_full_generic() {
1781+
res.push(ty);
1782+
}
17791783
}
17801784
}
17811785
}
1786+
_ => {}
17821787
}
1783-
_ => {}
17841788
}
17851789
}
1786-
} else {
1787-
let arg_s = arg.to_string();
17881790
if let Some(bound) = generics.params.iter().find(|g| {
17891791
g.is_type() && g.name == arg_s
17901792
}) {
17911793
for bound in bound.get_bounds().unwrap_or_else(|| &[]) {
17921794
if let Some(ty) = bound.get_trait_type() {
1793-
if let Some(mut adds) = get_real_types(generics, &ty, cx) {
1795+
let mut adds = get_real_types(generics, &ty, cx);
1796+
if !adds.is_empty() {
17941797
res.append(&mut adds);
1795-
} else {
1796-
if !ty.is_full_generic() {
1797-
res.push(ty.clone());
1798-
}
1798+
} else if !ty.is_full_generic() {
1799+
res.push(ty.clone());
17991800
}
18001801
}
18011802
}
1802-
} else if let Some(gens) = arg.generics() {
1803-
res.push(arg.clone());
1803+
}
1804+
} else {
1805+
res.push(arg.clone());
1806+
if let Some(gens) = arg.generics() {
18041807
for gen in gens.iter() {
18051808
if gen.is_full_generic() {
1806-
if let Some(mut adds) = get_real_types(generics, gen, cx) {
1809+
let mut adds = get_real_types(generics, gen, cx);
1810+
if !adds.is_empty() {
18071811
res.append(&mut adds);
18081812
}
18091813
} else {
@@ -1812,10 +1816,7 @@ fn get_real_types(
18121816
}
18131817
}
18141818
}
1815-
if res.is_empty() && !arg.is_full_generic() {
1816-
res.push(arg.clone());
1817-
}
1818-
Some(res)
1819+
res
18191820
}
18201821

18211822
pub fn get_all_types(
@@ -1828,7 +1829,8 @@ pub fn get_all_types(
18281829
if arg.type_.is_self_type() {
18291830
continue;
18301831
}
1831-
if let Some(mut args) = get_real_types(generics, &arg.type_, cx) {
1832+
let mut args = get_real_types(generics, &arg.type_, cx);
1833+
if !args.is_empty() {
18321834
all_types.append(&mut args);
18331835
} else {
18341836
all_types.push(arg.type_.clone());
@@ -1840,10 +1842,8 @@ pub fn get_all_types(
18401842

18411843
let mut ret_types = match decl.output {
18421844
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 {
1845+
let mut ret = get_real_types(generics, &return_type, cx);
1846+
if ret.is_empty() {
18471847
ret.push(return_type.clone());
18481848
}
18491849
ret

src/librustdoc/html/render.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -455,19 +455,27 @@ impl ToJson for Type {
455455
#[derive(Debug)]
456456
struct IndexItemFunctionType {
457457
inputs: Vec<Type>,
458-
output: Vec<Type>,
458+
output: Option<Vec<Type>>,
459459
}
460460

461461
impl ToJson for IndexItemFunctionType {
462462
fn to_json(&self) -> Json {
463463
// If we couldn't figure out a type, just write `null`.
464-
if self.inputs.iter().chain(self.output.iter()).any(|ref i| i.name.is_none()) {
464+
let mut iter = self.inputs.iter();
465+
if match self.output {
466+
Some(ref output) => iter.chain(output.iter()).any(|ref i| i.name.is_none()),
467+
None => iter.any(|ref i| i.name.is_none()),
468+
} {
465469
Json::Null
466470
} else {
467471
let mut data = Vec::with_capacity(2);
468472
data.push(self.inputs.to_json());
469-
if !self.output.is_empty() {
470-
data.push(self.output.to_json());
473+
if let Some(ref output) = self.output {
474+
if output.len() > 1 {
475+
data.push(output.to_json());
476+
} else {
477+
data.push(output[0].to_json());
478+
}
471479
}
472480
Json::Array(data)
473481
}
@@ -5034,11 +5042,17 @@ fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
50345042

50355043
let inputs = all_types.iter().map(|arg| {
50365044
get_index_type(&arg)
5037-
}).collect();
5045+
}).filter(|a| a.name.is_some()).collect();
50385046
let output = ret_types.iter().map(|arg| {
50395047
get_index_type(&arg)
5040-
}).collect();
5048+
}).filter(|a| a.name.is_some()).collect::<Vec<_>>();
5049+
let output = if output.is_empty() {
5050+
None
5051+
} else {
5052+
Some(output)
5053+
};
50415054

5055+
println!("===> {:?}", output);
50425056
Some(IndexItemFunctionType { inputs, output })
50435057
}
50445058

src/librustdoc/html/static/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,9 @@ if (!DOMTokenList.prototype.remove) {
756756

757757
if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
758758
var ret = obj.type[OUTPUT_DATA];
759+
//if (obj.name === "xo") {
760+
// debugger;
761+
//}
759762
if (!obj.type[OUTPUT_DATA].length) {
760763
ret = [ret];
761764
}

0 commit comments

Comments
 (0)