]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/clean/inline.rs
Auto merge of #105997 - RalfJung:immediate-abort, r=eholk
[rust.git] / src / librustdoc / clean / inline.rs
1 //! Support for inlining external documentation into the current AST.
2
3 use std::iter::once;
4 use std::sync::Arc;
5
6 use thin_vec::{thin_vec, ThinVec};
7
8 use rustc_ast as ast;
9 use rustc_data_structures::fx::FxHashSet;
10 use rustc_hir as hir;
11 use rustc_hir::def::{DefKind, Res};
12 use rustc_hir::def_id::DefId;
13 use rustc_hir::Mutability;
14 use rustc_metadata::creader::{CStore, LoadedMacro};
15 use rustc_middle::ty::{self, TyCtxt};
16 use rustc_span::hygiene::MacroKind;
17 use rustc_span::symbol::{kw, sym, Symbol};
18
19 use crate::clean::{
20     self, clean_fn_decl_from_did_and_sig, clean_generics, clean_impl_item, clean_middle_assoc_item,
21     clean_middle_field, clean_middle_ty, clean_trait_ref_with_bindings, clean_ty,
22     clean_ty_generics, clean_variant_def, utils, Attributes, AttributesExt, ImplKind, ItemId, Type,
23 };
24 use crate::core::DocContext;
25 use crate::formats::item_type::ItemType;
26
27 /// Attempt to inline a definition into this AST.
28 ///
29 /// This function will fetch the definition specified, and if it is
30 /// from another crate it will attempt to inline the documentation
31 /// from the other crate into this crate.
32 ///
33 /// This is primarily used for `pub use` statements which are, in general,
34 /// implementation details. Inlining the documentation should help provide a
35 /// better experience when reading the documentation in this use case.
36 ///
37 /// The returned value is `None` if the definition could not be inlined,
38 /// and `Some` of a vector of items if it was successfully expanded.
39 ///
40 /// `parent_module` refers to the parent of the *re-export*, not the original item.
41 pub(crate) fn try_inline(
42     cx: &mut DocContext<'_>,
43     parent_module: DefId,
44     import_def_id: Option<DefId>,
45     res: Res,
46     name: Symbol,
47     attrs: Option<&[ast::Attribute]>,
48     visited: &mut FxHashSet<DefId>,
49 ) -> Option<Vec<clean::Item>> {
50     let did = res.opt_def_id()?;
51     if did.is_local() {
52         return None;
53     }
54     let mut ret = Vec::new();
55
56     debug!("attrs={:?}", attrs);
57
58     let attrs_without_docs = attrs.map(|attrs| {
59         attrs.into_iter().filter(|a| a.doc_str().is_none()).cloned().collect::<Vec<_>>()
60     });
61     // We need this ugly code because:
62     //
63     // ```
64     // attrs_without_docs.map(|a| a.as_slice())
65     // ```
66     //
67     // will fail because it returns a temporary slice and:
68     //
69     // ```
70     // attrs_without_docs.map(|s| {
71     //     vec = s.as_slice();
72     //     vec
73     // })
74     // ```
75     //
76     // will fail because we're moving an uninitialized variable into a closure.
77     let vec;
78     let attrs_without_docs = match attrs_without_docs {
79         Some(s) => {
80             vec = s;
81             Some(vec.as_slice())
82         }
83         None => None,
84     };
85
86     let kind = match res {
87         Res::Def(DefKind::Trait, did) => {
88             record_extern_fqn(cx, did, ItemType::Trait);
89             build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
90             clean::TraitItem(Box::new(build_external_trait(cx, did)))
91         }
92         Res::Def(DefKind::Fn, did) => {
93             record_extern_fqn(cx, did, ItemType::Function);
94             clean::FunctionItem(build_external_function(cx, did))
95         }
96         Res::Def(DefKind::Struct, did) => {
97             record_extern_fqn(cx, did, ItemType::Struct);
98             build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
99             clean::StructItem(build_struct(cx, did))
100         }
101         Res::Def(DefKind::Union, did) => {
102             record_extern_fqn(cx, did, ItemType::Union);
103             build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
104             clean::UnionItem(build_union(cx, did))
105         }
106         Res::Def(DefKind::TyAlias, did) => {
107             record_extern_fqn(cx, did, ItemType::Typedef);
108             build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
109             clean::TypedefItem(build_type_alias(cx, did))
110         }
111         Res::Def(DefKind::Enum, did) => {
112             record_extern_fqn(cx, did, ItemType::Enum);
113             build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
114             clean::EnumItem(build_enum(cx, did))
115         }
116         Res::Def(DefKind::ForeignTy, did) => {
117             record_extern_fqn(cx, did, ItemType::ForeignType);
118             build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
119             clean::ForeignTypeItem
120         }
121         // Never inline enum variants but leave them shown as re-exports.
122         Res::Def(DefKind::Variant, _) => return None,
123         // Assume that enum variants and struct types are re-exported next to
124         // their constructors.
125         Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
126         Res::Def(DefKind::Mod, did) => {
127             record_extern_fqn(cx, did, ItemType::Module);
128             clean::ModuleItem(build_module(cx, did, visited))
129         }
130         Res::Def(DefKind::Static(_), did) => {
131             record_extern_fqn(cx, did, ItemType::Static);
132             clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
133         }
134         Res::Def(DefKind::Const, did) => {
135             record_extern_fqn(cx, did, ItemType::Constant);
136             clean::ConstantItem(build_const(cx, did))
137         }
138         Res::Def(DefKind::Macro(kind), did) => {
139             let mac = build_macro(cx, did, name, import_def_id);
140
141             let type_kind = match kind {
142                 MacroKind::Bang => ItemType::Macro,
143                 MacroKind::Attr => ItemType::ProcAttribute,
144                 MacroKind::Derive => ItemType::ProcDerive,
145             };
146             record_extern_fqn(cx, did, type_kind);
147             mac
148         }
149         _ => return None,
150     };
151
152     let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs);
153     cx.inlined.insert(did.into());
154     let mut item =
155         clean::Item::from_def_id_and_attrs_and_parts(did, Some(name), kind, Box::new(attrs), cfg);
156     // The visibility needs to reflect the one from the reexport and not from the "source" DefId.
157     item.inline_stmt_id = import_def_id;
158     ret.push(item);
159     Some(ret)
160 }
161
162 pub(crate) fn try_inline_glob(
163     cx: &mut DocContext<'_>,
164     res: Res,
165     visited: &mut FxHashSet<DefId>,
166     inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
167 ) -> Option<Vec<clean::Item>> {
168     let did = res.opt_def_id()?;
169     if did.is_local() {
170         return None;
171     }
172
173     match res {
174         Res::Def(DefKind::Mod, did) => {
175             let mut items = build_module_items(cx, did, visited, inlined_names);
176             items.drain_filter(|item| {
177                 if let Some(name) = item.name {
178                     // If an item with the same type and name already exists,
179                     // it takes priority over the inlined stuff.
180                     !inlined_names.insert((item.type_(), name))
181                 } else {
182                     false
183                 }
184             });
185             Some(items)
186         }
187         // glob imports on things like enums aren't inlined even for local exports, so just bail
188         _ => None,
189     }
190 }
191
192 pub(crate) fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> &'hir [ast::Attribute] {
193     cx.tcx.get_attrs_unchecked(did)
194 }
195
196 /// Record an external fully qualified name in the external_paths cache.
197 ///
198 /// These names are used later on by HTML rendering to generate things like
199 /// source links back to the original item.
200 pub(crate) fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType) {
201     let crate_name = cx.tcx.crate_name(did.krate);
202
203     let relative =
204         cx.tcx.def_path(did).data.into_iter().filter_map(|elem| elem.data.get_opt_name());
205     let fqn = if let ItemType::Macro = kind {
206         // Check to see if it is a macro 2.0 or built-in macro
207         if matches!(
208             CStore::from_tcx(cx.tcx).load_macro_untracked(did, cx.sess()),
209             LoadedMacro::MacroDef(def, _)
210                 if matches!(&def.kind, ast::ItemKind::MacroDef(ast_def)
211                     if !ast_def.macro_rules)
212         ) {
213             once(crate_name).chain(relative).collect()
214         } else {
215             vec![crate_name, relative.last().expect("relative was empty")]
216         }
217     } else {
218         once(crate_name).chain(relative).collect()
219     };
220
221     if did.is_local() {
222         cx.cache.exact_paths.insert(did, fqn);
223     } else {
224         cx.cache.external_paths.insert(did, (fqn, kind));
225     }
226 }
227
228 pub(crate) fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
229     let trait_items = cx
230         .tcx
231         .associated_items(did)
232         .in_definition_order()
233         .map(|item| clean_middle_assoc_item(item, cx))
234         .collect();
235
236     let predicates = cx.tcx.predicates_of(did);
237     let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
238     let generics = filter_non_trait_generics(did, generics);
239     let (generics, supertrait_bounds) = separate_supertrait_bounds(generics);
240     clean::Trait { def_id: did, generics, items: trait_items, bounds: supertrait_bounds }
241 }
242
243 fn build_external_function<'tcx>(cx: &mut DocContext<'tcx>, did: DefId) -> Box<clean::Function> {
244     let sig = cx.tcx.fn_sig(did);
245
246     let late_bound_regions = sig.bound_vars().into_iter().filter_map(|var| match var {
247         ty::BoundVariableKind::Region(ty::BrNamed(_, name)) if name != kw::UnderscoreLifetime => {
248             Some(clean::GenericParamDef::lifetime(name))
249         }
250         _ => None,
251     });
252
253     let predicates = cx.tcx.explicit_predicates_of(did);
254     let (generics, decl) = clean::enter_impl_trait(cx, |cx| {
255         // NOTE: generics need to be cleaned before the decl!
256         let mut generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
257         // FIXME: This does not place parameters in source order (late-bound ones come last)
258         generics.params.extend(late_bound_regions);
259         let decl = clean_fn_decl_from_did_and_sig(cx, Some(did), sig);
260         (generics, decl)
261     });
262     Box::new(clean::Function { decl, generics })
263 }
264
265 fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
266     let predicates = cx.tcx.explicit_predicates_of(did);
267
268     clean::Enum {
269         generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
270         variants: cx.tcx.adt_def(did).variants().iter().map(|v| clean_variant_def(v, cx)).collect(),
271     }
272 }
273
274 fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
275     let predicates = cx.tcx.explicit_predicates_of(did);
276     let variant = cx.tcx.adt_def(did).non_enum_variant();
277
278     clean::Struct {
279         ctor_kind: variant.ctor_kind(),
280         generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
281         fields: variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect(),
282     }
283 }
284
285 fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
286     let predicates = cx.tcx.explicit_predicates_of(did);
287     let variant = cx.tcx.adt_def(did).non_enum_variant();
288
289     let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
290     let fields = variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect();
291     clean::Union { generics, fields }
292 }
293
294 fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> Box<clean::Typedef> {
295     let predicates = cx.tcx.explicit_predicates_of(did);
296     let type_ = clean_middle_ty(ty::Binder::dummy(cx.tcx.type_of(did)), cx, Some(did));
297
298     Box::new(clean::Typedef {
299         type_,
300         generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
301         item_type: None,
302     })
303 }
304
305 /// Builds all inherent implementations of an ADT (struct/union/enum) or Trait item/path/reexport.
306 pub(crate) fn build_impls(
307     cx: &mut DocContext<'_>,
308     parent_module: Option<DefId>,
309     did: DefId,
310     attrs: Option<&[ast::Attribute]>,
311     ret: &mut Vec<clean::Item>,
312 ) {
313     let _prof_timer = cx.tcx.sess.prof.generic_activity("build_inherent_impls");
314     let tcx = cx.tcx;
315
316     // for each implementation of an item represented by `did`, build the clean::Item for that impl
317     for &did in tcx.inherent_impls(did).iter() {
318         build_impl(cx, parent_module, did, attrs, ret);
319     }
320
321     // This pretty much exists expressly for `dyn Error` traits that exist in the `alloc` crate.
322     // See also:
323     //
324     // * https://github.com/rust-lang/rust/issues/103170 â€” where it didn't used to get documented
325     // * https://github.com/rust-lang/rust/pull/99917 â€” where the feature got used
326     // * https://github.com/rust-lang/rust/issues/53487 â€” overall tracking issue for Error
327     if tcx.has_attr(did, sym::rustc_has_incoherent_inherent_impls) {
328         use rustc_middle::ty::fast_reject::SimplifiedType::*;
329         let type_ =
330             if tcx.is_trait(did) { TraitSimplifiedType(did) } else { AdtSimplifiedType(did) };
331         for &did in tcx.incoherent_impls(type_) {
332             build_impl(cx, parent_module, did, attrs, ret);
333         }
334     }
335 }
336
337 /// `parent_module` refers to the parent of the re-export, not the original item
338 pub(crate) fn merge_attrs(
339     cx: &mut DocContext<'_>,
340     parent_module: Option<DefId>,
341     old_attrs: &[ast::Attribute],
342     new_attrs: Option<&[ast::Attribute]>,
343 ) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
344     // NOTE: If we have additional attributes (from a re-export),
345     // always insert them first. This ensure that re-export
346     // doc comments show up before the original doc comments
347     // when we render them.
348     if let Some(inner) = new_attrs {
349         let mut both = inner.to_vec();
350         both.extend_from_slice(old_attrs);
351         (
352             if let Some(new_id) = parent_module {
353                 Attributes::from_ast_with_additional(old_attrs, (inner, new_id))
354             } else {
355                 Attributes::from_ast(&both)
356             },
357             both.cfg(cx.tcx, &cx.cache.hidden_cfg),
358         )
359     } else {
360         (Attributes::from_ast(&old_attrs), old_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg))
361     }
362 }
363
364 /// Inline an `impl`, inherent or of a trait. The `did` must be for an `impl`.
365 pub(crate) fn build_impl(
366     cx: &mut DocContext<'_>,
367     parent_module: Option<DefId>,
368     did: DefId,
369     attrs: Option<&[ast::Attribute]>,
370     ret: &mut Vec<clean::Item>,
371 ) {
372     if !cx.inlined.insert(did.into()) {
373         return;
374     }
375
376     let _prof_timer = cx.tcx.sess.prof.generic_activity("build_impl");
377
378     let tcx = cx.tcx;
379     let associated_trait = tcx.impl_trait_ref(did);
380
381     // Only inline impl if the implemented trait is
382     // reachable in rustdoc generated documentation
383     if !did.is_local() {
384         if let Some(traitref) = associated_trait {
385             let did = traitref.def_id;
386             if !cx.cache.effective_visibilities.is_directly_public(tcx, did) {
387                 return;
388             }
389
390             if let Some(stab) = tcx.lookup_stability(did) {
391                 if stab.is_unstable() && stab.feature == sym::rustc_private {
392                     return;
393                 }
394             }
395         }
396     }
397
398     let impl_item = match did.as_local() {
399         Some(did) => match &tcx.hir().expect_item(did).kind {
400             hir::ItemKind::Impl(impl_) => Some(impl_),
401             _ => panic!("`DefID` passed to `build_impl` is not an `impl"),
402         },
403         None => None,
404     };
405
406     let for_ = match &impl_item {
407         Some(impl_) => clean_ty(impl_.self_ty, cx),
408         None => clean_middle_ty(ty::Binder::dummy(tcx.type_of(did)), cx, Some(did)),
409     };
410
411     // Only inline impl if the implementing type is
412     // reachable in rustdoc generated documentation
413     if !did.is_local() {
414         if let Some(did) = for_.def_id(&cx.cache) {
415             if !cx.cache.effective_visibilities.is_directly_public(tcx, did) {
416                 return;
417             }
418
419             if let Some(stab) = tcx.lookup_stability(did) {
420                 if stab.is_unstable() && stab.feature == sym::rustc_private {
421                     return;
422                 }
423             }
424         }
425     }
426
427     let document_hidden = cx.render_options.document_hidden;
428     let predicates = tcx.explicit_predicates_of(did);
429     let (trait_items, generics) = match impl_item {
430         Some(impl_) => (
431             impl_
432                 .items
433                 .iter()
434                 .map(|item| tcx.hir().impl_item(item.id))
435                 .filter(|item| {
436                     // Filter out impl items whose corresponding trait item has `doc(hidden)`
437                     // not to document such impl items.
438                     // For inherent impls, we don't do any filtering, because that's already done in strip_hidden.rs.
439
440                     // When `--document-hidden-items` is passed, we don't
441                     // do any filtering, too.
442                     if document_hidden {
443                         return true;
444                     }
445                     if let Some(associated_trait) = associated_trait {
446                         let assoc_kind = match item.kind {
447                             hir::ImplItemKind::Const(..) => ty::AssocKind::Const,
448                             hir::ImplItemKind::Fn(..) => ty::AssocKind::Fn,
449                             hir::ImplItemKind::Type(..) => ty::AssocKind::Type,
450                         };
451                         let trait_item = tcx
452                             .associated_items(associated_trait.def_id)
453                             .find_by_name_and_kind(
454                                 tcx,
455                                 item.ident,
456                                 assoc_kind,
457                                 associated_trait.def_id,
458                             )
459                             .unwrap(); // SAFETY: For all impl items there exists trait item that has the same name.
460                         !tcx.is_doc_hidden(trait_item.def_id)
461                     } else {
462                         true
463                     }
464                 })
465                 .map(|item| clean_impl_item(item, cx))
466                 .collect::<Vec<_>>(),
467             clean_generics(impl_.generics, cx),
468         ),
469         None => (
470             tcx.associated_items(did)
471                 .in_definition_order()
472                 .filter(|item| {
473                     // If this is a trait impl, filter out associated items whose corresponding item
474                     // in the associated trait is marked `doc(hidden)`.
475                     // If this is an inherent impl, filter out private associated items.
476                     if let Some(associated_trait) = associated_trait {
477                         let trait_item = tcx
478                             .associated_items(associated_trait.def_id)
479                             .find_by_name_and_kind(
480                                 tcx,
481                                 item.ident(tcx),
482                                 item.kind,
483                                 associated_trait.def_id,
484                             )
485                             .unwrap(); // corresponding associated item has to exist
486                         !tcx.is_doc_hidden(trait_item.def_id)
487                     } else {
488                         item.visibility(tcx).is_public()
489                     }
490                 })
491                 .map(|item| clean_middle_assoc_item(item, cx))
492                 .collect::<Vec<_>>(),
493             clean::enter_impl_trait(cx, |cx| {
494                 clean_ty_generics(cx, tcx.generics_of(did), predicates)
495             }),
496         ),
497     };
498     let polarity = tcx.impl_polarity(did);
499     let trait_ = associated_trait
500         .map(|t| clean_trait_ref_with_bindings(cx, ty::Binder::dummy(t), ThinVec::new()));
501     if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
502         super::build_deref_target_impls(cx, &trait_items, ret);
503     }
504
505     // Return if the trait itself or any types of the generic parameters are doc(hidden).
506     let mut stack: Vec<&Type> = vec![&for_];
507
508     if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
509         if tcx.is_doc_hidden(did) {
510             return;
511         }
512     }
513     if let Some(generics) = trait_.as_ref().and_then(|t| t.generics()) {
514         stack.extend(generics);
515     }
516
517     while let Some(ty) = stack.pop() {
518         if let Some(did) = ty.def_id(&cx.cache) {
519             if tcx.is_doc_hidden(did) {
520                 return;
521             }
522         }
523         if let Some(generics) = ty.generics() {
524             stack.extend(generics);
525         }
526     }
527
528     if let Some(did) = trait_.as_ref().map(|t| t.def_id()) {
529         record_extern_trait(cx, did);
530     }
531
532     let (merged_attrs, cfg) = merge_attrs(cx, parent_module, load_attrs(cx, did), attrs);
533     trace!("merged_attrs={:?}", merged_attrs);
534
535     trace!(
536         "build_impl: impl {:?} for {:?}",
537         trait_.as_ref().map(|t| t.def_id()),
538         for_.def_id(&cx.cache)
539     );
540     ret.push(clean::Item::from_def_id_and_attrs_and_parts(
541         did,
542         None,
543         clean::ImplItem(Box::new(clean::Impl {
544             unsafety: hir::Unsafety::Normal,
545             generics,
546             trait_,
547             for_,
548             items: trait_items,
549             polarity,
550             kind: if utils::has_doc_flag(tcx, did, sym::fake_variadic) {
551                 ImplKind::FakeVaradic
552             } else {
553                 ImplKind::Normal
554             },
555         })),
556         Box::new(merged_attrs),
557         cfg,
558     ));
559 }
560
561 fn build_module(
562     cx: &mut DocContext<'_>,
563     did: DefId,
564     visited: &mut FxHashSet<DefId>,
565 ) -> clean::Module {
566     let items = build_module_items(cx, did, visited, &mut FxHashSet::default());
567
568     let span = clean::Span::new(cx.tcx.def_span(did));
569     clean::Module { items, span }
570 }
571
572 fn build_module_items(
573     cx: &mut DocContext<'_>,
574     did: DefId,
575     visited: &mut FxHashSet<DefId>,
576     inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
577 ) -> Vec<clean::Item> {
578     let mut items = Vec::new();
579
580     // If we're re-exporting a re-export it may actually re-export something in
581     // two namespaces, so the target may be listed twice. Make sure we only
582     // visit each node at most once.
583     for &item in cx.tcx.module_children(did).iter() {
584         if item.vis.is_public() {
585             let res = item.res.expect_non_local();
586             if let Some(def_id) = res.mod_def_id() {
587                 // If we're inlining a glob import, it's possible to have
588                 // two distinct modules with the same name. We don't want to
589                 // inline it, or mark any of its contents as visited.
590                 if did == def_id
591                     || inlined_names.contains(&(ItemType::Module, item.ident.name))
592                     || !visited.insert(def_id)
593                 {
594                     continue;
595                 }
596             }
597             if let Res::PrimTy(p) = res {
598                 // Primitive types can't be inlined so generate an import instead.
599                 let prim_ty = clean::PrimitiveType::from(p);
600                 items.push(clean::Item {
601                     name: None,
602                     attrs: Box::new(clean::Attributes::default()),
603                     item_id: ItemId::Primitive(prim_ty, did.krate),
604                     kind: Box::new(clean::ImportItem(clean::Import::new_simple(
605                         item.ident.name,
606                         clean::ImportSource {
607                             path: clean::Path {
608                                 res,
609                                 segments: thin_vec![clean::PathSegment {
610                                     name: prim_ty.as_sym(),
611                                     args: clean::GenericArgs::AngleBracketed {
612                                         args: Default::default(),
613                                         bindings: ThinVec::new(),
614                                     },
615                                 }],
616                             },
617                             did: None,
618                         },
619                         true,
620                     ))),
621                     cfg: None,
622                     inline_stmt_id: None,
623                 });
624             } else if let Some(i) = try_inline(cx, did, None, res, item.ident.name, None, visited) {
625                 items.extend(i)
626             }
627         }
628     }
629
630     items
631 }
632
633 pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
634     if let Some(did) = did.as_local() {
635         let hir_id = tcx.hir().local_def_id_to_hir_id(did);
636         rustc_hir_pretty::id_to_string(&tcx.hir(), hir_id)
637     } else {
638         tcx.rendered_const(did).clone()
639     }
640 }
641
642 fn build_const(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
643     clean::Constant {
644         type_: clean_middle_ty(ty::Binder::dummy(cx.tcx.type_of(def_id)), cx, Some(def_id)),
645         kind: clean::ConstantKind::Extern { def_id },
646     }
647 }
648
649 fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
650     clean::Static {
651         type_: clean_middle_ty(ty::Binder::dummy(cx.tcx.type_of(did)), cx, Some(did)),
652         mutability: if mutable { Mutability::Mut } else { Mutability::Not },
653         expr: None,
654     }
655 }
656
657 fn build_macro(
658     cx: &mut DocContext<'_>,
659     def_id: DefId,
660     name: Symbol,
661     import_def_id: Option<DefId>,
662 ) -> clean::ItemKind {
663     match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.sess()) {
664         LoadedMacro::MacroDef(item_def, _) => {
665             if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
666                 let vis = cx.tcx.visibility(import_def_id.unwrap_or(def_id));
667                 clean::MacroItem(clean::Macro {
668                     source: utils::display_macro_source(cx, name, def, def_id, vis),
669                 })
670             } else {
671                 unreachable!()
672             }
673         }
674         LoadedMacro::ProcMacro(ext) => clean::ProcMacroItem(clean::ProcMacro {
675             kind: ext.macro_kind(),
676             helpers: ext.helper_attrs,
677         }),
678     }
679 }
680
681 /// A trait's generics clause actually contains all of the predicates for all of
682 /// its associated types as well. We specifically move these clauses to the
683 /// associated types instead when displaying, so when we're generating the
684 /// generics for the trait itself we need to be sure to remove them.
685 /// We also need to remove the implied "recursive" Self: Trait bound.
686 ///
687 /// The inverse of this filtering logic can be found in the `Clean`
688 /// implementation for `AssociatedType`
689 fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean::Generics {
690     for pred in &mut g.where_predicates {
691         match *pred {
692             clean::WherePredicate::BoundPredicate {
693                 ty: clean::Generic(ref s),
694                 ref mut bounds,
695                 ..
696             } if *s == kw::SelfUpper => {
697                 bounds.retain(|bound| match bound {
698                     clean::GenericBound::TraitBound(clean::PolyTrait { trait_, .. }, _) => {
699                         trait_.def_id() != trait_did
700                     }
701                     _ => true,
702                 });
703             }
704             _ => {}
705         }
706     }
707
708     g.where_predicates.retain(|pred| match pred {
709         clean::WherePredicate::BoundPredicate {
710             ty: clean::QPath(box clean::QPathData { self_type: clean::Generic(ref s), trait_, .. }),
711             bounds,
712             ..
713         } => !(bounds.is_empty() || *s == kw::SelfUpper && trait_.def_id() == trait_did),
714         _ => true,
715     });
716     g
717 }
718
719 /// Supertrait bounds for a trait are also listed in the generics coming from
720 /// the metadata for a crate, so we want to separate those out and create a new
721 /// list of explicit supertrait bounds to render nicely.
722 fn separate_supertrait_bounds(
723     mut g: clean::Generics,
724 ) -> (clean::Generics, Vec<clean::GenericBound>) {
725     let mut ty_bounds = Vec::new();
726     g.where_predicates.retain(|pred| match *pred {
727         clean::WherePredicate::BoundPredicate { ty: clean::Generic(ref s), ref bounds, .. }
728             if *s == kw::SelfUpper =>
729         {
730             ty_bounds.extend(bounds.iter().cloned());
731             false
732         }
733         _ => true,
734     });
735     (g, ty_bounds)
736 }
737
738 pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
739     if did.is_local() {
740         return;
741     }
742
743     {
744         if cx.external_traits.borrow().contains_key(&did) || cx.active_extern_traits.contains(&did)
745         {
746             return;
747         }
748     }
749
750     {
751         cx.active_extern_traits.insert(did);
752     }
753
754     debug!("record_extern_trait: {:?}", did);
755     let trait_ = build_external_trait(cx, did);
756
757     cx.external_traits.borrow_mut().insert(did, trait_);
758     cx.active_extern_traits.remove(&did);
759 }