1 use errors::Applicability;
2 use rustc::hir::def::{Res, DefKind, Namespace::{self, *}, PerNS};
3 use rustc::hir::def_id::DefId;
5 use rustc::lint as lint;
7 use rustc_resolve::ParentScope;
9 use syntax::ast::{self, Ident};
10 use syntax_expand::base::SyntaxExtensionKind;
11 use syntax::feature_gate::UnstableFeatures;
12 use syntax::symbol::Symbol;
13 use syntax_pos::DUMMY_SP;
17 use crate::core::DocContext;
18 use crate::fold::DocFolder;
19 use crate::html::markdown::markdown_links;
21 use crate::passes::{look_for_tests, Pass};
23 use super::span_of_attrs;
25 pub const COLLECT_INTRA_DOC_LINKS: Pass = Pass {
26 name: "collect-intra-doc-links",
27 pass: collect_intra_doc_links,
28 description: "reads a crate's documentation to resolve intra-doc-links",
31 pub fn collect_intra_doc_links(krate: Crate, cx: &DocContext<'_>) -> Crate {
32 if !UnstableFeatures::from_environment().is_nightly_build() {
35 let mut coll = LinkCollector::new(cx);
37 coll.fold_crate(krate)
41 struct LinkCollector<'a, 'tcx> {
42 cx: &'a DocContext<'tcx>,
43 mod_ids: Vec<hir::HirId>,
46 impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
47 fn new(cx: &'a DocContext<'tcx>) -> Self {
54 /// Resolves a string as a path within a particular namespace. Also returns an optional
55 /// URL fragment in the case of variants and methods.
59 current_item: &Option<String>,
60 parent_id: Option<hir::HirId>)
61 -> Result<(Res, Option<String>), ()>
65 // In case we're in a module, try to resolve the relative path.
66 if let Some(module_id) = parent_id.or(self.mod_ids.last().cloned()) {
67 let module_id = cx.tcx.hir().hir_to_node_id(module_id);
68 let result = cx.enter_resolver(|resolver| {
69 resolver.resolve_str_path_error(DUMMY_SP, &path_str, ns, module_id)
71 let result = match result {
72 Ok((_, Res::Err)) => Err(()),
76 if let Ok((_, res)) = result {
77 let res = res.map_id(|_| panic!("unexpected node_id"));
78 // In case this is a trait item, skip the
79 // early return and try looking for the trait.
80 let value = match res {
81 Res::Def(DefKind::Method, _) | Res::Def(DefKind::AssocConst, _) => true,
82 Res::Def(DefKind::AssocTy, _) => false,
83 Res::Def(DefKind::Variant, _) => return handle_variant(cx, res),
84 // Not a trait item; just return what we found.
85 Res::PrimTy(..) => return Ok((res, Some(path_str.to_owned()))),
86 _ => return Ok((res, None))
89 if value != (ns == ValueNS) {
92 } else if let Some(prim) = is_primitive(path_str, ns) {
93 return Ok((prim, Some(path_str.to_owned())))
95 // If resolution failed, it may still be a method
96 // because methods are not handled by the resolver
97 // If so, bail when we're not looking for a value.
103 // Try looking for methods and associated items.
104 let mut split = path_str.rsplitn(2, "::");
105 let item_name = if let Some(first) = split.next() {
106 Symbol::intern(first)
111 let mut path = if let Some(second) = split.next() {
117 if path == "self" || path == "Self" {
118 if let Some(name) = current_item.as_ref() {
122 if let Some(prim) = is_primitive(&path, TypeNS) {
123 let did = primitive_impl(cx, &path).ok_or(())?;
124 return cx.tcx.associated_items(did)
125 .find(|item| item.ident.name == item_name)
126 .and_then(|item| match item.kind {
127 ty::AssocKind::Method => Some("method"),
130 .map(|out| (prim, Some(format!("{}#{}.{}", path, out, item_name))))
134 let (_, ty_res) = cx.enter_resolver(|resolver| {
135 resolver.resolve_str_path_error(DUMMY_SP, &path, TypeNS, module_id)
137 if let Res::Err = ty_res {
140 let ty_res = ty_res.map_id(|_| panic!("unexpected node_id"));
142 Res::Def(DefKind::Struct, did)
143 | Res::Def(DefKind::Union, did)
144 | Res::Def(DefKind::Enum, did)
145 | Res::Def(DefKind::TyAlias, did) => {
146 let item = cx.tcx.inherent_impls(did)
148 .flat_map(|imp| cx.tcx.associated_items(*imp))
149 .find(|item| item.ident.name == item_name);
150 if let Some(item) = item {
151 let out = match item.kind {
152 ty::AssocKind::Method if ns == ValueNS => "method",
153 ty::AssocKind::Const if ns == ValueNS => "associatedconstant",
156 Ok((ty_res, Some(format!("{}.{}", out, item_name))))
158 match cx.tcx.type_of(did).kind {
160 if let Some(item) = if def.is_enum() {
161 def.all_fields().find(|item| item.ident.name == item_name)
163 def.non_enum_variant()
166 .find(|item| item.ident.name == item_name)
169 Some(format!("{}.{}",
184 Res::Def(DefKind::Trait, did) => {
185 let item = cx.tcx.associated_item_def_ids(did).iter()
186 .map(|item| cx.tcx.associated_item(*item))
187 .find(|item| item.ident.name == item_name);
188 if let Some(item) = item {
189 let kind = match item.kind {
190 ty::AssocKind::Const if ns == ValueNS => "associatedconstant",
191 ty::AssocKind::Type if ns == TypeNS => "associatedtype",
192 ty::AssocKind::Method if ns == ValueNS => {
193 if item.defaultness.has_value() {
202 Ok((ty_res, Some(format!("{}.{}", kind, item_name))))
210 debug!("attempting to resolve item without parent module: {}", path_str);
216 impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
217 fn fold_item(&mut self, mut item: Item) -> Option<Item> {
218 let item_hir_id = if item.is_mod() {
219 if let Some(id) = self.cx.tcx.hir().as_local_hir_id(item.def_id) {
222 debug!("attempting to fold on a non-local item: {:?}", item);
223 return self.fold_item_recur(item);
229 // FIXME: get the resolver to work with non-local resolve scopes.
230 let parent_node = self.cx.as_local_hir_id(item.def_id).and_then(|hir_id| {
231 // FIXME: this fails hard for impls in non-module scope, but is necessary for the
232 // current `resolve()` implementation.
233 match self.cx.tcx.hir().get_module_parent_node(hir_id) {
234 id if id != hir_id => Some(id),
239 if parent_node.is_some() {
240 debug!("got parent node for {:?} {:?}, id {:?}", item.type_(), item.name, item.def_id);
243 let current_item = match item.inner {
245 if item.attrs.inner_docs {
246 if item_hir_id.unwrap() != hir::CRATE_HIR_ID {
252 match parent_node.or(self.mod_ids.last().cloned()) {
253 Some(parent) if parent != hir::CRATE_HIR_ID => {
254 // FIXME: can we pull the parent module's name from elsewhere?
255 Some(self.cx.tcx.hir().name(parent).to_string())
261 ImplItem(Impl { ref for_, .. }) => {
262 for_.def_id().map(|did| self.cx.tcx.item_name(did).to_string())
264 // we don't display docs on `extern crate` items anyway, so don't process them.
265 ExternCrateItem(..) => return self.fold_item_recur(item),
266 ImportItem(Import::Simple(ref name, ..)) => Some(name.clone()),
267 MacroItem(..) => None,
268 _ => item.name.clone(),
271 if item.is_mod() && item.attrs.inner_docs {
272 self.mod_ids.push(item_hir_id.unwrap());
276 let dox = item.attrs.collapsed_doc_value().unwrap_or_else(String::new);
278 look_for_tests(&cx, &dox, &item, true);
280 for (ori_link, link_range) in markdown_links(&dox) {
281 // Bail early for real links.
282 if ori_link.contains('/') {
286 // [] is mostly likely not supposed to be a link
287 if ori_link.is_empty() {
291 let link = ori_link.replace("`", "");
292 let (res, fragment) = {
294 let path_str = if let Some(prefix) =
295 ["struct@", "enum@", "type@",
296 "trait@", "union@"].iter()
297 .find(|p| link.starts_with(**p)) {
299 link.trim_start_matches(prefix)
300 } else if let Some(prefix) =
301 ["const@", "static@",
302 "value@", "function@", "mod@",
303 "fn@", "module@", "method@"]
304 .iter().find(|p| link.starts_with(**p)) {
305 kind = Some(ValueNS);
306 link.trim_start_matches(prefix)
307 } else if link.ends_with("()") {
308 kind = Some(ValueNS);
309 link.trim_end_matches("()")
310 } else if link.starts_with("macro@") {
311 kind = Some(MacroNS);
312 link.trim_start_matches("macro@")
313 } else if link.ends_with('!') {
314 kind = Some(MacroNS);
315 link.trim_end_matches('!')
320 if path_str.contains(|ch: char| !(ch.is_alphanumeric() ||
321 ch == ':' || ch == '_')) {
325 // In order to correctly resolve intra-doc-links we need to
326 // pick a base AST node to work from. If the documentation for
327 // this module came from an inner comment (//!) then we anchor
328 // our name resolution *inside* the module. If, on the other
329 // hand it was an outer comment (///) then we anchor the name
330 // resolution in the parent module on the basis that the names
331 // used are more likely to be intended to be parent names. For
332 // this, we set base_node to None for inner comments since
333 // we've already pushed this node onto the resolution stack but
334 // for outer comments we explicitly try and resolve against the
335 // parent_node first.
336 let base_node = if item.is_mod() && item.attrs.inner_docs {
343 Some(ns @ ValueNS) => {
344 if let Ok(res) = self.resolve(path_str, ns, ¤t_item, base_node) {
347 resolution_failure(cx, &item, path_str, &dox, link_range);
348 // This could just be a normal link or a broken link
349 // we could potentially check if something is
350 // "intra-doc-link-like" and warn in that case.
354 Some(ns @ TypeNS) => {
355 if let Ok(res) = self.resolve(path_str, ns, ¤t_item, base_node) {
358 resolution_failure(cx, &item, path_str, &dox, link_range);
359 // This could just be a normal link.
365 let candidates = PerNS {
366 macro_ns: macro_resolve(cx, path_str).map(|res| (res, None)),
368 .resolve(path_str, TypeNS, ¤t_item, base_node)
371 .resolve(path_str, ValueNS, ¤t_item, base_node)
373 .and_then(|(res, fragment)| {
374 // Constructors are picked up in the type namespace.
376 Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => None,
377 _ => Some((res, fragment))
382 if candidates.is_empty() {
383 resolution_failure(cx, &item, path_str, &dox, link_range);
384 // this could just be a normal link
388 let is_unambiguous = candidates.clone().present_items().count() == 1;
390 candidates.present_items().next().unwrap()
398 candidates.map(|candidate| candidate.map(|(res, _)| res)),
404 if let Some(res) = macro_resolve(cx, path_str) {
407 resolution_failure(cx, &item, path_str, &dox, link_range);
414 if let Res::PrimTy(_) = res {
415 item.attrs.links.push((ori_link, None, fragment));
417 let id = register_res(cx, res);
418 item.attrs.links.push((ori_link, Some(id), fragment));
422 if item.is_mod() && !item.attrs.inner_docs {
423 self.mod_ids.push(item_hir_id.unwrap());
427 let ret = self.fold_item_recur(item);
433 self.fold_item_recur(item)
437 // FIXME: if we can resolve intra-doc links from other crates, we can use the stock
438 // `fold_crate`, but until then we should avoid scanning `krate.external_traits` since those
439 // will never resolve properly
440 fn fold_crate(&mut self, mut c: Crate) -> Crate {
441 c.module = c.module.take().and_then(|module| self.fold_item(module));
447 /// Resolves a string as a macro.
448 fn macro_resolve(cx: &DocContext<'_>, path_str: &str) -> Option<Res> {
449 let path = ast::Path::from_ident(Ident::from_str(path_str));
450 cx.enter_resolver(|resolver| {
451 if let Ok((Some(ext), res)) = resolver.resolve_macro_path(
452 &path, None, &ParentScope::module(resolver.graph_root()), false, false
454 if let SyntaxExtensionKind::LegacyBang { .. } = ext.kind {
455 return Some(res.map_id(|_| panic!("unexpected id")));
458 if let Some(res) = resolver.all_macros().get(&Symbol::intern(path_str)) {
459 return Some(res.map_id(|_| panic!("unexpected id")));
465 /// Reports a resolution failure diagnostic.
467 /// If we cannot find the exact source span of the resolution failure, we use the span of the
468 /// documentation attributes themselves. This is a little heavy-handed, so we display the markdown
469 /// line containing the failure as a note as well.
470 fn resolution_failure(
475 link_range: Option<Range<usize>>,
477 let hir_id = match cx.as_local_hir_id(item.def_id) {
478 Some(hir_id) => hir_id,
480 // If non-local, no need to check anything.
484 let attrs = &item.attrs;
485 let sp = span_of_attrs(attrs).unwrap_or(item.source.span());
487 let mut diag = cx.tcx.struct_span_lint_hir(
488 lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
491 &format!("`[{}]` cannot be resolved, ignoring it...", path_str),
493 if let Some(link_range) = link_range {
494 if let Some(sp) = super::source_span_for_markdown_range(cx, dox, &link_range, attrs) {
496 diag.span_label(sp, "cannot be resolved, ignoring");
498 // blah blah blah\nblah\nblah [blah] blah blah\nblah blah
501 // last_new_line_offset
502 let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1);
503 let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
505 // Print the line containing the `link_range` and manually mark it with '^'s.
507 "the link appears in this line:\n\n{line}\n\
508 {indicator: <before$}{indicator:^<found$}",
511 before=link_range.start - last_new_line_offset,
512 found=link_range.len(),
516 diag.help("to escape `[` and `]` characters, just add '\\' before them like \
526 link_range: Option<Range<usize>>,
527 candidates: PerNS<Option<Res>>,
529 let hir_id = match cx.as_local_hir_id(item.def_id) {
530 Some(hir_id) => hir_id,
532 // If non-local, no need to check anything.
536 let attrs = &item.attrs;
537 let sp = span_of_attrs(attrs).unwrap_or(item.source.span());
539 let mut msg = format!("`{}` is ", path_str);
541 let candidates = [TypeNS, ValueNS, MacroNS].iter().filter_map(|&ns| {
542 candidates[ns].map(|res| (res, ns))
543 }).collect::<Vec<_>>();
544 match candidates.as_slice() {
545 [(first_def, _), (second_def, _)] => {
547 "both {} {} and {} {}",
550 second_def.article(),
555 let mut candidates = candidates.iter().peekable();
556 while let Some((res, _)) = candidates.next() {
557 if candidates.peek().is_some() {
558 msg += &format!("{} {}, ", res.article(), res.descr());
560 msg += &format!("and {} {}", res.article(), res.descr());
566 let mut diag = cx.tcx.struct_span_lint_hir(
567 lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
573 if let Some(link_range) = link_range {
574 if let Some(sp) = super::source_span_for_markdown_range(cx, dox, &link_range, attrs) {
576 diag.span_label(sp, "ambiguous link");
578 for (res, ns) in candidates {
579 let (action, mut suggestion) = match res {
580 Res::Def(DefKind::Method, _) | Res::Def(DefKind::Fn, _) => {
581 ("add parentheses", format!("{}()", path_str))
583 Res::Def(DefKind::Macro(..), _) => {
584 ("add an exclamation mark", format!("{}!", path_str))
587 let type_ = match (res, ns) {
588 (Res::Def(DefKind::Const, _), _) => "const",
589 (Res::Def(DefKind::Static, _), _) => "static",
590 (Res::Def(DefKind::Struct, _), _) => "struct",
591 (Res::Def(DefKind::Enum, _), _) => "enum",
592 (Res::Def(DefKind::Union, _), _) => "union",
593 (Res::Def(DefKind::Trait, _), _) => "trait",
594 (Res::Def(DefKind::Mod, _), _) => "module",
595 (_, TypeNS) => "type",
596 (_, ValueNS) => "value",
597 (_, MacroNS) => "macro",
600 // FIXME: if this is an implied shortcut link, it's bad style to suggest `@`
601 ("prefix with the item type", format!("{}@{}", type_, path_str))
605 if dox.bytes().nth(link_range.start) == Some(b'`') {
606 suggestion = format!("`{}`", suggestion);
609 diag.span_suggestion(
611 &format!("to link to the {}, {}", res.descr(), action),
613 Applicability::MaybeIncorrect,
617 // blah blah blah\nblah\nblah [blah] blah blah\nblah blah
620 // last_new_line_offset
621 let last_new_line_offset = dox[..link_range.start].rfind('\n').map_or(0, |n| n + 1);
622 let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
624 // Print the line containing the `link_range` and manually mark it with '^'s.
626 "the link appears in this line:\n\n{line}\n\
627 {indicator: <before$}{indicator:^<found$}",
630 before=link_range.start - last_new_line_offset,
631 found=link_range.len(),
639 /// Given an enum variant's res, return the res of its enum and the associated fragment.
640 fn handle_variant(cx: &DocContext<'_>, res: Res) -> Result<(Res, Option<String>), ()> {
641 use rustc::ty::DefIdTree;
643 let parent = if let Some(parent) = cx.tcx.parent(res.def_id()) {
648 let parent_def = Res::Def(DefKind::Enum, parent);
649 let variant = cx.tcx.expect_variant_res(res);
650 Ok((parent_def, Some(format!("{}.v", variant.ident.name))))
653 const PRIMITIVES: &[(&str, Res)] = &[
654 ("u8", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U8))),
655 ("u16", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U16))),
656 ("u32", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U32))),
657 ("u64", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U64))),
658 ("u128", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::U128))),
659 ("usize", Res::PrimTy(hir::PrimTy::Uint(syntax::ast::UintTy::Usize))),
660 ("i8", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I8))),
661 ("i16", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I16))),
662 ("i32", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I32))),
663 ("i64", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I64))),
664 ("i128", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::I128))),
665 ("isize", Res::PrimTy(hir::PrimTy::Int(syntax::ast::IntTy::Isize))),
666 ("f32", Res::PrimTy(hir::PrimTy::Float(syntax::ast::FloatTy::F32))),
667 ("f64", Res::PrimTy(hir::PrimTy::Float(syntax::ast::FloatTy::F64))),
668 ("str", Res::PrimTy(hir::PrimTy::Str)),
669 ("bool", Res::PrimTy(hir::PrimTy::Bool)),
670 ("char", Res::PrimTy(hir::PrimTy::Char)),
673 fn is_primitive(path_str: &str, ns: Namespace) -> Option<Res> {
675 PRIMITIVES.iter().find(|x| x.0 == path_str).map(|x| x.1)
681 fn primitive_impl(cx: &DocContext<'_>, path_str: &str) -> Option<DefId> {
684 "u8" => tcx.lang_items().u8_impl(),
685 "u16" => tcx.lang_items().u16_impl(),
686 "u32" => tcx.lang_items().u32_impl(),
687 "u64" => tcx.lang_items().u64_impl(),
688 "u128" => tcx.lang_items().u128_impl(),
689 "usize" => tcx.lang_items().usize_impl(),
690 "i8" => tcx.lang_items().i8_impl(),
691 "i16" => tcx.lang_items().i16_impl(),
692 "i32" => tcx.lang_items().i32_impl(),
693 "i64" => tcx.lang_items().i64_impl(),
694 "i128" => tcx.lang_items().i128_impl(),
695 "isize" => tcx.lang_items().isize_impl(),
696 "f32" => tcx.lang_items().f32_impl(),
697 "f64" => tcx.lang_items().f64_impl(),
698 "str" => tcx.lang_items().str_impl(),
699 "bool" => tcx.lang_items().bool_impl(),
700 "char" => tcx.lang_items().char_impl(),