Skip to content

rustdoc: Skip doc link resolution for non-exported items #107932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4236,7 +4236,10 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
{
return;
}
ResolveDocLinks::Exported if !maybe_exported.eval(self.r) => {
ResolveDocLinks::Exported
if !maybe_exported.eval(self.r)
&& !rustdoc::has_primitive_or_keyword_docs(attrs) =>
{
return;
}
ResolveDocLinks::ExportedMetadata
Expand Down
16 changes: 15 additions & 1 deletion compiler/rustc_resolve/src/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_ast as ast;
use rustc_ast::util::comments::beautify_doc_string;
use rustc_data_structures::fx::FxHashMap;
use rustc_span::def_id::DefId;
use rustc_span::symbol::{kw, Symbol};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
use std::{cmp, mem};

Expand Down Expand Up @@ -339,6 +339,20 @@ pub fn inner_docs(attrs: &[ast::Attribute]) -> bool {
attrs.iter().find(|a| a.doc_str().is_some()).map_or(true, |a| a.style == ast::AttrStyle::Inner)
}

/// Has `#[doc(primitive)]` or `#[doc(keyword)]`.
pub fn has_primitive_or_keyword_docs(attrs: &[ast::Attribute]) -> bool {
for attr in attrs {
if attr.has_name(sym::doc) && let Some(items) = attr.meta_item_list() {
for item in items {
if item.has_name(sym::primitive) || item.has_name(sym::keyword) {
return true;
}
}
}
}
false
}

/// Simplified version of the corresponding function in rustdoc.
/// If the rustdoc version returns a successful result, this function must return the same result.
/// Otherwise this function may return anything.
Expand Down
9 changes: 2 additions & 7 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,8 @@ pub(crate) fn create_config(

let crate_types =
if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };
let resolve_doc_links = if *document_private {
ResolveDocLinks::All
} else {
// Should be `ResolveDocLinks::Exported` in theory, but for some reason rustdoc
// still tries to request resolutions for links on private items.
ResolveDocLinks::All
};
let resolve_doc_links =
if *document_private { ResolveDocLinks::All } else { ResolveDocLinks::Exported };
let test = scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false);
// plays with error output here!
let sessopts = config::Options {
Expand Down
13 changes: 11 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use rustc_hir::def_id::{DefId, CRATE_DEF_ID};
use rustc_hir::Mutability;
use rustc_middle::ty::{fast_reject::TreatProjections, Ty, TyCtxt};
use rustc_middle::{bug, ty};
use rustc_resolve::rustdoc::MalformedGenerics;
use rustc_resolve::rustdoc::{prepare_to_doc_link_resolution, strip_generics_from_path};
use rustc_resolve::rustdoc::{has_primitive_or_keyword_docs, prepare_to_doc_link_resolution};
use rustc_resolve::rustdoc::{strip_generics_from_path, MalformedGenerics};
use rustc_session::lint::Lint;
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{sym, Ident, Symbol};
Expand Down Expand Up @@ -899,6 +899,15 @@ fn preprocessed_markdown_links(s: &str) -> Vec<PreprocessedMarkdownLink> {

impl LinkCollector<'_, '_> {
fn resolve_links(&mut self, item: &Item) {
if !self.cx.render_options.document_private
&& let Some(def_id) = item.item_id.as_def_id()
&& let Some(def_id) = def_id.as_local()
&& !self.cx.tcx.effective_visibilities(()).is_exported(def_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure is_exported is the correct thing here, and not is_reachable? Can you add a test case for

mod private {
  /// [core::str::FromStr]
  pub struct Bar;
}

pub fn foo() -> private::Bar {
    private::Bar
}

and make sure it behaves the same both before and after this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In both cases Bar is not documented and Bar in pub fn foo() -> Bar in the generated doc is not a link.

Rustdoc uses is_reachable in only one place when it generates json output, and I'm not sure why.

&& !has_primitive_or_keyword_docs(&item.attrs.other_attrs) {
// Skip link resolution for non-exported items.
return;
}

// We want to resolve in the lexical scope of the documentation.
// In the presence of re-exports, this is not the same as the module of the item.
// Rather than merging all documentation into one, resolve it one attribute at a time
Expand Down
13 changes: 13 additions & 0 deletions tests/rustdoc-ui/intra-doc/reachable-non-exported.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// The structure is reachable, but not exported, so rustdoc
// doesn't attempt to request doc link resolutions on it.

// check-pass

mod private {
/// [core::str::FromStr]
pub struct ReachableButNotExported;
}

pub fn foo() -> private::ReachableButNotExported {
private::ReachableButNotExported
}