diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index e723ee1d10ad..095c92ff10d8 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -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() } +/// 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>( tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, @@ -1382,14 +1386,14 @@ pub fn checked_type_of<'a, 'tcx>( for param in &generics.params { if let ty::GenericParamDefKind::Const = param.kind { if param_index == arg_index { - return tcx.type_of(param.def_id); + return Some(tcx.type_of(param.def_id)); } param_index += 1; } } // This is no generic parameter associated with the arg. This is // probably from an extra arg where one is not needed. - return tcx.types.err; + return Some(tcx.types.err); } Def::Err => tcx.types.err, x => { diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index c41d02fbfbbc..e7277308cd99 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1474,7 +1474,7 @@ impl GenericParamDefKind { } } - pub fn get_type(&self, cx: &DocContext<'_, '_, '_>) -> Option { + pub fn get_type(&self, cx: &DocContext<'_>) -> Option { match *self { GenericParamDefKind::Type { did, .. } => { rustc_typeck::checked_type_of(cx.tcx, did, false).map(|t| t.clean(cx)) @@ -1505,7 +1505,7 @@ impl GenericParamDef { self.kind.is_type() } - pub fn get_type(&self, cx: &DocContext<'_, '_, '_>) -> Option { + pub fn get_type(&self, cx: &DocContext<'_>) -> Option { self.kind.get_type(cx) } @@ -1750,12 +1750,16 @@ impl<'a, 'tcx> Clean 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` 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( generics: &Generics, arg: &Type, - cx: &DocContext<'_, '_, '_>, -) -> Vec { + cx: &DocContext<'_>, +) -> FxHashSet { let arg_s = arg.to_string(); let mut res = Vec::new(); if arg.is_full_generic() { @@ -1776,7 +1780,7 @@ fn get_real_types( if let Some(ty) = x.get_type(cx) { let mut adds = get_real_types(generics, &ty, cx); if !adds.is_empty() { - res.append(&mut adds); + res.extend(adds); } else if !ty.is_full_generic() { res.push(ty); } @@ -1794,7 +1798,7 @@ fn get_real_types( if let Some(ty) = bound.get_trait_type() { let mut adds = get_real_types(generics, &ty, cx); if !adds.is_empty() { - res.append(&mut adds); + res.extend(adds); } else if !ty.is_full_generic() { res.push(ty.clone()); } @@ -1808,7 +1812,7 @@ fn get_real_types( if gen.is_full_generic() { let mut adds = get_real_types(generics, gen, cx); if !adds.is_empty() { - res.append(&mut adds); + res.extend(adds); } } else { res.push(gen.clone()); @@ -1819,10 +1823,14 @@ fn get_real_types( res } +/// Return the full list of types when bounds have been resolved. +/// +/// i.e. `fn foo>(x: u32, y: B)` will return +/// `[u32, Display, Option]`. pub fn get_all_types( generics: &Generics, decl: &FnDecl, - cx: &DocContext<'_, '_, '_>, + cx: &DocContext<'_>, ) -> (Vec, Vec) { let mut all_types = Vec::new(); 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); if !args.is_empty() { - all_types.append(&mut args); + all_types.extend(args); } else { all_types.push(arg.type_.clone()); } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 930f3fd3c5c8..b0f0a26bd27b 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -5052,7 +5052,6 @@ fn get_index_search_type(item: &clean::Item) -> Option { Some(output) }; - println!("===> {:?}", output); Some(IndexItemFunctionType { inputs, output }) }