Skip to content

Commit

Permalink
Warn if linking to a private item
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Jun 26, 2020
1 parent 14e65d5 commit 6f8bec9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern crate rustc_metadata;
extern crate rustc_middle;
extern crate rustc_mir;
extern crate rustc_parse;
extern crate rustc_privacy;
extern crate rustc_resolve;
extern crate rustc_session;
extern crate rustc_span as rustc_span;
Expand Down
30 changes: 27 additions & 3 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
let result = cx.enter_resolver(|resolver| {
resolver.resolve_str_path_error(DUMMY_SP, &path_str, ns, module_id)
});
debug!("{} resolved to {:?} in namespace {:?}", path_str, ns, result);
let result = match result {
Ok((_, Res::Err)) => Err(ErrorKind::ResolutionFailure),
_ => result.map_err(|_| ErrorKind::ResolutionFailure),
Expand All @@ -202,7 +203,11 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
}
return Ok((res, Some(path_str.to_owned())));
}
_ => return Ok((res, extra_fragment.clone())),
other => {
debug!("failed to resolve {} in namespace {:?} (got {:?})", path_str, ns, other);
debug!("extra_fragment is {:?}", extra_fragment);
return Ok((res, extra_fragment.clone()));
}
};

if value != (ns == ValueNS) {
Expand Down Expand Up @@ -555,9 +560,11 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
} else {
(parts[0].to_owned(), None)
};
let resolved_self;
let mut path_str;
let (res, fragment) = {
let mut kind = None;
let mut path_str = if let Some(prefix) =
path_str = if let Some(prefix) =
["struct@", "enum@", "type@", "trait@", "union@"]
.iter()
.find(|p| link.starts_with(**p))
Expand Down Expand Up @@ -614,7 +621,6 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
let base_node =
if item.is_mod() && item.attrs.inner_docs { None } else { parent_node };

let resolved_self;
// replace `Self` with suitable item's parent name
if path_str.starts_with("Self::") {
if let Some(ref name) = parent_name {
Expand Down Expand Up @@ -760,6 +766,24 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
if let Res::PrimTy(_) = res {
item.attrs.links.push((ori_link, None, fragment));
} else {
// ~~WRONG: TODO: I think this happens too late and we need to instead put this in `self.resolve`~~
debug!("item {:?} resolved to {:?}", item, res);
if let Some(local) = res.opt_def_id().and_then(|def_id| def_id.as_local()) {
let hir_id = self.cx.tcx.hir().as_local_hir_id(local);
if !self.cx.tcx.privacy_access_levels(rustc_hir::def_id::LOCAL_CRATE).is_exported(hir_id) {
let item_name = item.name.as_deref().unwrap_or("<unknown>");
build_diagnostic(
cx,
&item,
path_str,
&dox,
link_range,
&format!("public documentation for `{}` links to a private item", item_name),
"this item is private",
None,
);
}
}
let id = register_res(cx, res);
item.attrs.links.push((ori_link, Some(id), fragment));
}
Expand Down

0 comments on commit 6f8bec9

Please sign in to comment.