Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some function fields #80883

Merged
merged 6 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,10 @@ fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
let (generics, decl) = clean::enter_impl_trait(cx, || {
((cx.tcx.generics_of(did), predicates).clean(cx), (did, sig).clean(cx))
});
let (all_types, ret_types) = clean::get_all_types(&generics, &decl, cx);
clean::Function {
decl,
generics,
header: hir::FnHeader { unsafety: sig.unsafety(), abi: sig.abi(), constness, asyncness },
all_types,
ret_types,
}
}

Expand Down
31 changes: 3 additions & 28 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,7 @@ impl<'a> Clean<Function> for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::Bo
fn clean(&self, cx: &DocContext<'_>) -> Function {
let (generics, decl) =
enter_impl_trait(cx, || (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx)));
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
Function { decl, generics, header: self.0.header, all_types, ret_types }
Function { decl, generics, header: self.0.header }
}
}

Expand Down Expand Up @@ -1043,21 +1042,7 @@ impl Clean<PolyTrait> for hir::PolyTraitRef<'_> {

impl Clean<TypeKind> for hir::def::DefKind {
fn clean(&self, _: &DocContext<'_>) -> TypeKind {
match *self {
hir::def::DefKind::Mod => TypeKind::Module,
hir::def::DefKind::Struct => TypeKind::Struct,
hir::def::DefKind::Union => TypeKind::Union,
hir::def::DefKind::Enum => TypeKind::Enum,
hir::def::DefKind::Trait => TypeKind::Trait,
hir::def::DefKind::TyAlias => TypeKind::Typedef,
hir::def::DefKind::ForeignTy => TypeKind::Foreign,
hir::def::DefKind::TraitAlias => TypeKind::TraitAlias,
hir::def::DefKind::Fn => TypeKind::Function,
hir::def::DefKind::Const => TypeKind::Const,
hir::def::DefKind::Static => TypeKind::Static,
hir::def::DefKind::Macro(_) => TypeKind::Macro,
_ => TypeKind::Foreign,
}
(*self).into()
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -1082,9 +1067,7 @@ impl Clean<Item> for hir::TraitItem<'_> {
let (generics, decl) = enter_impl_trait(cx, || {
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
});
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
let mut t =
Function { header: sig.header, decl, generics, all_types, ret_types };
let mut t = Function { header: sig.header, decl, generics };
if t.header.constness == hir::Constness::Const
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
{
Expand Down Expand Up @@ -1196,7 +1179,6 @@ impl Clean<Item> for ty::AssocItem {
ty::ImplContainer(_) => true,
ty::TraitContainer(_) => self.defaultness.has_value(),
};
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
if provided {
let constness = if is_min_const_fn(cx.tcx, self.def_id) {
hir::Constness::Const
Expand All @@ -1218,8 +1200,6 @@ impl Clean<Item> for ty::AssocItem {
constness,
asyncness,
},
all_types,
ret_types,
},
defaultness,
)
Expand All @@ -1233,8 +1213,6 @@ impl Clean<Item> for ty::AssocItem {
constness: hir::Constness::NotConst,
asyncness: hir::IsAsync::NotAsync,
},
all_types,
ret_types,
})
}
}
Expand Down Expand Up @@ -2274,7 +2252,6 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {
let (generics, decl) = enter_impl_trait(cx, || {
(generics.clean(cx), (&**decl, &names[..]).clean(cx))
});
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
ForeignFunctionItem(Function {
decl,
generics,
Expand All @@ -2284,8 +2261,6 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {
constness: hir::Constness::NotConst,
asyncness: hir::IsAsync::NotAsync,
},
all_types,
ret_types,
})
}
hir::ForeignItemKind::Static(ref ty, mutability) => ForeignStaticItem(Static {
Expand Down
43 changes: 39 additions & 4 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,8 +1087,6 @@ crate struct Function {
crate decl: FnDecl,
crate generics: Generics,
crate header: hir::FnHeader,
crate all_types: Vec<(Type, TypeKind)>,
crate ret_types: Vec<(Type, TypeKind)>,
}

#[derive(Clone, PartialEq, Eq, Debug, Hash)]
Expand Down Expand Up @@ -1304,6 +1302,43 @@ crate enum TypeKind {
Primitive,
}

impl From<hir::def::DefKind> for TypeKind {
fn from(other: hir::def::DefKind) -> Self {
match other {
hir::def::DefKind::Enum => Self::Enum,
hir::def::DefKind::Fn => Self::Function,
hir::def::DefKind::Mod => Self::Module,
hir::def::DefKind::Const => Self::Const,
hir::def::DefKind::Static => Self::Static,
hir::def::DefKind::Struct => Self::Struct,
hir::def::DefKind::Union => Self::Union,
hir::def::DefKind::Trait => Self::Trait,
hir::def::DefKind::TyAlias => Self::Typedef,
hir::def::DefKind::TraitAlias => Self::TraitAlias,
hir::def::DefKind::Macro(_) => Self::Macro,
hir::def::DefKind::ForeignTy
| hir::def::DefKind::Variant
| hir::def::DefKind::AssocTy
| hir::def::DefKind::TyParam
| hir::def::DefKind::ConstParam
| hir::def::DefKind::Ctor(..)
| hir::def::DefKind::AssocFn
| hir::def::DefKind::AssocConst
| hir::def::DefKind::ExternCrate
| hir::def::DefKind::Use
| hir::def::DefKind::ForeignMod
| hir::def::DefKind::AnonConst
| hir::def::DefKind::OpaqueTy
| hir::def::DefKind::Field
| hir::def::DefKind::LifetimeParam
| hir::def::DefKind::GlobalAsm
| hir::def::DefKind::Impl
| hir::def::DefKind::Closure
| hir::def::DefKind::Generator => Self::Foreign,
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

crate trait GetDefId {
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
/// This will return [`None`] when called on a primitive [`clean::Type`].
Expand Down Expand Up @@ -1367,14 +1402,14 @@ impl Type {
}
}

crate fn generics(&self) -> Option<Vec<Type>> {
crate fn generics(&self) -> Option<Vec<&Type>> {
match *self {
ResolvedPath { ref path, .. } => path.segments.last().and_then(|seg| {
if let GenericArgs::AngleBracketed { ref args, .. } = seg.args {
Some(
args.iter()
.filter_map(|arg| match arg {
GenericArg::Type(ty) => Some(ty.clone()),
GenericArg::Type(ty) => Some(ty),
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
_ => None,
})
.collect(),
Expand Down
126 changes: 3 additions & 123 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::clean::auto_trait::AutoTraitFinder;
use crate::clean::blanket_impl::BlanketImplFinder;
use crate::clean::{
inline, Clean, Crate, ExternalCrate, FnDecl, FnRetTy, Generic, GenericArg, GenericArgs,
GenericBound, Generics, GetDefId, ImportSource, Item, ItemKind, Lifetime, MacroKind, Path,
PathSegment, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding, TypeKind,
WherePredicate,
inline, Clean, Crate, ExternalCrate, Generic, GenericArg, GenericArgs, ImportSource, Item,
ItemKind, Lifetime, MacroKind, Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Type,
TypeBinding, TypeKind,
};
use crate::core::DocContext;

Expand Down Expand Up @@ -160,125 +159,6 @@ pub(super) fn external_path(
}
}

/// 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).
crate fn get_real_types(
generics: &Generics,
arg: &Type,
cx: &DocContext<'_>,
recurse: i32,
) -> FxHashSet<(Type, TypeKind)> {
fn insert(res: &mut FxHashSet<(Type, TypeKind)>, cx: &DocContext<'_>, ty: Type) {
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
res.insert((ty, kind));
} else if ty.is_primitive() {
// This is a primitive, let's store it as such.
res.insert((ty, TypeKind::Primitive));
}
}
let mut res = FxHashSet::default();
if recurse >= 10 {
// FIXME: remove this whole recurse thing when the recursion bug is fixed
return res;
}

if arg.is_full_generic() {
let arg_s = Symbol::intern(&arg.print(&cx.cache).to_string());
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
WherePredicate::BoundPredicate { ty, .. } => ty.def_id() == arg.def_id(),
_ => false,
}) {
let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
for bound in bounds.iter() {
if let GenericBound::TraitBound(poly_trait, _) = bound {
for x in poly_trait.generic_params.iter() {
if !x.is_type() {
continue;
}
if let Some(ty) = x.get_type() {
let adds = get_real_types(generics, &ty, cx, recurse + 1);
if !adds.is_empty() {
res.extend(adds);
} else if !ty.is_full_generic() {
insert(&mut res, cx, ty);
}
}
}
}
}
}
if let Some(bound) = generics.params.iter().find(|g| g.is_type() && g.name == arg_s) {
for bound in bound.get_bounds().unwrap_or_else(|| &[]) {
if let Some(ty) = bound.get_trait_type() {
let adds = get_real_types(generics, &ty, cx, recurse + 1);
if !adds.is_empty() {
res.extend(adds);
} else if !ty.is_full_generic() {
insert(&mut res, cx, ty);
}
}
}
}
} else {
insert(&mut res, cx, arg.clone());
if let Some(gens) = arg.generics() {
for gen in gens.iter() {
if gen.is_full_generic() {
let adds = get_real_types(generics, gen, cx, recurse + 1);
if !adds.is_empty() {
res.extend(adds);
}
} else {
insert(&mut res, cx, gen.clone());
}
}
}
}
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]`.
crate fn get_all_types(
generics: &Generics,
decl: &FnDecl,
cx: &DocContext<'_>,
) -> (Vec<(Type, TypeKind)>, Vec<(Type, TypeKind)>) {
let mut all_types = FxHashSet::default();
for arg in decl.inputs.values.iter() {
if arg.type_.is_self_type() {
continue;
}
let args = get_real_types(generics, &arg.type_, cx, 0);
if !args.is_empty() {
all_types.extend(args);
} else {
if let Some(kind) = arg.type_.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
all_types.insert((arg.type_.clone(), kind));
}
}
}

let ret_types = match decl.output {
FnRetTy::Return(ref return_type) => {
let mut ret = get_real_types(generics, &return_type, cx, 0);
if ret.is_empty() {
if let Some(kind) = return_type.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
ret.insert((return_type.clone(), kind));
}
}
ret.into_iter().collect()
}
_ => Vec::new(),
};
(all_types.into_iter().collect(), ret_types)
}

crate fn strip_type(ty: Type) -> Type {
match ty {
Type::ResolvedPath { path, param_names, did, is_generic } => {
Expand Down
Loading