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.
5 #![allow(rustc::default_hash_types)]
7 use std::convert::From;
11 use rustc_hir::{def::CtorKind, def_id::DefId};
12 use rustc_middle::ty::{self, TyCtxt};
13 use rustc_span::{Pos, Symbol};
14 use rustc_target::spec::abi::Abi as RustcAbi;
16 use rustdoc_json_types::*;
18 use crate::clean::utils::print_const_expr;
19 use crate::clean::{self, ItemId};
20 use crate::formats::item_type::ItemType;
21 use crate::json::JsonRenderer;
23 impl JsonRenderer<'_> {
24 pub(super) fn convert_item(&self, item: clean::Item) -> Option<Item> {
25 let deprecation = item.deprecation(self.tcx);
32 .map(|clean::ItemLink { link, did, .. }| {
33 (link.clone(), from_item_id((*did).into(), self.tcx))
36 let docs = item.attrs.collapsed_doc_value();
41 .map(rustc_ast_pretty::pprust::attribute_to_string)
43 let span = item.span(self.tcx);
44 let clean::Item { name, attrs: _, kind: _, visibility, item_id, cfg: _ } = item;
45 let inner = match *item.kind {
46 clean::KeywordItem => return None,
47 clean::StrippedItem(ref inner) => {
49 // We document stripped modules as with `Module::is_stripped` set to
50 // `true`, to prevent contained items from being orphaned for downstream users,
51 // as JSON does no inlining.
53 if self.imported_items.contains(&item_id.expect_def_id()) =>
55 from_clean_item(item, self.tcx)
60 _ => from_clean_item(item, self.tcx),
63 id: from_item_id_with_name(item_id, self.tcx, name),
64 crate_id: item_id.krate().as_u32(),
65 name: name.map(|sym| sym.to_string()),
66 span: span.and_then(|span| self.convert_span(span)),
67 visibility: self.convert_visibility(visibility),
70 deprecation: deprecation.map(from_deprecation),
76 fn convert_span(&self, span: clean::Span) -> Option<Span> {
77 match span.filename(self.sess()) {
78 rustc_span::FileName::Real(name) => {
79 if let Some(local_path) = name.into_local_path() {
80 let hi = span.hi(self.sess());
81 let lo = span.lo(self.sess());
84 begin: (lo.line, lo.col.to_usize()),
85 end: (hi.line, hi.col.to_usize()),
95 fn convert_visibility(&self, v: clean::Visibility) -> Visibility {
96 use clean::Visibility::*;
98 Public => Visibility::Public,
99 Inherited => Visibility::Default,
100 Restricted(did) if did.is_crate_root() => Visibility::Crate,
101 Restricted(did) => Visibility::Restricted {
102 parent: from_item_id(did.into(), self.tcx),
103 path: self.tcx.def_path(did).to_string_no_crate_verbose(),
109 pub(crate) trait FromWithTcx<T> {
110 fn from_tcx(f: T, tcx: TyCtxt<'_>) -> Self;
113 pub(crate) trait IntoWithTcx<T> {
114 fn into_tcx(self, tcx: TyCtxt<'_>) -> T;
117 impl<T, U> IntoWithTcx<U> for T
121 fn into_tcx(self, tcx: TyCtxt<'_>) -> U {
122 U::from_tcx(self, tcx)
126 impl<I, T, U> FromWithTcx<I> for Vec<U>
128 I: IntoIterator<Item = T>,
131 fn from_tcx(f: I, tcx: TyCtxt<'_>) -> Vec<U> {
132 f.into_iter().map(|x| x.into_tcx(tcx)).collect()
136 pub(crate) fn from_deprecation(deprecation: rustc_attr::Deprecation) -> Deprecation {
138 let rustc_attr::Deprecation { since, note, is_since_rustc_version: _, suggestion: _ } = deprecation;
139 Deprecation { since: since.map(|s| s.to_string()), note: note.map(|s| s.to_string()) }
142 impl FromWithTcx<clean::GenericArgs> for GenericArgs {
143 fn from_tcx(args: clean::GenericArgs, tcx: TyCtxt<'_>) -> Self {
144 use clean::GenericArgs::*;
146 AngleBracketed { args, bindings } => GenericArgs::AngleBracketed {
147 args: args.into_vec().into_tcx(tcx),
148 bindings: bindings.into_tcx(tcx),
150 Parenthesized { inputs, output } => GenericArgs::Parenthesized {
151 inputs: inputs.into_vec().into_tcx(tcx),
152 output: output.map(|a| (*a).into_tcx(tcx)),
158 impl FromWithTcx<clean::GenericArg> for GenericArg {
159 fn from_tcx(arg: clean::GenericArg, tcx: TyCtxt<'_>) -> Self {
160 use clean::GenericArg::*;
162 Lifetime(l) => GenericArg::Lifetime(convert_lifetime(l)),
163 Type(t) => GenericArg::Type(t.into_tcx(tcx)),
164 Const(box c) => GenericArg::Const(c.into_tcx(tcx)),
165 Infer => GenericArg::Infer,
170 impl FromWithTcx<clean::Constant> for Constant {
171 fn from_tcx(constant: clean::Constant, tcx: TyCtxt<'_>) -> Self {
172 let expr = constant.expr(tcx);
173 let value = constant.value(tcx);
174 let is_literal = constant.is_literal(tcx);
175 Constant { type_: constant.type_.into_tcx(tcx), expr, value, is_literal }
179 impl FromWithTcx<clean::TypeBinding> for TypeBinding {
180 fn from_tcx(binding: clean::TypeBinding, tcx: TyCtxt<'_>) -> Self {
182 name: binding.assoc.name.to_string(),
183 args: binding.assoc.args.into_tcx(tcx),
184 binding: binding.kind.into_tcx(tcx),
189 impl FromWithTcx<clean::TypeBindingKind> for TypeBindingKind {
190 fn from_tcx(kind: clean::TypeBindingKind, tcx: TyCtxt<'_>) -> Self {
191 use clean::TypeBindingKind::*;
193 Equality { term } => TypeBindingKind::Equality(term.into_tcx(tcx)),
194 Constraint { bounds } => TypeBindingKind::Constraint(bounds.into_tcx(tcx)),
199 /// It generates an ID as follows:
201 /// `CRATE_ID:ITEM_ID[:NAME_ID]` (if there is no name, NAME_ID is not generated).
202 pub(crate) fn from_item_id(item_id: ItemId, tcx: TyCtxt<'_>) -> Id {
203 from_item_id_with_name(item_id, tcx, None)
206 // FIXME: this function (and appending the name at the end of the ID) should be removed when
207 // reexports are not inlined anymore for json format. It should be done in #93518.
208 pub(crate) fn from_item_id_with_name(item_id: ItemId, tcx: TyCtxt<'_>, name: Option<Symbol>) -> Id {
209 struct DisplayDefId<'a>(DefId, TyCtxt<'a>, Option<Symbol>);
211 impl<'a> fmt::Display for DisplayDefId<'a> {
212 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213 let name = match self.2 {
214 Some(name) => format!(":{}", name.as_u32()),
217 .opt_item_name(self.0)
218 .map(|n| format!(":{}", n.as_u32()))
219 .unwrap_or_default(),
221 write!(f, "{}:{}{}", self.0.krate.as_u32(), u32::from(self.0.index), name)
226 ItemId::DefId(did) => Id(format!("{}", DisplayDefId(did, tcx, name))),
227 ItemId::Blanket { for_, impl_id } => {
228 Id(format!("b:{}-{}", DisplayDefId(impl_id, tcx, None), DisplayDefId(for_, tcx, name)))
230 ItemId::Auto { for_, trait_ } => {
231 Id(format!("a:{}-{}", DisplayDefId(trait_, tcx, None), DisplayDefId(for_, tcx, name)))
233 ItemId::Primitive(ty, krate) => Id(format!("p:{}:{}", krate.as_u32(), ty.as_sym())),
237 fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
238 use clean::ItemKind::*;
239 let name = item.name;
240 let is_crate = item.is_crate();
241 let header = item.fn_header(tcx);
245 ItemEnum::Module(Module { is_crate, items: ids(m.items, tcx), is_stripped: false })
247 ImportItem(i) => ItemEnum::Import(i.into_tcx(tcx)),
248 StructItem(s) => ItemEnum::Struct(s.into_tcx(tcx)),
249 UnionItem(u) => ItemEnum::Union(u.into_tcx(tcx)),
250 StructFieldItem(f) => ItemEnum::StructField(f.into_tcx(tcx)),
251 EnumItem(e) => ItemEnum::Enum(e.into_tcx(tcx)),
252 VariantItem(v) => ItemEnum::Variant(v.into_tcx(tcx)),
253 FunctionItem(f) => ItemEnum::Function(from_function(f, header.unwrap(), tcx)),
254 ForeignFunctionItem(f) => ItemEnum::Function(from_function(f, header.unwrap(), tcx)),
255 TraitItem(t) => ItemEnum::Trait((*t).into_tcx(tcx)),
256 TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_tcx(tcx)),
257 MethodItem(m, _) => ItemEnum::Method(from_function_method(m, true, header.unwrap(), tcx)),
258 TyMethodItem(m) => ItemEnum::Method(from_function_method(m, false, header.unwrap(), tcx)),
259 ImplItem(i) => ItemEnum::Impl((*i).into_tcx(tcx)),
260 StaticItem(s) => ItemEnum::Static(s.into_tcx(tcx)),
261 ForeignStaticItem(s) => ItemEnum::Static(s.into_tcx(tcx)),
262 ForeignTypeItem => ItemEnum::ForeignType,
263 TypedefItem(t) => ItemEnum::Typedef(t.into_tcx(tcx)),
264 OpaqueTyItem(t) => ItemEnum::OpaqueTy(t.into_tcx(tcx)),
265 ConstantItem(c) => ItemEnum::Constant(c.into_tcx(tcx)),
266 MacroItem(m) => ItemEnum::Macro(m.source),
267 ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
268 PrimitiveItem(p) => ItemEnum::PrimitiveType(p.as_sym().to_string()),
269 TyAssocConstItem(ty) => ItemEnum::AssocConst { type_: ty.into_tcx(tcx), default: None },
270 AssocConstItem(ty, default) => {
271 ItemEnum::AssocConst { type_: ty.into_tcx(tcx), default: Some(default.expr(tcx)) }
273 TyAssocTypeItem(g, b) => ItemEnum::AssocType {
274 generics: (*g).into_tcx(tcx),
275 bounds: b.into_tcx(tcx),
278 AssocTypeItem(t, b) => ItemEnum::AssocType {
279 generics: t.generics.into_tcx(tcx),
280 bounds: b.into_tcx(tcx),
281 default: Some(t.item_type.unwrap_or(t.type_).into_tcx(tcx)),
283 // `convert_item` early returns `None` for stripped items and keywords.
284 KeywordItem => unreachable!(),
285 StrippedItem(inner) => {
287 ModuleItem(m) => ItemEnum::Module(Module {
289 items: ids(m.items, tcx),
292 // `convert_item` early returns `None` for stripped items we're not including
296 ExternCrateItem { ref src } => ItemEnum::ExternCrate {
297 name: name.as_ref().unwrap().to_string(),
298 rename: src.map(|x| x.to_string()),
303 impl FromWithTcx<clean::Struct> for Struct {
304 fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
305 let fields_stripped = struct_.has_stripped_entries();
306 let clean::Struct { struct_type, generics, fields } = struct_;
308 struct_type: from_ctor_kind(struct_type),
309 generics: generics.into_tcx(tcx),
311 fields: ids(fields, tcx),
312 impls: Vec::new(), // Added in JsonRenderer::item
317 impl FromWithTcx<clean::Union> for Union {
318 fn from_tcx(union_: clean::Union, tcx: TyCtxt<'_>) -> Self {
319 let fields_stripped = union_.has_stripped_entries();
320 let clean::Union { generics, fields } = union_;
322 generics: generics.into_tcx(tcx),
324 fields: ids(fields, tcx),
325 impls: Vec::new(), // Added in JsonRenderer::item
330 pub(crate) fn from_ctor_kind(struct_type: CtorKind) -> StructType {
332 CtorKind::Fictive => StructType::Plain,
333 CtorKind::Fn => StructType::Tuple,
334 CtorKind::Const => StructType::Unit,
338 pub(crate) fn from_fn_header(header: &rustc_hir::FnHeader) -> Header {
340 async_: header.is_async(),
341 const_: header.is_const(),
342 unsafe_: header.is_unsafe(),
343 abi: convert_abi(header.abi),
347 fn convert_abi(a: RustcAbi) -> Abi {
349 RustcAbi::Rust => Abi::Rust,
350 RustcAbi::C { unwind } => Abi::C { unwind },
351 RustcAbi::Cdecl { unwind } => Abi::Cdecl { unwind },
352 RustcAbi::Stdcall { unwind } => Abi::Stdcall { unwind },
353 RustcAbi::Fastcall { unwind } => Abi::Fastcall { unwind },
354 RustcAbi::Aapcs { unwind } => Abi::Aapcs { unwind },
355 RustcAbi::Win64 { unwind } => Abi::Win64 { unwind },
356 RustcAbi::SysV64 { unwind } => Abi::SysV64 { unwind },
357 RustcAbi::System { unwind } => Abi::System { unwind },
358 _ => Abi::Other(a.to_string()),
362 fn convert_lifetime(l: clean::Lifetime) -> String {
366 impl FromWithTcx<clean::Generics> for Generics {
367 fn from_tcx(generics: clean::Generics, tcx: TyCtxt<'_>) -> Self {
369 params: generics.params.into_tcx(tcx),
370 where_predicates: generics.where_predicates.into_tcx(tcx),
375 impl FromWithTcx<clean::GenericParamDef> for GenericParamDef {
376 fn from_tcx(generic_param: clean::GenericParamDef, tcx: TyCtxt<'_>) -> Self {
378 name: generic_param.name.to_string(),
379 kind: generic_param.kind.into_tcx(tcx),
384 impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
385 fn from_tcx(kind: clean::GenericParamDefKind, tcx: TyCtxt<'_>) -> Self {
386 use clean::GenericParamDefKind::*;
388 Lifetime { outlives } => GenericParamDefKind::Lifetime {
389 outlives: outlives.into_iter().map(convert_lifetime).collect(),
391 Type { did: _, bounds, default, synthetic } => GenericParamDefKind::Type {
392 bounds: bounds.into_tcx(tcx),
393 default: default.map(|x| (*x).into_tcx(tcx)),
396 Const { did: _, ty, default } => GenericParamDefKind::Const {
397 type_: (*ty).into_tcx(tcx),
398 default: default.map(|x| *x),
404 impl FromWithTcx<clean::WherePredicate> for WherePredicate {
405 fn from_tcx(predicate: clean::WherePredicate, tcx: TyCtxt<'_>) -> Self {
406 use clean::WherePredicate::*;
408 BoundPredicate { ty, bounds, bound_params } => WherePredicate::BoundPredicate {
409 type_: ty.into_tcx(tcx),
410 bounds: bounds.into_tcx(tcx),
411 generic_params: bound_params
413 .map(|x| GenericParamDef {
414 name: x.0.to_string(),
415 kind: GenericParamDefKind::Lifetime { outlives: vec![] },
419 RegionPredicate { lifetime, bounds } => WherePredicate::RegionPredicate {
420 lifetime: convert_lifetime(lifetime),
421 bounds: bounds.into_tcx(tcx),
423 EqPredicate { lhs, rhs } => {
424 WherePredicate::EqPredicate { lhs: lhs.into_tcx(tcx), rhs: rhs.into_tcx(tcx) }
430 impl FromWithTcx<clean::GenericBound> for GenericBound {
431 fn from_tcx(bound: clean::GenericBound, tcx: TyCtxt<'_>) -> Self {
432 use clean::GenericBound::*;
434 TraitBound(clean::PolyTrait { trait_, generic_params }, modifier) => {
435 GenericBound::TraitBound {
436 trait_: trait_.into_tcx(tcx),
437 generic_params: generic_params.into_tcx(tcx),
438 modifier: from_trait_bound_modifier(modifier),
441 Outlives(lifetime) => GenericBound::Outlives(convert_lifetime(lifetime)),
446 pub(crate) fn from_trait_bound_modifier(
447 modifier: rustc_hir::TraitBoundModifier,
448 ) -> TraitBoundModifier {
449 use rustc_hir::TraitBoundModifier::*;
451 None => TraitBoundModifier::None,
452 Maybe => TraitBoundModifier::Maybe,
453 MaybeConst => TraitBoundModifier::MaybeConst,
457 impl FromWithTcx<clean::Type> for Type {
458 fn from_tcx(ty: clean::Type, tcx: TyCtxt<'_>) -> Self {
460 Array, BareFunction, BorrowedRef, Generic, ImplTrait, Infer, Primitive, QPath,
461 RawPointer, Slice, Tuple,
465 clean::Type::Path { path } => Type::ResolvedPath(path.into_tcx(tcx)),
466 clean::Type::DynTrait(bounds, lt) => Type::DynTrait(DynTrait {
467 lifetime: lt.map(convert_lifetime),
468 traits: bounds.into_tcx(tcx),
470 Generic(s) => Type::Generic(s.to_string()),
471 Primitive(p) => Type::Primitive(p.as_sym().to_string()),
472 BareFunction(f) => Type::FunctionPointer(Box::new((*f).into_tcx(tcx))),
473 Tuple(t) => Type::Tuple(t.into_tcx(tcx)),
474 Slice(t) => Type::Slice(Box::new((*t).into_tcx(tcx))),
475 Array(t, s) => Type::Array { type_: Box::new((*t).into_tcx(tcx)), len: s },
476 ImplTrait(g) => Type::ImplTrait(g.into_tcx(tcx)),
477 Infer => Type::Infer,
478 RawPointer(mutability, type_) => Type::RawPointer {
479 mutable: mutability == ast::Mutability::Mut,
480 type_: Box::new((*type_).into_tcx(tcx)),
482 BorrowedRef { lifetime, mutability, type_ } => Type::BorrowedRef {
483 lifetime: lifetime.map(convert_lifetime),
484 mutable: mutability == ast::Mutability::Mut,
485 type_: Box::new((*type_).into_tcx(tcx)),
487 QPath(box clean::QPathData { assoc, self_type, trait_, .. }) => Type::QualifiedPath {
488 name: assoc.name.to_string(),
489 args: Box::new(assoc.args.into_tcx(tcx)),
490 self_type: Box::new(self_type.into_tcx(tcx)),
491 trait_: trait_.into_tcx(tcx),
497 impl FromWithTcx<clean::Path> for Path {
498 fn from_tcx(path: clean::Path, tcx: TyCtxt<'_>) -> Path {
500 name: path.whole_name(),
501 id: from_item_id(path.def_id().into(), tcx),
502 args: path.segments.last().map(|args| Box::new(args.clone().args.into_tcx(tcx))),
507 impl FromWithTcx<clean::Term> for Term {
508 fn from_tcx(term: clean::Term, tcx: TyCtxt<'_>) -> Term {
510 clean::Term::Type(ty) => Term::Type(FromWithTcx::from_tcx(ty, tcx)),
511 clean::Term::Constant(c) => Term::Constant(FromWithTcx::from_tcx(c, tcx)),
516 impl FromWithTcx<clean::BareFunctionDecl> for FunctionPointer {
517 fn from_tcx(bare_decl: clean::BareFunctionDecl, tcx: TyCtxt<'_>) -> Self {
518 let clean::BareFunctionDecl { unsafety, generic_params, decl, abi } = bare_decl;
521 unsafe_: matches!(unsafety, rustc_hir::Unsafety::Unsafe),
524 abi: convert_abi(abi),
526 generic_params: generic_params.into_tcx(tcx),
527 decl: decl.into_tcx(tcx),
532 impl FromWithTcx<clean::FnDecl> for FnDecl {
533 fn from_tcx(decl: clean::FnDecl, tcx: TyCtxt<'_>) -> Self {
534 let clean::FnDecl { inputs, output, c_variadic } = decl;
539 .map(|arg| (arg.name.to_string(), arg.type_.into_tcx(tcx)))
541 output: match output {
542 clean::FnRetTy::Return(t) => Some(t.into_tcx(tcx)),
543 clean::FnRetTy::DefaultReturn => None,
550 impl FromWithTcx<clean::Trait> for Trait {
551 fn from_tcx(trait_: clean::Trait, tcx: TyCtxt<'_>) -> Self {
552 let is_auto = trait_.is_auto(tcx);
553 let is_unsafe = trait_.unsafety(tcx) == rustc_hir::Unsafety::Unsafe;
554 let clean::Trait { items, generics, bounds, .. } = trait_;
558 items: ids(items, tcx),
559 generics: generics.into_tcx(tcx),
560 bounds: bounds.into_tcx(tcx),
561 implementations: Vec::new(), // Added in JsonRenderer::item
566 impl FromWithTcx<clean::PolyTrait> for PolyTrait {
568 clean::PolyTrait { trait_, generic_params }: clean::PolyTrait,
571 PolyTrait { trait_: trait_.into_tcx(tcx), generic_params: generic_params.into_tcx(tcx) }
575 impl FromWithTcx<clean::Impl> for Impl {
576 fn from_tcx(impl_: clean::Impl, tcx: TyCtxt<'_>) -> Self {
577 let provided_trait_methods = impl_.provided_trait_methods(tcx);
578 let clean::Impl { unsafety, generics, trait_, for_, items, polarity, kind } = impl_;
579 // FIXME: use something like ImplKind in JSON?
580 let (synthetic, blanket_impl) = match kind {
581 clean::ImplKind::Normal | clean::ImplKind::FakeVaradic => (false, None),
582 clean::ImplKind::Auto => (true, None),
583 clean::ImplKind::Blanket(ty) => (false, Some(*ty)),
585 let negative_polarity = match polarity {
586 ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => false,
587 ty::ImplPolarity::Negative => true,
590 is_unsafe: unsafety == rustc_hir::Unsafety::Unsafe,
591 generics: generics.into_tcx(tcx),
592 provided_trait_methods: provided_trait_methods
594 .map(|x| x.to_string())
596 trait_: trait_.map(|path| path.into_tcx(tcx)),
597 for_: for_.into_tcx(tcx),
598 items: ids(items, tcx),
599 negative: negative_polarity,
601 blanket_impl: blanket_impl.map(|x| x.into_tcx(tcx)),
606 pub(crate) fn from_function(
607 function: Box<clean::Function>,
608 header: rustc_hir::FnHeader,
611 let clean::Function { decl, generics } = *function;
613 decl: decl.into_tcx(tcx),
614 generics: generics.into_tcx(tcx),
615 header: from_fn_header(&header),
619 pub(crate) fn from_function_method(
620 function: Box<clean::Function>,
622 header: rustc_hir::FnHeader,
625 let clean::Function { decl, generics } = *function;
627 decl: decl.into_tcx(tcx),
628 generics: generics.into_tcx(tcx),
629 header: from_fn_header(&header),
634 impl FromWithTcx<clean::Enum> for Enum {
635 fn from_tcx(enum_: clean::Enum, tcx: TyCtxt<'_>) -> Self {
636 let variants_stripped = enum_.has_stripped_entries();
637 let clean::Enum { variants, generics } = enum_;
639 generics: generics.into_tcx(tcx),
641 variants: ids(variants, tcx),
642 impls: Vec::new(), // Added in JsonRenderer::item
647 impl FromWithTcx<clean::VariantStruct> for Struct {
648 fn from_tcx(struct_: clean::VariantStruct, tcx: TyCtxt<'_>) -> Self {
649 let fields_stripped = struct_.has_stripped_entries();
650 let clean::VariantStruct { struct_type, fields } = struct_;
652 struct_type: from_ctor_kind(struct_type),
653 generics: Generics { params: vec![], where_predicates: vec![] },
655 fields: ids(fields, tcx),
661 impl FromWithTcx<clean::Variant> for Variant {
662 fn from_tcx(variant: clean::Variant, tcx: TyCtxt<'_>) -> Self {
663 use clean::Variant::*;
665 CLike => Variant::Plain,
666 Tuple(fields) => Variant::Tuple(
669 .filter_map(|f| match *f.kind {
670 clean::StructFieldItem(ty) => Some(ty.into_tcx(tcx)),
671 clean::StrippedItem(_) => None,
676 Struct(s) => Variant::Struct(ids(s.fields, tcx)),
681 impl FromWithTcx<clean::Import> for Import {
682 fn from_tcx(import: clean::Import, tcx: TyCtxt<'_>) -> Self {
683 use clean::ImportKind::*;
684 let (name, glob) = match import.kind {
685 Simple(s) => (s.to_string(), false),
687 import.source.path.last_opt().unwrap_or_else(|| Symbol::intern("*")).to_string(),
692 source: import.source.path.whole_name(),
694 id: import.source.did.map(ItemId::from).map(|i| from_item_id(i, tcx)),
700 impl FromWithTcx<clean::ProcMacro> for ProcMacro {
701 fn from_tcx(mac: clean::ProcMacro, _tcx: TyCtxt<'_>) -> Self {
703 kind: from_macro_kind(mac.kind),
704 helpers: mac.helpers.iter().map(|x| x.to_string()).collect(),
709 pub(crate) fn from_macro_kind(kind: rustc_span::hygiene::MacroKind) -> MacroKind {
710 use rustc_span::hygiene::MacroKind::*;
712 Bang => MacroKind::Bang,
713 Attr => MacroKind::Attr,
714 Derive => MacroKind::Derive,
718 impl FromWithTcx<Box<clean::Typedef>> for Typedef {
719 fn from_tcx(typedef: Box<clean::Typedef>, tcx: TyCtxt<'_>) -> Self {
720 let clean::Typedef { type_, generics, item_type: _ } = *typedef;
721 Typedef { type_: type_.into_tcx(tcx), generics: generics.into_tcx(tcx) }
725 impl FromWithTcx<clean::OpaqueTy> for OpaqueTy {
726 fn from_tcx(opaque: clean::OpaqueTy, tcx: TyCtxt<'_>) -> Self {
727 OpaqueTy { bounds: opaque.bounds.into_tcx(tcx), generics: opaque.generics.into_tcx(tcx) }
731 impl FromWithTcx<clean::Static> for Static {
732 fn from_tcx(stat: clean::Static, tcx: TyCtxt<'_>) -> Self {
734 type_: stat.type_.into_tcx(tcx),
735 mutable: stat.mutability == ast::Mutability::Mut,
736 expr: stat.expr.map(|e| print_const_expr(tcx, e)).unwrap_or_default(),
741 impl FromWithTcx<clean::TraitAlias> for TraitAlias {
742 fn from_tcx(alias: clean::TraitAlias, tcx: TyCtxt<'_>) -> Self {
743 TraitAlias { generics: alias.generics.into_tcx(tcx), params: alias.bounds.into_tcx(tcx) }
747 impl FromWithTcx<ItemType> for ItemKind {
748 fn from_tcx(kind: ItemType, _tcx: TyCtxt<'_>) -> Self {
751 Module => ItemKind::Module,
752 ExternCrate => ItemKind::ExternCrate,
753 Import => ItemKind::Import,
754 Struct => ItemKind::Struct,
755 Union => ItemKind::Union,
756 Enum => ItemKind::Enum,
757 Function => ItemKind::Function,
758 Typedef => ItemKind::Typedef,
759 OpaqueTy => ItemKind::OpaqueTy,
760 Static => ItemKind::Static,
761 Constant => ItemKind::Constant,
762 Trait => ItemKind::Trait,
763 Impl => ItemKind::Impl,
764 TyMethod | Method => ItemKind::Method,
765 StructField => ItemKind::StructField,
766 Variant => ItemKind::Variant,
767 Macro => ItemKind::Macro,
768 Primitive => ItemKind::Primitive,
769 AssocConst => ItemKind::AssocConst,
770 AssocType => ItemKind::AssocType,
771 ForeignType => ItemKind::ForeignType,
772 Keyword => ItemKind::Keyword,
773 TraitAlias => ItemKind::TraitAlias,
774 ProcAttribute => ItemKind::ProcAttribute,
775 ProcDerive => ItemKind::ProcDerive,
780 fn ids(items: impl IntoIterator<Item = clean::Item>, tcx: TyCtxt<'_>) -> Vec<Id> {
783 .filter(|x| !x.is_stripped() && !x.is_keyword())
784 .map(|i| from_item_id_with_name(i.item_id, tcx, i.name))