1 use crate::clean::auto_trait::AutoTraitFinder;
2 use crate::clean::blanket_impl::BlanketImplFinder;
3 use crate::clean::{
4     inline, Clean, Crate, ExternalCrate, Generic, GenericArg, GenericArgs, ImportSource, Item,
5     ItemKind, Lifetime, Path, PathSegment, Primitive, PrimitiveType, Type, TypeBinding, Visibility,
6 };
7 use crate::core::DocContext;
8 use crate::formats::item_type::ItemType;
9 use crate::visit_lib::LibEmbargoVisitor;
10 
11 use rustc_ast as ast;
12 use rustc_ast::tokenstream::TokenTree;
13 use rustc_hir as hir;
14 use rustc_hir::def::{DefKind, Res};
15 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
16 use rustc_middle::mir::interpret::ConstValue;
17 use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
18 use rustc_middle::ty::{self, DefIdTree, TyCtxt};
19 use rustc_span::symbol::{kw, sym, Symbol};
20 use std::fmt::Write as _;
21 use std::mem;
22 
23 #[cfg(test)]
24 mod tests;
25 
krate(cx: &mut DocContext<'_>) -> Crate26 crate fn krate(cx: &mut DocContext<'_>) -> Crate {
27     let module = crate::visit_ast::RustdocVisitor::new(cx).visit();
28 
29     for &cnum in cx.tcx.crates(()) {
30         // Analyze doc-reachability for extern items
31         LibEmbargoVisitor::new(cx).visit_lib(cnum);
32     }
33 
34     // Clean the crate, translating the entire librustc_ast AST to one that is
35     // understood by rustdoc.
36     let mut module = module.clean(cx);
37 
38     match *module.kind {
39         ItemKind::ModuleItem(ref module) => {
40             for it in &module.items {
41                 // `compiler_builtins` should be masked too, but we can't apply
42                 // `#[doc(masked)]` to the injected `extern crate` because it's unstable.
43                 if it.is_extern_crate()
44                     && (it.attrs.has_doc_flag(sym::masked)
45                         || cx.tcx.is_compiler_builtins(it.def_id.krate()))
46                 {
47                     cx.cache.masked_crates.insert(it.def_id.krate());
48                 }
49             }
50         }
51         _ => unreachable!(),
52     }
53 
54     let local_crate = ExternalCrate { crate_num: LOCAL_CRATE };
55     let primitives = local_crate.primitives(cx.tcx);
56     let keywords = local_crate.keywords(cx.tcx);
57     {
58         let m = match *module.kind {
59             ItemKind::ModuleItem(ref mut m) => m,
60             _ => unreachable!(),
61         };
62         m.items.extend(primitives.iter().map(|&(def_id, prim)| {
63             Item::from_def_id_and_parts(
64                 def_id,
65                 Some(prim.as_sym()),
66                 ItemKind::PrimitiveItem(prim),
67                 cx,
68             )
69         }));
70         m.items.extend(keywords.into_iter().map(|(def_id, kw)| {
71             Item::from_def_id_and_parts(def_id, Some(kw), ItemKind::KeywordItem(kw), cx)
72         }));
73     }
74 
75     Crate { module, primitives, external_traits: cx.external_traits.clone(), collapsed: false }
76 }
77 
external_generic_args( cx: &mut DocContext<'_>, did: DefId, has_self: bool, bindings: Vec<TypeBinding>, substs: SubstsRef<'_>, ) -> GenericArgs78 fn external_generic_args(
79     cx: &mut DocContext<'_>,
80     did: DefId,
81     has_self: bool,
82     bindings: Vec<TypeBinding>,
83     substs: SubstsRef<'_>,
84 ) -> GenericArgs {
85     let mut skip_self = has_self;
86     let mut ty_kind = None;
87     let args: Vec<_> = substs
88         .iter()
89         .filter_map(|kind| match kind.unpack() {
90             GenericArgKind::Lifetime(lt) => match lt {
91                 ty::ReLateBound(_, ty::BoundRegion { kind: ty::BrAnon(_), .. }) => {
92                     Some(GenericArg::Lifetime(Lifetime::elided()))
93                 }
94                 _ => lt.clean(cx).map(GenericArg::Lifetime),
95             },
96             GenericArgKind::Type(_) if skip_self => {
97                 skip_self = false;
98                 None
99             }
100             GenericArgKind::Type(ty) => {
101                 ty_kind = Some(ty.kind());
102                 Some(GenericArg::Type(ty.clean(cx)))
103             }
104             GenericArgKind::Const(ct) => Some(GenericArg::Const(Box::new(ct.clean(cx)))),
105         })
106         .collect();
107 
108     if cx.tcx.fn_trait_kind_from_lang_item(did).is_some() {
109         let inputs = match ty_kind.unwrap() {
110             ty::Tuple(tys) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(),
111             _ => return GenericArgs::AngleBracketed { args, bindings },
112         };
113         let output = None;
114         // FIXME(#20299) return type comes from a projection now
115         // match types[1].kind {
116         //     ty::Tuple(ref v) if v.is_empty() => None, // -> ()
117         //     _ => Some(types[1].clean(cx))
118         // };
119         GenericArgs::Parenthesized { inputs, output }
120     } else {
121         GenericArgs::AngleBracketed { args, bindings }
122     }
123 }
124 
external_path( cx: &mut DocContext<'_>, did: DefId, has_self: bool, bindings: Vec<TypeBinding>, substs: SubstsRef<'_>, ) -> Path125 pub(super) fn external_path(
126     cx: &mut DocContext<'_>,
127     did: DefId,
128     has_self: bool,
129     bindings: Vec<TypeBinding>,
130     substs: SubstsRef<'_>,
131 ) -> Path {
132     let def_kind = cx.tcx.def_kind(did);
133     let name = cx.tcx.item_name(did);
134     Path {
135         res: Res::Def(def_kind, did),
136         segments: vec![PathSegment {
137             name,
138             args: external_generic_args(cx, did, has_self, bindings, substs),
139         }],
140     }
141 }
142 
143 /// Remove the generic arguments from a path.
strip_path_generics(path: Path) -> Path144 crate fn strip_path_generics(path: Path) -> Path {
145     let segments = path
146         .segments
147         .iter()
148         .map(|s| PathSegment {
149             name: s.name,
150             args: GenericArgs::AngleBracketed { args: vec![], bindings: vec![] },
151         })
152         .collect();
153 
154     Path { res: path.res, segments }
155 }
156 
qpath_to_string(p: &hir::QPath<'_>) -> String157 crate fn qpath_to_string(p: &hir::QPath<'_>) -> String {
158     let segments = match *p {
159         hir::QPath::Resolved(_, path) => &path.segments,
160         hir::QPath::TypeRelative(_, segment) => return segment.ident.to_string(),
161         hir::QPath::LangItem(lang_item, ..) => return lang_item.name().to_string(),
162     };
163 
164     let mut s = String::new();
165     for (i, seg) in segments.iter().enumerate() {
166         if i > 0 {
167             s.push_str("::");
168         }
169         if seg.ident.name != kw::PathRoot {
170             s.push_str(&seg.ident.as_str());
171         }
172     }
173     s
174 }
175 
build_deref_target_impls(cx: &mut DocContext<'_>, items: &[Item], ret: &mut Vec<Item>)176 crate fn build_deref_target_impls(cx: &mut DocContext<'_>, items: &[Item], ret: &mut Vec<Item>) {
177     let tcx = cx.tcx;
178 
179     for item in items {
180         let target = match *item.kind {
181             ItemKind::TypedefItem(ref t, true) => &t.type_,
182             _ => continue,
183         };
184 
185         if let Some(prim) = target.primitive_type() {
186             for &did in prim.impls(tcx).iter().filter(|did| !did.is_local()) {
187                 inline::build_impl(cx, None, did, None, ret);
188             }
189         } else if let Type::Path { path } = target {
190             let did = path.def_id();
191             if !did.is_local() {
192                 inline::build_impls(cx, None, did, None, ret);
193             }
194         }
195     }
196 }
197 
name_from_pat(p: &hir::Pat<'_>) -> Symbol198 crate fn name_from_pat(p: &hir::Pat<'_>) -> Symbol {
199     use rustc_hir::*;
200     debug!("trying to get a name from pattern: {:?}", p);
201 
202     Symbol::intern(&match p.kind {
203         PatKind::Wild | PatKind::Struct(..) => return kw::Underscore,
204         PatKind::Binding(_, _, ident, _) => return ident.name,
205         PatKind::TupleStruct(ref p, ..) | PatKind::Path(ref p) => qpath_to_string(p),
206         PatKind::Or(pats) => {
207             pats.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(" | ")
208         }
209         PatKind::Tuple(elts, _) => format!(
210             "({})",
211             elts.iter().map(|p| name_from_pat(p).to_string()).collect::<Vec<String>>().join(", ")
212         ),
213         PatKind::Box(p) => return name_from_pat(&*p),
214         PatKind::Ref(p, _) => return name_from_pat(&*p),
215         PatKind::Lit(..) => {
216             warn!(
217                 "tried to get argument name from PatKind::Lit, which is silly in function arguments"
218             );
219             return Symbol::intern("()");
220         }
221         PatKind::Range(..) => return kw::Underscore,
222         PatKind::Slice(begin, ref mid, end) => {
223             let begin = begin.iter().map(|p| name_from_pat(p).to_string());
224             let mid = mid.as_ref().map(|p| format!("..{}", name_from_pat(&**p))).into_iter();
225             let end = end.iter().map(|p| name_from_pat(p).to_string());
226             format!("[{}]", begin.chain(mid).chain(end).collect::<Vec<_>>().join(", "))
227         }
228     })
229 }
230 
print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String231 crate fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String {
232     match n.val {
233         ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs_: _, promoted }) => {
234             let mut s = if let Some(def) = def.as_local() {
235                 let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def.did);
236                 print_const_expr(cx.tcx, cx.tcx.hir().body_owned_by(hir_id))
237             } else {
238                 inline::print_inlined_const(cx.tcx, def.did)
239             };
240             if let Some(promoted) = promoted {
241                 s.push_str(&format!("::{:?}", promoted))
242             }
243             s
244         }
245         _ => {
246             let mut s = n.to_string();
247             // array lengths are obviously usize
248             if s.ends_with("_usize") {
249                 let n = s.len() - "_usize".len();
250                 s.truncate(n);
251                 if s.ends_with(": ") {
252                     let n = s.len() - ": ".len();
253                     s.truncate(n);
254                 }
255             }
256             s
257         }
258     }
259 }
260 
print_evaluated_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<String>261 crate fn print_evaluated_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<String> {
262     tcx.const_eval_poly(def_id).ok().and_then(|val| {
263         let ty = tcx.type_of(def_id);
264         match (val, ty.kind()) {
265             (_, &ty::Ref(..)) => None,
266             (ConstValue::Scalar(_), &ty::Adt(_, _)) => None,
267             (ConstValue::Scalar(_), _) => {
268                 let const_ = ty::Const::from_value(tcx, val, ty);
269                 Some(print_const_with_custom_print_scalar(tcx, const_))
270             }
271             _ => None,
272         }
273     })
274 }
275 
format_integer_with_underscore_sep(num: &str) -> String276 fn format_integer_with_underscore_sep(num: &str) -> String {
277     let num_chars: Vec<_> = num.chars().collect();
278     let mut num_start_index = if num_chars.get(0) == Some(&'-') { 1 } else { 0 };
279     let chunk_size = match num[num_start_index..].as_bytes() {
280         [b'0', b'b' | b'x', ..] => {
281             num_start_index += 2;
282             4
283         }
284         [b'0', b'o', ..] => {
285             num_start_index += 2;
286             let remaining_chars = num_chars.len() - num_start_index;
287             if remaining_chars <= 6 {
288                 // don't add underscores to Unix permissions like 0755 or 100755
289                 return num.to_string();
290             }
291             3
292         }
293         _ => 3,
294     };
295 
296     num_chars[..num_start_index]
297         .iter()
298         .chain(num_chars[num_start_index..].rchunks(chunk_size).rev().intersperse(&['_']).flatten())
299         .collect()
300 }
301 
print_const_with_custom_print_scalar(tcx: TyCtxt<'_>, ct: &'tcx ty::Const<'tcx>) -> String302 fn print_const_with_custom_print_scalar(tcx: TyCtxt<'_>, ct: &'tcx ty::Const<'tcx>) -> String {
303     // Use a slightly different format for integer types which always shows the actual value.
304     // For all other types, fallback to the original `pretty_print_const`.
305     match (ct.val, ct.ty.kind()) {
306         (ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Uint(ui)) => {
307             format!("{}{}", format_integer_with_underscore_sep(&int.to_string()), ui.name_str())
308         }
309         (ty::ConstKind::Value(ConstValue::Scalar(int)), ty::Int(i)) => {
310             let ty = tcx.lift(ct.ty).unwrap();
311             let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size;
312             let data = int.assert_bits(size);
313             let sign_extended_data = size.sign_extend(data) as i128;
314 
315             format!(
316                 "{}{}",
317                 format_integer_with_underscore_sep(&sign_extended_data.to_string()),
318                 i.name_str()
319             )
320         }
321         _ => ct.to_string(),
322     }
323 }
324 
is_literal_expr(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool325 crate fn is_literal_expr(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
326     if let hir::Node::Expr(expr) = tcx.hir().get(hir_id) {
327         if let hir::ExprKind::Lit(_) = &expr.kind {
328             return true;
329         }
330 
331         if let hir::ExprKind::Unary(hir::UnOp::Neg, expr) = &expr.kind {
332             if let hir::ExprKind::Lit(_) = &expr.kind {
333                 return true;
334             }
335         }
336     }
337 
338     false
339 }
340 
print_const_expr(tcx: TyCtxt<'_>, body: hir::BodyId) -> String341 crate fn print_const_expr(tcx: TyCtxt<'_>, body: hir::BodyId) -> String {
342     let hir = tcx.hir();
343     let value = &hir.body(body).value;
344 
345     let snippet = if !value.span.from_expansion() {
346         tcx.sess.source_map().span_to_snippet(value.span).ok()
347     } else {
348         None
349     };
350 
351     snippet.unwrap_or_else(|| rustc_hir_pretty::id_to_string(&hir, body.hir_id))
352 }
353 
354 /// Given a type Path, resolve it to a Type using the TyCtxt
resolve_type(cx: &mut DocContext<'_>, path: Path) -> Type355 crate fn resolve_type(cx: &mut DocContext<'_>, path: Path) -> Type {
356     debug!("resolve_type({:?})", path);
357 
358     match path.res {
359         Res::PrimTy(p) => Primitive(PrimitiveType::from(p)),
360         Res::SelfTy(..) if path.segments.len() == 1 => Generic(kw::SelfUpper),
361         Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => Generic(path.segments[0].name),
362         _ => {
363             let _ = register_res(cx, path.res);
364             Type::Path { path }
365         }
366     }
367 }
368 
get_auto_trait_and_blanket_impls( cx: &mut DocContext<'tcx>, item_def_id: DefId, ) -> impl Iterator<Item = Item>369 crate fn get_auto_trait_and_blanket_impls(
370     cx: &mut DocContext<'tcx>,
371     item_def_id: DefId,
372 ) -> impl Iterator<Item = Item> {
373     let auto_impls = cx
374         .sess()
375         .prof
376         .generic_activity("get_auto_trait_impls")
377         .run(|| AutoTraitFinder::new(cx).get_auto_trait_impls(item_def_id));
378     let blanket_impls = cx
379         .sess()
380         .prof
381         .generic_activity("get_blanket_impls")
382         .run(|| BlanketImplFinder { cx }.get_blanket_impls(item_def_id));
383     auto_impls.into_iter().chain(blanket_impls)
384 }
385 
386 /// If `res` has a documentation page associated, store it in the cache.
387 ///
388 /// This is later used by [`href()`] to determine the HTML link for the item.
389 ///
390 /// [`href()`]: crate::html::format::href
register_res(cx: &mut DocContext<'_>, res: Res) -> DefId391 crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
392     use DefKind::*;
393     debug!("register_res({:?})", res);
394 
395     let (did, kind) = match res {
396         // These should be added to the cache using `record_extern_fqn`.
397         Res::Def(
398             kind
399             @
400             (AssocTy | AssocFn | AssocConst | Variant | Fn | TyAlias | Enum | Trait | Struct
401             | Union | Mod | ForeignTy | Const | Static | Macro(..) | TraitAlias),
402             i,
403         ) => (i, kind.into()),
404         // This is part of a trait definition; document the trait.
405         Res::SelfTy(Some(trait_def_id), _) => (trait_def_id, ItemType::Trait),
406         // This is an inherent impl; it doesn't have its own page.
407         Res::SelfTy(None, Some((impl_def_id, _))) => return impl_def_id,
408         Res::SelfTy(None, None)
409         | Res::PrimTy(_)
410         | Res::ToolMod
411         | Res::SelfCtor(_)
412         | Res::Local(_)
413         | Res::NonMacroAttr(_)
414         | Res::Err => return res.def_id(),
415         Res::Def(
416             TyParam | ConstParam | Ctor(..) | ExternCrate | Use | ForeignMod | AnonConst
417             | InlineConst | OpaqueTy | Field | LifetimeParam | GlobalAsm | Impl | Closure
418             | Generator,
419             id,
420         ) => return id,
421     };
422     if did.is_local() {
423         return did;
424     }
425     inline::record_extern_fqn(cx, did, kind);
426     if let ItemType::Trait = kind {
427         inline::record_extern_trait(cx, did);
428     }
429     did
430 }
431 
resolve_use_source(cx: &mut DocContext<'_>, path: Path) -> ImportSource432 crate fn resolve_use_source(cx: &mut DocContext<'_>, path: Path) -> ImportSource {
433     ImportSource {
434         did: if path.res.opt_def_id().is_none() { None } else { Some(register_res(cx, path.res)) },
435         path,
436     }
437 }
438 
enter_impl_trait<F, R>(cx: &mut DocContext<'_>, f: F) -> R where F: FnOnce(&mut DocContext<'_>) -> R,439 crate fn enter_impl_trait<F, R>(cx: &mut DocContext<'_>, f: F) -> R
440 where
441     F: FnOnce(&mut DocContext<'_>) -> R,
442 {
443     let old_bounds = mem::take(&mut cx.impl_trait_bounds);
444     let r = f(cx);
445     assert!(cx.impl_trait_bounds.is_empty());
446     cx.impl_trait_bounds = old_bounds;
447     r
448 }
449 
450 /// Find the nearest parent module of a [`DefId`].
find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId>451 crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
452     if def_id.is_top_level_module() {
453         // The crate root has no parent. Use it as the root instead.
454         Some(def_id)
455     } else {
456         let mut current = def_id;
457         // The immediate parent might not always be a module.
458         // Find the first parent which is.
459         while let Some(parent) = tcx.parent(current) {
460             if tcx.def_kind(parent) == DefKind::Mod {
461                 return Some(parent);
462             }
463             current = parent;
464         }
465         None
466     }
467 }
468 
469 /// Checks for the existence of `hidden` in the attribute below if `flag` is `sym::hidden`:
470 ///
471 /// ```
472 /// #[doc(hidden)]
473 /// pub fn foo() {}
474 /// ```
475 ///
476 /// This function exists because it runs on `hir::Attributes` whereas the other is a
477 /// `clean::Attributes` method.
has_doc_flag(attrs: ty::Attributes<'_>, flag: Symbol) -> bool478 crate fn has_doc_flag(attrs: ty::Attributes<'_>, flag: Symbol) -> bool {
479     attrs.iter().any(|attr| {
480         attr.has_name(sym::doc)
481             && attr.meta_item_list().map_or(false, |l| rustc_attr::list_contains_name(&l, flag))
482     })
483 }
484 
485 /// A link to `doc.rust-lang.org` that includes the channel name. Use this instead of manual links
486 /// so that the channel is consistent.
487 ///
488 /// Set by `bootstrap::Builder::doc_rust_lang_org_channel` in order to keep tests passing on beta/stable.
489 crate const DOC_RUST_LANG_ORG_CHANNEL: &str = env!("DOC_RUST_LANG_ORG_CHANNEL");
490 
491 /// Render a sequence of macro arms in a format suitable for displaying to the user
492 /// as part of an item declaration.
render_macro_arms<'a>( matchers: impl Iterator<Item = &'a TokenTree>, arm_delim: &str, ) -> String493 pub(super) fn render_macro_arms<'a>(
494     matchers: impl Iterator<Item = &'a TokenTree>,
495     arm_delim: &str,
496 ) -> String {
497     let mut out = String::new();
498     for matcher in matchers {
499         writeln!(out, "    {} => {{ ... }}{}", render_macro_matcher(matcher), arm_delim).unwrap();
500     }
501     out
502 }
503 
504 /// Render a macro matcher in a format suitable for displaying to the user
505 /// as part of an item declaration.
render_macro_matcher(matcher: &TokenTree) -> String506 pub(super) fn render_macro_matcher(matcher: &TokenTree) -> String {
507     rustc_ast_pretty::pprust::tt_to_string(matcher)
508 }
509 
display_macro_source( cx: &mut DocContext<'_>, name: Symbol, def: &ast::MacroDef, def_id: DefId, vis: Visibility, ) -> String510 pub(super) fn display_macro_source(
511     cx: &mut DocContext<'_>,
512     name: Symbol,
513     def: &ast::MacroDef,
514     def_id: DefId,
515     vis: Visibility,
516 ) -> String {
517     let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
518     // Extract the spans of all matchers. They represent the "interface" of the macro.
519     let matchers = tts.chunks(4).map(|arm| &arm[0]);
520 
521     if def.macro_rules {
522         format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";"))
523     } else {
524         if matchers.len() <= 1 {
525             format!(
526                 "{}macro {}{} {{\n    ...\n}}",
527                 vis.to_src_with_space(cx.tcx, def_id),
528                 name,
529                 matchers.map(render_macro_matcher).collect::<String>(),
530             )
531         } else {
532             format!(
533                 "{}macro {} {{\n{}}}",
534                 vis.to_src_with_space(cx.tcx, def_id),
535                 name,
536                 render_macro_arms(matchers, ","),
537             )
538         }
539     }
540 }
541