Skip to content

Commit

Permalink
Fix private intra-doc warnings on associated items
Browse files Browse the repository at this point in the history
The issue was that the `kind, id` override was previously only being
considered for the disambiguator check, not the privacy check. This uses
the same ID for both.
  • Loading branch information
jyn514 committed Feb 12, 2021
1 parent 178108b commit 67fb96c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,11 +1151,12 @@ impl LinkCollector<'_, '_> {
};

let verify = |kind: DefKind, id: DefId| {
debug!("intra-doc link to {} resolved to {:?}", path_str, res);
let (kind, id) = self.kind_side_channel.take().unwrap_or((kind, id));
debug!("intra-doc link to {} resolved to {:?} (id: {:?})", path_str, res, id);

// Disallow e.g. linking to enums with `struct@`
debug!("saw kind {:?} with disambiguator {:?}", kind, disambiguator);
match (self.kind_side_channel.take().map(|(kind, _)| kind).unwrap_or(kind), disambiguator) {
match (kind, disambiguator) {
| (DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst, Some(Disambiguator::Kind(DefKind::Const)))
// NOTE: this allows 'method' to mean both normal functions and associated functions
// This can't cause ambiguity because both are in the same namespace.
Expand Down Expand Up @@ -1190,7 +1191,7 @@ impl LinkCollector<'_, '_> {
}
}

Some((kind, id))
Some(())
};

match res {
Expand Down Expand Up @@ -1241,7 +1242,7 @@ impl LinkCollector<'_, '_> {
Some(ItemLink { link: ori_link.link, link_text, did: None, fragment })
}
Res::Def(kind, id) => {
let (kind, id) = verify(kind, id)?;
verify(kind, id)?;
let id = clean::register_res(cx, rustc_hir::def::Res::Def(kind, id));
Some(ItemLink { link: ori_link.link, link_text, did: Some(id), fragment })
}
Expand Down
12 changes: 10 additions & 2 deletions src/test/rustdoc-ui/intra-doc/private.private.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
warning: public documentation for `DocMe` links to private item `DontDocMe`
--> $DIR/private.rs:5:11
|
LL | /// docs [DontDocMe]
LL | /// docs [DontDocMe] [DontDocMe::f]
| ^^^^^^^^^ this item is private
|
= note: `#[warn(private_intra_doc_links)]` on by default
= note: this link resolves only because you passed `--document-private-items`, but will break without

warning: 1 warning emitted
warning: public documentation for `DocMe` links to private item `DontDocMe::f`
--> $DIR/private.rs:5:23
|
LL | /// docs [DontDocMe] [DontDocMe::f]
| ^^^^^^^^^^^^ this item is private
|
= note: this link resolves only because you passed `--document-private-items`, but will break without

warning: 2 warnings emitted

12 changes: 10 additions & 2 deletions src/test/rustdoc-ui/intra-doc/private.public.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
warning: public documentation for `DocMe` links to private item `DontDocMe`
--> $DIR/private.rs:5:11
|
LL | /// docs [DontDocMe]
LL | /// docs [DontDocMe] [DontDocMe::f]
| ^^^^^^^^^ this item is private
|
= note: `#[warn(private_intra_doc_links)]` on by default
= note: this link will resolve properly if you pass `--document-private-items`

warning: 1 warning emitted
warning: public documentation for `DocMe` links to private item `DontDocMe::f`
--> $DIR/private.rs:5:23
|
LL | /// docs [DontDocMe] [DontDocMe::f]
| ^^^^^^^^^^^^ this item is private
|
= note: this link will resolve properly if you pass `--document-private-items`

warning: 2 warnings emitted

7 changes: 6 additions & 1 deletion src/test/rustdoc-ui/intra-doc/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
// revisions: public private
// [private]compile-flags: --document-private-items

/// docs [DontDocMe]
/// docs [DontDocMe] [DontDocMe::f]
//~^ WARNING public documentation for `DocMe` links to private item `DontDocMe`
//~| WARNING public documentation for `DocMe` links to private item `DontDocMe::f`
// FIXME: for [private] we should also make sure the link was actually generated
pub struct DocMe;
struct DontDocMe;

impl DontDocMe {
fn f() {}
}

0 comments on commit 67fb96c

Please sign in to comment.