1
Fork 0
This commit is contained in:
Guillaume Gomez 2019-03-07 23:32:42 +01:00
parent 6bce61cd4b
commit dc628b4f67
3 changed files with 24 additions and 13 deletions

View file

@ -1133,6 +1133,10 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Ty<'tcx> {
checked_type_of(tcx, def_id, true).unwrap() checked_type_of(tcx, def_id, true).unwrap()
} }
/// Same as [`type_of`] but returns [`Option`] instead of failing.
///
/// If you want to fail anyway, you can set the `fail` parameter to true, but in this case,
/// you'd better just call [`type_of`] directly.
pub fn checked_type_of<'a, 'tcx>( pub fn checked_type_of<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>, tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId, def_id: DefId,
@ -1382,14 +1386,14 @@ pub fn checked_type_of<'a, 'tcx>(
for param in &generics.params { for param in &generics.params {
if let ty::GenericParamDefKind::Const = param.kind { if let ty::GenericParamDefKind::Const = param.kind {
if param_index == arg_index { if param_index == arg_index {
return tcx.type_of(param.def_id); return Some(tcx.type_of(param.def_id));
} }
param_index += 1; param_index += 1;
} }
} }
// This is no generic parameter associated with the arg. This is // This is no generic parameter associated with the arg. This is
// probably from an extra arg where one is not needed. // probably from an extra arg where one is not needed.
return tcx.types.err; return Some(tcx.types.err);
} }
Def::Err => tcx.types.err, Def::Err => tcx.types.err,
x => { x => {

View file

@ -1474,7 +1474,7 @@ impl GenericParamDefKind {
} }
} }
pub fn get_type(&self, cx: &DocContext<'_, '_, '_>) -> Option<Type> { pub fn get_type(&self, cx: &DocContext<'_>) -> Option<Type> {
match *self { match *self {
GenericParamDefKind::Type { did, .. } => { GenericParamDefKind::Type { did, .. } => {
rustc_typeck::checked_type_of(cx.tcx, did, false).map(|t| t.clean(cx)) rustc_typeck::checked_type_of(cx.tcx, did, false).map(|t| t.clean(cx))
@ -1505,7 +1505,7 @@ impl GenericParamDef {
self.kind.is_type() self.kind.is_type()
} }
pub fn get_type(&self, cx: &DocContext<'_, '_, '_>) -> Option<Type> { pub fn get_type(&self, cx: &DocContext<'_>) -> Option<Type> {
self.kind.get_type(cx) self.kind.get_type(cx)
} }
@ -1750,12 +1750,16 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics,
} }
} }
// The point is to replace bounds with types. /// The point of this function is to replace bounds with types.
///
/// i.e. `[T, U]` when you have the following bounds: `T: Display, U: Option<T>` will return
/// `[Display, Option]` (we just returns the list of the types, we don't care about the
/// wrapped types in here).
fn get_real_types( fn get_real_types(
generics: &Generics, generics: &Generics,
arg: &Type, arg: &Type,
cx: &DocContext<'_, '_, '_>, cx: &DocContext<'_>,
) -> Vec<Type> { ) -> FxHashSet<Type> {
let arg_s = arg.to_string(); let arg_s = arg.to_string();
let mut res = Vec::new(); let mut res = Vec::new();
if arg.is_full_generic() { if arg.is_full_generic() {
@ -1776,7 +1780,7 @@ fn get_real_types(
if let Some(ty) = x.get_type(cx) { if let Some(ty) = x.get_type(cx) {
let mut adds = get_real_types(generics, &ty, cx); let mut adds = get_real_types(generics, &ty, cx);
if !adds.is_empty() { if !adds.is_empty() {
res.append(&mut adds); res.extend(adds);
} else if !ty.is_full_generic() { } else if !ty.is_full_generic() {
res.push(ty); res.push(ty);
} }
@ -1794,7 +1798,7 @@ fn get_real_types(
if let Some(ty) = bound.get_trait_type() { if let Some(ty) = bound.get_trait_type() {
let mut adds = get_real_types(generics, &ty, cx); let mut adds = get_real_types(generics, &ty, cx);
if !adds.is_empty() { if !adds.is_empty() {
res.append(&mut adds); res.extend(adds);
} else if !ty.is_full_generic() { } else if !ty.is_full_generic() {
res.push(ty.clone()); res.push(ty.clone());
} }
@ -1808,7 +1812,7 @@ fn get_real_types(
if gen.is_full_generic() { if gen.is_full_generic() {
let mut adds = get_real_types(generics, gen, cx); let mut adds = get_real_types(generics, gen, cx);
if !adds.is_empty() { if !adds.is_empty() {
res.append(&mut adds); res.extend(adds);
} }
} else { } else {
res.push(gen.clone()); res.push(gen.clone());
@ -1819,10 +1823,14 @@ fn get_real_types(
res res
} }
/// Return the full list of types when bounds have been resolved.
///
/// i.e. `fn foo<A: Display, B: Option<A>>(x: u32, y: B)` will return
/// `[u32, Display, Option]`.
pub fn get_all_types( pub fn get_all_types(
generics: &Generics, generics: &Generics,
decl: &FnDecl, decl: &FnDecl,
cx: &DocContext<'_, '_, '_>, cx: &DocContext<'_>,
) -> (Vec<Type>, 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() {
@ -1831,7 +1839,7 @@ pub fn get_all_types(
} }
let mut args = get_real_types(generics, &arg.type_, cx); let mut args = get_real_types(generics, &arg.type_, cx);
if !args.is_empty() { if !args.is_empty() {
all_types.append(&mut args); all_types.extend(args);
} else { } else {
all_types.push(arg.type_.clone()); all_types.push(arg.type_.clone());
} }

View file

@ -5052,7 +5052,6 @@ fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
Some(output) Some(output)
}; };
println!("===> {:?}", output);
Some(IndexItemFunctionType { inputs, output }) Some(IndexItemFunctionType { inputs, output })
} }