Skip to content

Commit 186b16e

Browse files
committed
Handle errors for intra doc link path lookup
1 parent 52fcd6a commit 186b16e

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use rustc::middle::resolve_lifetime as rl;
3434
use rustc::middle::lang_items;
3535
use rustc::hir::def::{Def, CtorKind};
3636
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
37-
use rustc::hir::lowering::Resolver;
3837
use rustc::ty::subst::Substs;
3938
use rustc::ty::{self, Ty, AdtKind};
4039
use rustc::middle::stability;
@@ -828,25 +827,25 @@ impl Clean<Attributes> for [ast::Attribute] {
828827
let dox = attrs.collapsed_doc_value().unwrap_or_else(String::new);
829828
for link in markdown_links(&dox, cx.render_type) {
830829
let path = {
831-
let is_value: bool;
830+
let is_value;
832831
let path_str = if let Some(prefix) =
833832
["struct", "enum", "type", "trait", "union"].iter()
834833
.find(|p| link.starts_with(**p)) {
835-
is_value = false;
834+
is_value = Some(false);
836835
link.trim_left_matches(prefix).trim()
837836
} else if let Some(prefix) =
838837
["const", "static"].iter()
839838
.find(|p| link.starts_with(**p)) {
840-
is_value = true;
839+
is_value = Some(true);
841840
link.trim_left_matches(prefix).trim()
842841
} else if link.ends_with("()") {
843-
is_value = true;
842+
is_value = Some(true);
844843
link.trim_right_matches("()").trim()
845844
} else if link.ends_with("!") {
846845
// FIXME (misdreavus): macros are resolved with different machinery
847846
continue;
848847
} else {
849-
is_value = false;
848+
is_value = None;
850849
link.trim()
851850
};
852851

@@ -864,12 +863,34 @@ impl Clean<Attributes> for [ast::Attribute] {
864863
// but it can't because that would break object safety. This can still be
865864
// fixed.
866865
let components = path_str.split("::").skip(1).collect::<Vec<_>>();
867-
cx.resolver.borrow_mut().resolve_str_path(DUMMY_SP, None, &components, is_value)
866+
let resolve = |is_val| cx.resolver.borrow_mut().resolve_str_path_error(DUMMY_SP, None, &components, is_val);
867+
868+
if let Some(is_value) = is_value {
869+
if let Ok(path) = resolve(is_value) {
870+
path
871+
} else {
872+
// this could just be a normal link or a broken link
873+
// we could potentially check if something is
874+
// "intra-doc-link-like" and warn in that case
875+
continue;
876+
}
877+
} else {
878+
// try both!
879+
// It is imperative we search for not-a-value first
880+
// Otherwise we will find struct ctors for when we are looking
881+
// for structs, etc, and the link won't work.
882+
if let Ok(path) = resolve(false) {
883+
path
884+
} else if let Ok(path) = resolve(true) {
885+
path
886+
} else {
887+
// this could just be a normal link
888+
continue;
889+
}
890+
}
868891
};
869892

870-
if path.def != Def::Err {
871-
attrs.links.push((link, path.def.def_id()));
872-
}
893+
attrs.links.push((link, path.def.def_id()));
873894
}
874895

875896
cx.sess().abort_if_errors();

0 commit comments

Comments
 (0)