Skip to content

Commit a73c2e5

Browse files
committed
Auto merge of #80883 - GuillaumeGomez:remove-some-function-fields, r=ollie27
Remove some function fields Same kind as #80845. This PR removes the `all_types` and `ret_types` from the `clean::Function` type. Another change that I had to do was implementing the `From` trait to be able to convert `hir::def::DefKind` into `clean::TypeKind` without requiring `DocContext` (and so I updated the `clean` method so that it's taken into account). The last two commits improve a bit the `get_real_types` function and the `Type::generics` method. r? `@jyn514`
2 parents 9a1d617 + c92b161 commit a73c2e5

File tree

9 files changed

+246
-220
lines changed

9 files changed

+246
-220
lines changed

src/librustdoc/clean/inline.rs

-3
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,10 @@ fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
217217
let (generics, decl) = clean::enter_impl_trait(cx, || {
218218
((cx.tcx.generics_of(did), predicates).clean(cx), (did, sig).clean(cx))
219219
});
220-
let (all_types, ret_types) = clean::get_all_types(&generics, &decl, cx);
221220
clean::Function {
222221
decl,
223222
generics,
224223
header: hir::FnHeader { unsafety: sig.unsafety(), abi: sig.abi(), constness, asyncness },
225-
all_types,
226-
ret_types,
227224
}
228225
}
229226

src/librustdoc/clean/mod.rs

+3-28
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,7 @@ impl<'a> Clean<Function> for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::Bo
928928
fn clean(&self, cx: &DocContext<'_>) -> Function {
929929
let (generics, decl) =
930930
enter_impl_trait(cx, || (self.1.clean(cx), (&*self.0.decl, self.2).clean(cx)));
931-
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
932-
Function { decl, generics, header: self.0.header, all_types, ret_types }
931+
Function { decl, generics, header: self.0.header }
933932
}
934933
}
935934

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

10441043
impl Clean<TypeKind> for hir::def::DefKind {
10451044
fn clean(&self, _: &DocContext<'_>) -> TypeKind {
1046-
match *self {
1047-
hir::def::DefKind::Mod => TypeKind::Module,
1048-
hir::def::DefKind::Struct => TypeKind::Struct,
1049-
hir::def::DefKind::Union => TypeKind::Union,
1050-
hir::def::DefKind::Enum => TypeKind::Enum,
1051-
hir::def::DefKind::Trait => TypeKind::Trait,
1052-
hir::def::DefKind::TyAlias => TypeKind::Typedef,
1053-
hir::def::DefKind::ForeignTy => TypeKind::Foreign,
1054-
hir::def::DefKind::TraitAlias => TypeKind::TraitAlias,
1055-
hir::def::DefKind::Fn => TypeKind::Function,
1056-
hir::def::DefKind::Const => TypeKind::Const,
1057-
hir::def::DefKind::Static => TypeKind::Static,
1058-
hir::def::DefKind::Macro(_) => TypeKind::Macro,
1059-
_ => TypeKind::Foreign,
1060-
}
1045+
(*self).into()
10611046
}
10621047
}
10631048

