]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/json/conversions.rs
Rollup merge of #84616 - GuillaumeGomez:fix-empty-dom-toggle, r=jsha
[rust.git] / src / librustdoc / json / conversions.rs
1 //! These from impls are used to create the JSON types which get serialized. They're very close to
2 //! the `clean` types but with some fields removed or stringified to simplify the output and not
3 //! expose unstable compiler internals.
4
5 #![allow(rustc::default_hash_types)]
6
7 use std::convert::From;
8
9 use rustc_ast::ast;
10 use rustc_hir::def::CtorKind;
11 use rustc_middle::ty::TyCtxt;
12 use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
13 use rustc_span::Pos;
14
15 use rustdoc_json_types::*;
16
17 use crate::clean;
18 use crate::clean::utils::print_const_expr;
19 use crate::formats::item_type::ItemType;
20 use crate::json::JsonRenderer;
21 use std::collections::HashSet;
22
23 impl JsonRenderer<'_> {
24     pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
25         let deprecation = item.deprecation(self.tcx);
26         let links = self
27             .cache
28             .intra_doc_links
29             .get(&item.def_id)
30             .into_iter()
31             .flatten()
32             .filter_map(|clean::ItemLink { link, did, .. }| {
33                 did.map(|did| (link.clone(), from_def_id(did)))
34             })
35             .collect();
36         let docs = item.attrs.collapsed_doc_value();
37         let attrs = item
38             .attrs
39             .other_attrs
40             .iter()
41             .map(rustc_ast_pretty::pprust::attribute_to_string)
42             .collect();
43         let span = item.span(self.tcx);
44         let clean::Item { name, attrs: _, kind: _, visibility, def_id, cfg: _ } = item;
45         let inner = match *item.kind {
46             clean::StrippedItem(_) => return None,
47             _ => from_clean_item(item, self.tcx),
48         };
49         Some(Item {
50             id: from_def_id(def_id),
51             crate_id: def_id.krate.as_u32(),
52             name: name.map(|sym| sym.to_string()),
53             span: self.convert_span(span),
54             visibility: self.convert_visibility(visibility),
55             docs,
56             attrs,
57             deprecation: deprecation.map(from_deprecation),
58             inner,
59             links,
60         })
61     }
62
63     fn convert_span(&self, span: clean::Span) -> Option<Span> {
64         match span.filename(self.sess()) {
65             rustc_span::FileName::Real(name) => {
66                 let hi = span.hi(self.sess());
67                 let lo = span.lo(self.sess());
68                 Some(Span {
69                     filename: match name {
70                         rustc_span::RealFileName::Named(path) => path,
71                         rustc_span::RealFileName::Devirtualized { local_path, virtual_name: _ } => {
72                             local_path
73                         }
74                     },
75                     begin: (lo.line, lo.col.to_usize()),
76                     end: (hi.line, hi.col.to_usize()),
77                 })
78             }
79             _ => None,
80         }
81     }
82
83     fn convert_visibility(&self, v: clean::Visibility) -> Visibility {
84         use clean::Visibility::*;
85         match v {
86             Public => Visibility::Public,
87             Inherited => Visibility::Default,
88             Restricted(did) if did.index == CRATE_DEF_INDEX => Visibility::Crate,
89             Restricted(did) => Visibility::Restricted {
90                 parent: from_def_id(did),
91                 path: self.tcx.def_path(did).to_string_no_crate_verbose(),
92             },
93         }
94     }
95 }
96
97 crate trait FromWithTcx<T> {
98     fn from_tcx(f: T, tcx: TyCtxt<'_>) -> Self;
99 }
100
101 crate trait IntoWithTcx<T> {
102     fn into_tcx(self, tcx: TyCtxt<'_>) -> T;
103 }
104
105 impl<T, U> IntoWithTcx<U> for T
106 where
107     U: FromWithTcx<T>,
108 {
109     fn into_tcx(self, tcx: TyCtxt<'_>) -> U {
110         U::from_tcx(self, tcx)
111     }
112 }
113
114 crate fn from_deprecation(deprecation: rustc_attr::Deprecation) -> Deprecation {
115     #[rustfmt::skip]
116     let rustc_attr::Deprecation { since, note, is_since_rustc_version: _, suggestion: _ } = deprecation;
117     Deprecation { since: since.map(|s| s.to_string()), note: note.map(|s| s.to_string()) }
118 }
119
120 impl FromWithTcx<clean::GenericArgs> for GenericArgs {
121     fn from_tcx(args: clean::GenericArgs, tcx: TyCtxt<'_>) -> Self {
122         use clean::GenericArgs::*;
123         match args {
124             AngleBracketed { args, bindings } => GenericArgs::AngleBracketed {
125                 args: args.into_iter().map(|a| a.into_tcx(tcx)).collect(),
126                 bindings: bindings.into_iter().map(|a| a.into_tcx(tcx)).collect(),
127             },
128             Parenthesized { inputs, output } => GenericArgs::Parenthesized {
129                 inputs: inputs.into_iter().map(|a| a.into_tcx(tcx)).collect(),
130                 output: output.map(|a| a.into_tcx(tcx)),
131             },
132         }
133     }
134 }
135
136 impl FromWithTcx<clean::GenericArg> for GenericArg {
137     fn from_tcx(arg: clean::GenericArg, tcx: TyCtxt<'_>) -> Self {
138         use clean::GenericArg::*;
139         match arg {
140             Lifetime(l) => GenericArg::Lifetime(l.0.to_string()),
141             Type(t) => GenericArg::Type(t.into_tcx(tcx)),
142             Const(c) => GenericArg::Const(c.into_tcx(tcx)),
143         }
144     }
145 }
146
147 impl FromWithTcx<clean::Constant> for Constant {
148     fn from_tcx(constant: clean::Constant, tcx: TyCtxt<'_>) -> Self {
149         let expr = constant.expr(tcx);
150         let value = constant.value(tcx);
151         let is_literal = constant.is_literal(tcx);
152         Constant { type_: constant.type_.into_tcx(tcx), expr, value, is_literal }
153     }
154 }
155
156 impl FromWithTcx<clean::TypeBinding> for TypeBinding {
157     fn from_tcx(binding: clean::TypeBinding, tcx: TyCtxt<'_>) -> Self {
158         TypeBinding { name: binding.name.to_string(), binding: binding.kind.into_tcx(tcx) }
159     }
160 }
161
162 impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
163     fn from_tcx(kind: clean::TypeBindingKind, tcx: TyCtxt<'_>) -> Self {
164         use clean::TypeBindingKind::*;
165         match kind {
166             Equality { ty } => TypeBindingKind::Equality(ty.into_tcx(tcx)),
167             Constraint { bounds } => {
168                 TypeBindingKind::Constraint(bounds.into_iter().map(|a| a.into_tcx(tcx)).collect())
169             }
170         }
171     }
172 }
173
174 crate fn from_def_id(did: DefId) -> Id {
175     Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index)))
176 }
177
178 fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
179     use clean::ItemKind::*;
180     let name = item.name;
181     let is_crate = item.is_crate();
182     match *item.kind {
183         ModuleItem(m) => ItemEnum::Module(Module { is_crate, items: ids(m.items) }),
184         ImportItem(i) => ItemEnum::Import(i.into_tcx(tcx)),
185         StructItem(s) => ItemEnum::Struct(s.into_tcx(tcx)),
186         UnionItem(u) => ItemEnum::Union(u.into_tcx(tcx)),
187         StructFieldItem(f) => ItemEnum::StructField(f.into_tcx(tcx)),
188         EnumItem(e) => ItemEnum::Enum(e.into_tcx(tcx)),
189         VariantItem(v) => ItemEnum::Variant(v.into_tcx(tcx)),
190         FunctionItem(f) => ItemEnum::Function(f.into_tcx(tcx)),
191         ForeignFunctionItem(f) => ItemEnum::Function(f.into_tcx(tcx)),
192         TraitItem(t) => ItemEnum::Trait(t.into_tcx(tcx)),
193         TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_tcx(tcx)),
194         MethodItem(m, _) => ItemEnum::Method(from_function_method(m, true, tcx)),
195         TyMethodItem(m) => ItemEnum::Method(from_function_method(m, false, tcx)),
196         ImplItem(i) => ItemEnum::Impl(i.into_tcx(tcx)),
197         StaticItem(s) => ItemEnum::Static(s.into_tcx(tcx)),
198         ForeignStaticItem(s) => ItemEnum::Static(s.into_tcx(tcx)),
199         ForeignTypeItem => ItemEnum::ForeignType,
200         TypedefItem(t, _) => ItemEnum::Typedef(t.into_tcx(tcx)),
201         OpaqueTyItem(t) => ItemEnum::OpaqueTy(t.into_tcx(tcx)),
202         ConstantItem(c) => ItemEnum::Constant(c.into_tcx(tcx)),
203         MacroItem(m) => ItemEnum::Macro(m.source),
204         ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
205         AssocConstItem(t, s) => ItemEnum::AssocConst { type_: t.into_tcx(tcx), default: s },
206         AssocTypeItem(g, t) => ItemEnum::AssocType {
207             bounds: g.into_iter().map(|x| x.into_tcx(tcx)).collect(),
208             default: t.map(|x| x.into_tcx(tcx)),
209         },
210         // `convert_item` early returns `None` for striped items
211         StrippedItem(_) => unreachable!(),
212         PrimitiveItem(_) | KeywordItem(_) => {
213             panic!("{:?} is not supported for JSON output", item)
214         }
215         ExternCrateItem { ref src } => ItemEnum::ExternCrate {
216             name: name.as_ref().unwrap().to_string(),
217             rename: src.map(|x| x.to_string()),
218         },
219     }
220 }
221
222 impl FromWithTcx<clean::Struct> for Struct {
223     fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
224         let clean::Struct { struct_type, generics, fields, fields_stripped } = struct_;
225         Struct {
226             struct_type: from_ctor_kind(struct_type),
227             generics: generics.into_tcx(tcx),
228             fields_stripped,
229             fields: ids(fields),
230             impls: Vec::new(), // Added in JsonRenderer::item
231         }
232     }
233 }
234
235 impl FromWithTcx<clean::Union> for Union {
236     fn from_tcx(struct_: clean::Union, tcx: TyCtxt<'_>) -> Self {
237         let clean::Union { generics, fields, fields_stripped } = struct_;
238         Union {
239             generics: generics.into_tcx(tcx),
240             fields_stripped,
241             fields: ids(fields),
242             impls: Vec::new(), // Added in JsonRenderer::item
243         }
244     }
245 }
246
247 crate fn from_ctor_kind(struct_type: CtorKind) -> StructType {
248     match struct_type {
249         CtorKind::Fictive => StructType::Plain,
250         CtorKind::Fn => StructType::Tuple,
251         CtorKind::Const => StructType::Unit,
252     }
253 }
254
255 crate fn from_fn_header(header: &rustc_hir::FnHeader) -> HashSet<Qualifiers> {
256     let mut v = HashSet::new();
257
258     if let rustc_hir::Unsafety::Unsafe = header.unsafety {
259         v.insert(Qualifiers::Unsafe);
260     }
261
262     if let rustc_hir::IsAsync::Async = header.asyncness {
263         v.insert(Qualifiers::Async);
264     }
265
266     if let rustc_hir::Constness::Const = header.constness {
267         v.insert(Qualifiers::Const);
268     }
269
270     v
271 }
272
273 impl FromWithTcx<clean::Function> for Function {
274     fn from_tcx(function: clean::Function, tcx: TyCtxt<'_>) -> Self {
275         let clean::Function { decl, generics, header } = function;
276         Function {
277             decl: decl.into_tcx(tcx),
278             generics: generics.into_tcx(tcx),
279             header: from_fn_header(&header),
280             abi: header.abi.to_string(),
281         }
282     }
283 }
284
285 impl FromWithTcx<clean::Generics> for Generics {
286     fn from_tcx(generics: clean::Generics, tcx: TyCtxt<'_>) -> Self {
287         Generics {
288             params: generics.params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
289             where_predicates: generics
290                 .where_predicates
291                 .into_iter()
292                 .map(|x| x.into_tcx(tcx))
293                 .collect(),
294         }
295     }
296 }
297
298 impl FromWithTcx<clean::GenericParamDef> for GenericParamDef {
299     fn from_tcx(generic_param: clean::GenericParamDef, tcx: TyCtxt<'_>) -> Self {
300         GenericParamDef {
301             name: generic_param.name.to_string(),
302             kind: generic_param.kind.into_tcx(tcx),
303         }
304     }
305 }
306
307 impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
308     fn from_tcx(kind: clean::GenericParamDefKind, tcx: TyCtxt<'_>) -> Self {
309         use clean::GenericParamDefKind::*;
310         match kind {
311             Lifetime => GenericParamDefKind::Lifetime,
312             Type { did: _, bounds, default, synthetic: _ } => GenericParamDefKind::Type {
313                 bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
314                 default: default.map(|x| x.into_tcx(tcx)),
315             },
316             Const { did: _, ty } => GenericParamDefKind::Const(ty.into_tcx(tcx)),
317         }
318     }
319 }
320
321 impl FromWithTcx<clean::WherePredicate> for WherePredicate {
322     fn from_tcx(predicate: clean::WherePredicate, tcx: TyCtxt<'_>) -> Self {
323         use clean::WherePredicate::*;
324         match predicate {
325             BoundPredicate { ty, bounds } => WherePredicate::BoundPredicate {
326                 ty: ty.into_tcx(tcx),
327                 bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
328             },
329             RegionPredicate { lifetime, bounds } => WherePredicate::RegionPredicate {
330                 lifetime: lifetime.0.to_string(),
331                 bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
332             },
333             EqPredicate { lhs, rhs } => {
334                 WherePredicate::EqPredicate { lhs: lhs.into_tcx(tcx), rhs: rhs.into_tcx(tcx) }
335             }
336         }
337     }
338 }
339
340 impl FromWithTcx<clean::GenericBound> for GenericBound {
341     fn from_tcx(bound: clean::GenericBound, tcx: TyCtxt<'_>) -> Self {
342         use clean::GenericBound::*;
343         match bound {
344             TraitBound(clean::PolyTrait { trait_, generic_params }, modifier) => {
345                 GenericBound::TraitBound {
346                     trait_: trait_.into_tcx(tcx),
347                     generic_params: generic_params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
348                     modifier: from_trait_bound_modifier(modifier),
349                 }
350             }
351             Outlives(lifetime) => GenericBound::Outlives(lifetime.0.to_string()),
352         }
353     }
354 }
355
356 crate fn from_trait_bound_modifier(modifier: rustc_hir::TraitBoundModifier) -> TraitBoundModifier {
357     use rustc_hir::TraitBoundModifier::*;
358     match modifier {
359         None => TraitBoundModifier::None,
360         Maybe => TraitBoundModifier::Maybe,
361         MaybeConst => TraitBoundModifier::MaybeConst,
362     }
363 }
364
365 impl FromWithTcx<clean::Type> for Type {
366     fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
367         use clean::Type::*;
368         match ty {
369             ResolvedPath { path, param_names, did, is_generic: _ } => Type::ResolvedPath {
370                 name: path.whole_name(),
371                 id: from_def_id(did),
372                 args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
373                 param_names: param_names
374                     .map(|v| v.into_iter().map(|x| x.into_tcx(tcx)).collect())
375                     .unwrap_or_default(),
376             },
377             Generic(s) => Type::Generic(s.to_string()),
378             Primitive(p) => Type::Primitive(p.as_str().to_string()),
379             BareFunction(f) => Type::FunctionPointer(Box::new((*f).into_tcx(tcx))),
380             Tuple(t) => Type::Tuple(t.into_iter().map(|x| x.into_tcx(tcx)).collect()),
381             Slice(t) => Type::Slice(Box::new((*t).into_tcx(tcx))),
382             Array(t, s) => Type::Array { type_: Box::new((*t).into_tcx(tcx)), len: s },
383             ImplTrait(g) => Type::ImplTrait(g.into_iter().map(|x| x.into_tcx(tcx)).collect()),
384             Never => Type::Never,
385             Infer => Type::Infer,
386             RawPointer(mutability, type_) => Type::RawPointer {
387                 mutable: mutability == ast::Mutability::Mut,
388                 type_: Box::new((*type_).into_tcx(tcx)),
389             },
390             BorrowedRef { lifetime, mutability, type_ } => Type::BorrowedRef {
391                 lifetime: lifetime.map(|l| l.0.to_string()),
392                 mutable: mutability == ast::Mutability::Mut,
393                 type_: Box::new((*type_).into_tcx(tcx)),
394             },
395             QPath { name, self_type, trait_ } => Type::QualifiedPath {
396                 name: name.to_string(),
397                 self_type: Box::new((*self_type).into_tcx(tcx)),
398                 trait_: Box::new((*trait_).into_tcx(tcx)),
399             },
400         }
401     }
402 }
403
404 impl FromWithTcx<clean::BareFunctionDecl> for FunctionPointer {
405     fn from_tcx(bare_decl: clean::BareFunctionDecl, tcx: TyCtxt<'_>) -> Self {
406         let clean::BareFunctionDecl { unsafety, generic_params, decl, abi } = bare_decl;
407         FunctionPointer {
408             header: if let rustc_hir::Unsafety::Unsafe = unsafety {
409                 let mut hs = HashSet::new();
410                 hs.insert(Qualifiers::Unsafe);
411                 hs
412             } else {
413                 HashSet::new()
414             },
415             generic_params: generic_params.into_iter().map(|x| x.into_tcx(tcx)).collect(),
416             decl: decl.into_tcx(tcx),
417             abi: abi.to_string(),
418         }
419     }
420 }
421
422 impl FromWithTcx<clean::FnDecl> for FnDecl {
423     fn from_tcx(decl: clean::FnDecl, tcx: TyCtxt<'_>) -> Self {
424         let clean::FnDecl { inputs, output, c_variadic } = decl;
425         FnDecl {
426             inputs: inputs
427                 .values
428                 .into_iter()
429                 .map(|arg| (arg.name.to_string(), arg.type_.into_tcx(tcx)))
430                 .collect(),
431             output: match output {
432                 clean::FnRetTy::Return(t) => Some(t.into_tcx(tcx)),
433                 clean::FnRetTy::DefaultReturn => None,
434             },
435             c_variadic,
436         }
437     }
438 }
439
440 impl FromWithTcx<clean::Trait> for Trait {
441     fn from_tcx(trait_: clean::Trait, tcx: TyCtxt<'_>) -> Self {
442         let clean::Trait { unsafety, items, generics, bounds, is_auto } = trait_;
443         Trait {
444             is_auto,
445             is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
446             items: ids(items),
447             generics: generics.into_tcx(tcx),
448             bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
449             implementors: Vec::new(), // Added in JsonRenderer::item
450         }
451     }
452 }
453
454 impl FromWithTcx<clean::Impl> for Impl {
455     fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self {
456         let clean::Impl {
457             unsafety,
458             generics,
459             provided_trait_methods,
460             trait_,
461             for_,
462             items,
463             negative_polarity,
464             synthetic,
465             blanket_impl,
466             span: _span,
467         } = impl_;
468         Impl {
469             is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
470             generics: generics.into_tcx(tcx),
471             provided_trait_methods: provided_trait_methods
472                 .into_iter()
473                 .map(|x| x.to_string())
474                 .collect(),
475             trait_: trait_.map(|x| x.into_tcx(tcx)),
476             for_: for_.into_tcx(tcx),
477             items: ids(items),
478             negative: negative_polarity,
479             synthetic,
480             blanket_impl: blanket_impl.map(|x| x.into_tcx(tcx)),
481         }
482     }
483 }
484
485 crate fn from_function_method(
486     function: clean::Function,
487     has_body: bool,
488     tcx: TyCtxt<'_>,
489 ) -> Method {
490     let clean::Function { header, decl, generics } = function;
491     Method {
492         decl: decl.into_tcx(tcx),
493         generics: generics.into_tcx(tcx),
494         header: from_fn_header(&header),
495         abi: header.abi.to_string(),
496         has_body,
497     }
498 }
499
500 impl FromWithTcx<clean::Enum> for Enum {
501     fn from_tcx(enum_: clean::Enum, tcx: TyCtxt<'_>) -> Self {
502         let clean::Enum { variants, generics, variants_stripped } = enum_;
503         Enum {
504             generics: generics.into_tcx(tcx),
505             variants_stripped,
506             variants: ids(variants),
507             impls: Vec::new(), // Added in JsonRenderer::item
508         }
509     }
510 }
511
512 impl FromWithTcx<clean::VariantStruct> for Struct {
513     fn from_tcx(struct_: clean::VariantStruct, _tcx: TyCtxt<'_>) -> Self {
514         let clean::VariantStruct { struct_type, fields, fields_stripped } = struct_;
515         Struct {
516             struct_type: from_ctor_kind(struct_type),
517             generics: Default::default(),
518             fields_stripped,
519             fields: ids(fields),
520             impls: Vec::new(),
521         }
522     }
523 }
524
525 impl FromWithTcx<clean::Variant> for Variant {
526     fn from_tcx(variant: clean::Variant, tcx: TyCtxt<'_>) -> Self {
527         use clean::Variant::*;
528         match variant {
529             CLike => Variant::Plain,
530             Tuple(t) => Variant::Tuple(t.into_iter().map(|x| x.into_tcx(tcx)).collect()),
531             Struct(s) => Variant::Struct(ids(s.fields)),
532         }
533     }
534 }
535
536 impl FromWithTcx<clean::Import> for Import {
537     fn from_tcx(import: clean::Import, _tcx: TyCtxt<'_>) -> Self {
538         use clean::ImportKind::*;
539         match import.kind {
540             Simple(s) => Import {
541                 source: import.source.path.whole_name(),
542                 name: s.to_string(),
543                 id: import.source.did.map(from_def_id),
544                 glob: false,
545             },
546             Glob => Import {
547                 source: import.source.path.whole_name(),
548                 name: import.source.path.last_name().to_string(),
549                 id: import.source.did.map(from_def_id),
550                 glob: true,
551             },
552         }
553     }
554 }
555
556 impl FromWithTcx<clean::ProcMacro> for ProcMacro {
557     fn from_tcx(mac: clean::ProcMacro, _tcx: TyCtxt<'_>) -> Self {
558         ProcMacro {
559             kind: from_macro_kind(mac.kind),
560             helpers: mac.helpers.iter().map(|x| x.to_string()).collect(),
561         }
562     }
563 }
564
565 crate fn from_macro_kind(kind: rustc_span::hygiene::MacroKind) -> MacroKind {
566     use rustc_span::hygiene::MacroKind::*;
567     match kind {
568         Bang => MacroKind::Bang,
569         Attr => MacroKind::Attr,
570         Derive => MacroKind::Derive,
571     }
572 }
573
574 impl FromWithTcx<clean::Typedef> for Typedef {
575     fn from_tcx(typedef: clean::Typedef, tcx: TyCtxt<'_>) -> Self {
576         let clean::Typedef { type_, generics, item_type: _ } = typedef;
577         Typedef { type_: type_.into_tcx(tcx), generics: generics.into_tcx(tcx) }
578     }
579 }
580
581 impl FromWithTcx<clean::OpaqueTy> for OpaqueTy {
582     fn from_tcx(opaque: clean::OpaqueTy, tcx: TyCtxt<'_>) -> Self {
583         OpaqueTy {
584             bounds: opaque.bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
585             generics: opaque.generics.into_tcx(tcx),
586         }
587     }
588 }
589
590 impl FromWithTcx<clean::Static> for Static {
591     fn from_tcx(stat: clean::Static, tcx: TyCtxt<'_>) -> Self {
592         Static {
593             type_: stat.type_.into_tcx(tcx),
594             mutable: stat.mutability == ast::Mutability::Mut,
595             expr: stat.expr.map(|e| print_const_expr(tcx, e)).unwrap_or_default(),
596         }
597     }
598 }
599
600 impl FromWithTcx<clean::TraitAlias> for TraitAlias {
601     fn from_tcx(alias: clean::TraitAlias, tcx: TyCtxt<'_>) -> Self {
602         TraitAlias {
603             generics: alias.generics.into_tcx(tcx),
604             params: alias.bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
605         }
606     }
607 }
608
609 impl FromWithTcx<ItemType> for ItemKind {
610     fn from_tcx(kind: ItemType, _tcx: TyCtxt<'_>) -> Self {
611         use ItemType::*;
612         match kind {
613             Module => ItemKind::Module,
614             ExternCrate => ItemKind::ExternCrate,
615             Import => ItemKind::Import,
616             Struct => ItemKind::Struct,
617             Union => ItemKind::Union,
618             Enum => ItemKind::Enum,
619             Function => ItemKind::Function,
620             Typedef => ItemKind::Typedef,
621             OpaqueTy => ItemKind::OpaqueTy,
622             Static => ItemKind::Static,
623             Constant => ItemKind::Constant,
624             Trait => ItemKind::Trait,
625             Impl => ItemKind::Impl,
626             TyMethod | Method => ItemKind::Method,
627             StructField => ItemKind::StructField,
628             Variant => ItemKind::Variant,
629             Macro => ItemKind::Macro,
630             Primitive => ItemKind::Primitive,
631             AssocConst => ItemKind::AssocConst,
632             AssocType => ItemKind::AssocType,
633             ForeignType => ItemKind::ForeignType,
634             Keyword => ItemKind::Keyword,
635             TraitAlias => ItemKind::TraitAlias,
636             ProcAttribute => ItemKind::ProcAttribute,
637             ProcDerive => ItemKind::ProcDerive,
638         }
639     }
640 }
641
642 fn ids(items: impl IntoIterator<Item = clean::Item>) -> Vec<Id> {
643     items.into_iter().filter(|x| !x.is_stripped()).map(|i| from_def_id(i.def_id)).collect()
644 }