Fix invalid returned types generation

This commit is contained in:
Guillaume Gomez 2019-03-07 22:47:43 +01:00
parent aefe75095a
commit 6bce61cd4b
3 changed files with 63 additions and 46 deletions

View file

@ -1755,55 +1755,59 @@ fn get_real_types(
generics: &Generics, generics: &Generics,
arg: &Type, arg: &Type,
cx: &DocContext<'_, '_, '_>, cx: &DocContext<'_, '_, '_>,
) -> Option<Vec<Type>> { ) -> Vec<Type> {
let arg_s = arg.to_string();
let mut res = Vec::new(); let mut res = Vec::new();
if let Some(where_pred) = generics.where_predicates.iter().find(|g| { if arg.is_full_generic() {
match g { if let Some(where_pred) = generics.where_predicates.iter().find(|g| {
&WherePredicate::BoundPredicate { ref ty, .. } => ty.def_id() == arg.def_id(), match g {
_ => false, &WherePredicate::BoundPredicate { ref ty, .. } => ty.def_id() == arg.def_id(),
} _ => false,
}) { }
let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]); }) {
for bound in bounds.iter() { let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
match *bound { for bound in bounds.iter() {
GenericBound::TraitBound(ref poly_trait, _) => { match *bound {
for x in poly_trait.generic_params.iter() { GenericBound::TraitBound(ref poly_trait, _) => {
if !x.is_type() { for x in poly_trait.generic_params.iter() {
continue if !x.is_type() {
} continue
if let Some(ty) = x.get_type(cx) { }
if let Some(mut adds) = get_real_types(generics, &ty, cx) { if let Some(ty) = x.get_type(cx) {
res.append(&mut adds); let mut adds = get_real_types(generics, &ty, cx);
} else if !ty.is_full_generic() { if !adds.is_empty() {
res.push(ty); res.append(&mut adds);
} else if !ty.is_full_generic() {
res.push(ty);
}
} }
} }
} }
_ => {}
} }
_ => {}
} }
} }
} else {
let arg_s = arg.to_string();
if let Some(bound) = generics.params.iter().find(|g| { if let Some(bound) = generics.params.iter().find(|g| {
g.is_type() && g.name == arg_s g.is_type() && g.name == arg_s
}) { }) {
for bound in bound.get_bounds().unwrap_or_else(|| &[]) { for bound in bound.get_bounds().unwrap_or_else(|| &[]) {
if let Some(ty) = bound.get_trait_type() { if let Some(ty) = bound.get_trait_type() {
if let Some(mut adds) = get_real_types(generics, &ty, cx) { let mut adds = get_real_types(generics, &ty, cx);
if !adds.is_empty() {
res.append(&mut adds); res.append(&mut adds);
} else { } else if !ty.is_full_generic() {
if !ty.is_full_generic() { res.push(ty.clone());
res.push(ty.clone());
}
} }
} }
} }
} else if let Some(gens) = arg.generics() { }
res.push(arg.clone()); } else {
res.push(arg.clone());
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() {
if let Some(mut adds) = get_real_types(generics, gen, cx) { let mut adds = get_real_types(generics, gen, cx);
if !adds.is_empty() {
res.append(&mut adds); res.append(&mut adds);
} }
} else { } else {
@ -1812,10 +1816,7 @@ fn get_real_types(
} }
} }
} }
if res.is_empty() && !arg.is_full_generic() { res
res.push(arg.clone());
}
Some(res)
} }
pub fn get_all_types( pub fn get_all_types(
@ -1828,7 +1829,8 @@ pub fn get_all_types(
if arg.type_.is_self_type() { if arg.type_.is_self_type() {
continue; continue;
} }
if let Some(mut args) = get_real_types(generics, &arg.type_, cx) { let mut args = get_real_types(generics, &arg.type_, cx);
if !args.is_empty() {
all_types.append(&mut args); all_types.append(&mut args);
} else { } else {
all_types.push(arg.type_.clone()); all_types.push(arg.type_.clone());
@ -1840,10 +1842,8 @@ pub fn get_all_types(
let mut ret_types = match decl.output { let mut ret_types = match decl.output {
FunctionRetTy::Return(ref return_type) => { FunctionRetTy::Return(ref return_type) => {
let mut ret = Vec::new(); let mut ret = get_real_types(generics, &return_type, cx);
if let Some(mut args) = get_real_types(generics, &return_type, cx) { if ret.is_empty() {
ret.append(&mut args);
} else {
ret.push(return_type.clone()); ret.push(return_type.clone());
} }
ret ret

View file

@ -455,19 +455,27 @@ impl ToJson for Type {
#[derive(Debug)] #[derive(Debug)]
struct IndexItemFunctionType { struct IndexItemFunctionType {
inputs: Vec<Type>, inputs: Vec<Type>,
output: Vec<Type>, output: Option<Vec<Type>>,
} }
impl ToJson for IndexItemFunctionType { impl ToJson for IndexItemFunctionType {
fn to_json(&self) -> Json { fn to_json(&self) -> Json {
// If we couldn't figure out a type, just write `null`. // If we couldn't figure out a type, just write `null`.
if self.inputs.iter().chain(self.output.iter()).any(|ref i| i.name.is_none()) { let mut iter = self.inputs.iter();
if match self.output {
Some(ref output) => iter.chain(output.iter()).any(|ref i| i.name.is_none()),
None => iter.any(|ref i| i.name.is_none()),
} {
Json::Null Json::Null
} 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 !self.output.is_empty() { if let Some(ref output) = self.output {
data.push(self.output.to_json()); if output.len() > 1 {
data.push(output.to_json());
} else {
data.push(output[0].to_json());
}
} }
Json::Array(data) Json::Array(data)
} }
@ -5034,11 +5042,17 @@ fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
let inputs = all_types.iter().map(|arg| { let inputs = all_types.iter().map(|arg| {
get_index_type(&arg) get_index_type(&arg)
}).collect(); }).filter(|a| a.name.is_some()).collect();
let output = ret_types.iter().map(|arg| { let output = ret_types.iter().map(|arg| {
get_index_type(&arg) get_index_type(&arg)
}).collect(); }).filter(|a| a.name.is_some()).collect::<Vec<_>>();
let output = if output.is_empty() {
None
} else {
Some(output)
};
println!("===> {:?}", output);
Some(IndexItemFunctionType { inputs, output }) Some(IndexItemFunctionType { inputs, output })
} }

View file

@ -756,6 +756,9 @@ if (!DOMTokenList.prototype.remove) {
if (obj && obj.type && obj.type.length > OUTPUT_DATA) { if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
var ret = obj.type[OUTPUT_DATA]; var ret = obj.type[OUTPUT_DATA];
//if (obj.name === "xo") {
// debugger;
//}
if (!obj.type[OUTPUT_DATA].length) { if (!obj.type[OUTPUT_DATA].length) {
ret = [ret]; ret = [ret];
} }