@@ -1082,9 +1067,7 @@ impl Clean<Item> for hir::TraitItem<'_> {
10821067
let (generics, decl) = enter_impl_trait(cx, || {
10831068
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
10841069
});
1085-
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1086-
let mut t =
1087-
Function { header: sig.header, decl, generics, all_types, ret_types };
1070+
let mut t = Function { header: sig.header, decl, generics };
10881071
if t.header.constness == hir::Constness::Const
10891072
&& is_unstable_const_fn(cx.tcx, local_did).is_some()
10901073
{
@@ -1196,7 +1179,6 @@ impl Clean<Item> for ty::AssocItem {
11961179
ty::ImplContainer(_) => true,
11971180
ty::TraitContainer(_) => self.defaultness.has_value(),
11981181
};
1199-
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
12001182
if provided {
12011183
let constness = if is_min_const_fn(cx.tcx, self.def_id) {
12021184
hir::Constness::Const
@@ -1218,8 +1200,6 @@ impl Clean<Item> for ty::AssocItem {
12181200
constness,
12191201
asyncness,
12201202
},
1221-
all_types,
1222-
ret_types,
12231203
},
12241204
defaultness,
12251205
)
@@ -1233,8 +1213,6 @@ impl Clean<Item> for ty::AssocItem {
12331213
constness: hir::Constness::NotConst,
12341214
asyncness: hir::IsAsync::NotAsync,
12351215
},
1236-
all_types,
1237-
ret_types,
12381216
})
12391217
}
12401218
}
@@ -2274,7 +2252,6 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {
22742252
let (generics, decl) = enter_impl_trait(cx, || {
22752253
(generics.clean(cx), (&**decl, &names[..]).clean(cx))
22762254
});
2277-
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
22782255
ForeignFunctionItem(Function {
22792256
decl,
22802257
generics,
@@ -2284,8 +2261,6 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {
22842261
constness: hir::Constness::NotConst,
22852262
asyncness: hir::IsAsync::NotAsync,
22862263
},
2287-
all_types,
2288-
ret_types,
22892264
})
22902265
}
22912266
hir::ForeignItemKind::Static(ref ty, mutability) => ForeignStaticItem(Static {

src/librustdoc/clean/types.rs

+39-4
Original file line numberDiff line numberDiff line change
@@ -1087,8 +1087,6 @@ crate struct Function {
10871087
crate decl: FnDecl,
10881088
crate generics: Generics,
10891089
crate header: hir::FnHeader,
1090-
crate all_types: Vec<(Type, TypeKind)>,
1091-
crate ret_types: Vec<(Type, TypeKind)>,
10921090
}
10931091

10941092
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
@@ -1304,6 +1302,43 @@ crate enum TypeKind {
13041302
Primitive,
13051303
}
13061304

1305+
impl From<hir::def::DefKind> for TypeKind {
1306+
fn from(other: hir::def::DefKind) -> Self {
1307+
match other {
1308+
hir::def::DefKind::Enum => Self::Enum,
1309+
hir::def::DefKind::Fn => Self::Function,
1310+
hir::def::DefKind::Mod => Self::Module,
1311+
hir::def::DefKind::Const => Self::Const,
1312+
hir::def::DefKind::Static => Self::Static,
1313+
hir::def::DefKind::Struct => Self::Struct,
1314+
hir::def::DefKind::Union => Self::Union,
1315+
hir::def::DefKind::Trait => Self::Trait,
1316+
hir::def::DefKind::TyAlias => Self::Typedef,
1317+
hir::def::DefKind::TraitAlias => Self::TraitAlias,
1318+
hir::def::DefKind::Macro(_) => Self::Macro,
1319+
hir::def::DefKind::ForeignTy
1320+
| hir::def::DefKind::Variant
1321+
| hir::def::DefKind::AssocTy
1322+
| hir::def::DefKind::TyParam
1323+
| hir::def::DefKind::ConstParam
1324+
| hir::def::DefKind::Ctor(..)
1325+
| hir::def::DefKind::AssocFn
1326+
| hir::def::DefKind::AssocConst
1327+
| hir::def::DefKind::ExternCrate
1328+
| hir::def::DefKind::Use
1329+
| hir::def::DefKind::ForeignMod
1330+
| hir::def::DefKind::AnonConst
1331+
| hir::def::DefKind::OpaqueTy
1332+
| hir::def::DefKind::Field
1333+
| hir::def::DefKind::LifetimeParam
1334+
| hir::def::DefKind::GlobalAsm
1335+
| hir::def::DefKind::Impl
1336+
| hir::def::DefKind::Closure
1337+
| hir::def::DefKind::Generator => Self::Foreign,
1338+
}
1339+
}
1340+
}
1341+
13071342
crate trait GetDefId {
13081343
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
13091344
/// This will return [`None`] when called on a primitive [`clean::Type`].
@@ -1367,14 +1402,14 @@ impl Type {
13671402
}
13681403
}
13691404

1370-
crate fn generics(&self) -> Option<Vec<Type>> {
1405+
crate fn generics(&self) -> Option<Vec<&Type>> {
13711406
match *self {
13721407
ResolvedPath { ref path, .. } => path.segments.last().and_then(|seg| {
13731408
if let GenericArgs::AngleBracketed { ref args, .. } = seg.args {
13741409
Some(
13751410
args.iter()
13761411
.filter_map(|arg| match arg {
1377-
GenericArg::Type(ty) => Some(ty.clone()),
1412+
GenericArg::Type(ty) => Some(ty),
13781413
_ => None,
13791414
})
13801415
.collect(),

src/librustdoc/clean/utils.rs

+3-123
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::clean::auto_trait::AutoTraitFinder;
22
use crate::clean::blanket_impl::BlanketImplFinder;
33
use crate::clean::{
4-
inline, Clean, Crate, ExternalCrate, FnDecl, FnRetTy, Generic, GenericArg, GenericArgs,
5-
GenericBound, Generics, GetDefId, ImportSource, Item, ItemKind, Lifetime, MacroKind, Path,
6-
PathSegment, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding, TypeKind,
7-
WherePredicate,
4+
inline, Clean, Crate, ExternalCrate, Generic, GenericArg, GenericArgs, ImportSource, Item,
5+
ItemKind, Lifetime, MacroKind, Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Type,
6+
TypeBinding, TypeKind,
87
};
98
use crate::core::DocContext;
109

@@ -160,125 +159,6 @@ pub(super) fn external_path(
160159
}
161160
}
162161

163-
/// The point of this function is to replace bounds with types.
164-
///
165-
/// i.e. `[T, U]` when you have the following bounds: `T: Display, U: Option<T>` will return
166-
/// `[Display, Option]` (we just returns the list of the types, we don't care about the
167-
/// wrapped types in here).
168-
crate fn get_real_types(
169-
generics: &Generics,
170-
arg: &Type,
171-
cx: &DocContext<'_>,
172-
recurse: i32,
173-
) -> FxHashSet<(Type, TypeKind)> {
174-
fn insert(res: &mut FxHashSet<(Type, TypeKind)>, cx: &DocContext<'_>, ty: Type) {
175-
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
176-
res.insert((ty, kind));
177-
} else if ty.is_primitive() {
178-
// This is a primitive, let's store it as such.
179-
res.insert((ty, TypeKind::Primitive));
180-
}
181-
}
182-
let mut res = FxHashSet::default();
183-
if recurse >= 10 {
184-
// FIXME: remove this whole recurse thing when the recursion bug is fixed
185-
return res;
186-
}
187-
188-
if arg.is_full_generic() {
189-
let arg_s = Symbol::intern(&arg.print(&cx.cache).to_string());
190-
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
191-
WherePredicate::BoundPredicate { ty, .. } => ty.def_id() == arg.def_id(),
192-
_ => false,
193-
}) {
194-
let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
195-
for bound in bounds.iter() {
196-
if let GenericBound::TraitBound(poly_trait, _) = bound {
197-
for x in poly_trait.generic_params.iter() {
198-
if !x.is_type() {
199-
continue;
200-
}
201-
if let Some(ty) = x.get_type() {
202-
let adds = get_real_types(generics, &ty, cx, recurse + 1);
203-
if !adds.is_empty() {
204-
res.extend(adds);
205-
} else if !ty.is_full_generic() {
206-
insert(&mut res, cx, ty);
207-
}
208-
}
209-
}
210-
}
211-
}
212-
}
213-
if let Some(bound) = generics.params.iter().find(|g| g.is_type() && g.name == arg_s) {
214-
for bound in bound.get_bounds().unwrap_or_else(|| &[]) {
215-
if let Some(ty) = bound.get_trait_type() {
216-
let adds = get_real_types(generics, &ty, cx, recurse + 1);
217-
if !adds.is_empty() {
218-
res.extend(adds);
219-
} else if !ty.is_full_generic() {
220-
insert(&mut res, cx, ty);
221-
}
222-
}
223-
}
224-
}
225-
} else {
226-
insert(&mut res, cx, arg.clone());
227-
if let Some(gens) = arg.generics() {
228-
for gen in gens.iter() {
229-
if gen.is_full_generic() {
230-
let adds = get_real_types(generics, gen, cx, recurse + 1);
231-
if !adds.is_empty() {
232-
res.extend(adds);
233-
}
234-
} else {
235-
insert(&mut res, cx, gen.clone());
236-
}
237-
}
238-
}
239-
}
240-
res
241-
}
242-
243-
/// Return the full list of types when bounds have been resolved.
244-
///
245-
/// i.e. `fn foo<A: Display, B: Option<A>>(x: u32, y: B)` will return
246-
/// `[u32, Display, Option]`.
247-
crate fn get_all_types(
248-
generics: &Generics,
249-
decl: &FnDecl,
250-
cx: &DocContext<'_>,
251-
) -> (Vec<(Type, TypeKind)>, Vec<(Type, TypeKind)>) {
252-
let mut all_types = FxHashSet::default();
253-
for arg in decl.inputs.values.iter() {
254-
if arg.type_.is_self_type() {
255-
continue;
256-
}
257-
let args = get_real_types(generics, &arg.type_, cx, 0);
258-
if !args.is_empty() {
259-
all_types.extend(args);
260-
} else {
261-
if let Some(kind) = arg.type_.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
262-
all_types.insert((arg.type_.clone(), kind));
263-
}
264-
}
265-
}
266-
267-
let ret_types = match decl.output {
268-
FnRetTy::Return(ref return_type) => {
269-
let mut ret = get_real_types(generics, &return_type, cx, 0);
270-
if ret.is_empty() {
271-
if let Some(kind) = return_type.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
272-
ret.insert((return_type.clone(), kind));
273-
}
274-
}
275-
ret.into_iter().collect()
276-
}
277-
_ => Vec::new(),
278-
};
279-
(all_types.into_iter().collect(), ret_types)
280-
}
281-
282162
crate fn strip_type(ty: Type) -> Type {
283163
match ty {
284164
Type::ResolvedPath { path, param_names, did, is_generic } => {

0 commit comments

Comments
 (0)