Skip to content

Commit

Permalink
Use the proper kind for associated items
Browse files Browse the repository at this point in the history
See comments in the diff; this is such a hack.

The reason this can't be done properly in `register_res` is because
there's no way to get back the parent type: calling
`tcx.parent(assoc_item)` gets you the _impl_, not the type.
You can call `tcx.impl_trait_ref(impl_).self_ty()`, but there's no way
to go from that to a DefId without unwrapping.
  • Loading branch information
jyn514 committed Aug 6, 2020
1 parent f05e9da commit 0c99d80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
10 changes: 8 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Expand Up @@ -17,6 +17,7 @@ use rustc_span::symbol::Ident;
use rustc_span::symbol::Symbol;
use rustc_span::DUMMY_SP;

use std::cell::Cell;
use std::ops::Range;

use crate::clean::*;
Expand Down Expand Up @@ -62,11 +63,12 @@ struct LinkCollector<'a, 'tcx> {
cx: &'a DocContext<'tcx>,
// NOTE: this may not necessarily be a module in the current crate
mod_ids: Vec<DefId>,
kind_side_channel: Cell<Option<DefKind>>,
}

impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
fn new(cx: &'a DocContext<'tcx>) -> Self {
LinkCollector { cx, mod_ids: Vec::new() }
LinkCollector { cx, mod_ids: Vec::new(), kind_side_channel: Cell::new(None) }
}

fn variant_field(
Expand Down Expand Up @@ -347,6 +349,10 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
AnchorFailure::AssocConstant
}))
} else {
// HACK(jynelson): `clean` expects the type, not the associated item.
// but the disambiguator logic expects the associated item.
// Store the kind in a side channel so that only the disambiguator logic looks at it.
self.kind_side_channel.replace(Some(item.kind.as_def_kind()));
Ok((ty_res, Some(format!("{}.{}", out, item_name))))
}
} else {
Expand Down Expand Up @@ -763,7 +769,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
debug!("saw kind {:?} with disambiguator {:?}", kind, disambiguator);
// NOTE: this relies on the fact that `''` is never parsed as a disambiguator
// NOTE: this needs to be kept in sync with the disambiguator parsing
match (kind, disambiguator) {
match (self.kind_side_channel.take().unwrap_or(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
6 changes: 6 additions & 0 deletions src/test/rustdoc-ui/intra-links-disambiguator-mismatch.rs
Expand Up @@ -2,6 +2,10 @@
//~^ NOTE lint level is defined
pub enum S {}

impl S {
fn assoc_fn() {}
}

macro_rules! m {
() => {};
}
Expand Down Expand Up @@ -65,4 +69,6 @@ trait T {}
//~^ ERROR incompatible link kind for `f`
//~| NOTE this link resolved
//~| HELP use its disambiguator

/// Link to [S::assoc_fn()]
pub fn f() {}
22 changes: 11 additions & 11 deletions src/test/rustdoc-ui/intra-links-disambiguator-mismatch.stderr
@@ -1,5 +1,5 @@
error: incompatible link kind for `S`
--> $DIR/intra-links-disambiguator-mismatch.rs:14:14
--> $DIR/intra-links-disambiguator-mismatch.rs:18:14
|
LL | /// Link to [struct@S]
| ^^^^^^^^ help: to link to the enum, use its disambiguator: `enum@S`
Expand All @@ -12,79 +12,79 @@ LL | #![deny(broken_intra_doc_links)]
= note: this link resolved to an enum, which is not a struct

error: incompatible link kind for `S`
--> $DIR/intra-links-disambiguator-mismatch.rs:19:14
--> $DIR/intra-links-disambiguator-mismatch.rs:23:14
|
LL | /// Link to [mod@S]
| ^^^^^ help: to link to the enum, use its disambiguator: `enum@S`
|
= note: this link resolved to an enum, which is not a module

error: incompatible link kind for `S`
--> $DIR/intra-links-disambiguator-mismatch.rs:24:14
--> $DIR/intra-links-disambiguator-mismatch.rs:28:14
|
LL | /// Link to [union@S]
| ^^^^^^^ help: to link to the enum, use its disambiguator: `enum@S`
|
= note: this link resolved to an enum, which is not a union

error: incompatible link kind for `S`
--> $DIR/intra-links-disambiguator-mismatch.rs:29:14
--> $DIR/intra-links-disambiguator-mismatch.rs:33:14
|
LL | /// Link to [trait@S]
| ^^^^^^^ help: to link to the enum, use its disambiguator: `enum@S`
|
= note: this link resolved to an enum, which is not a trait

error: incompatible link kind for `T`
--> $DIR/intra-links-disambiguator-mismatch.rs:34:14
--> $DIR/intra-links-disambiguator-mismatch.rs:38:14
|
LL | /// Link to [struct@T]
| ^^^^^^^^ help: to link to the trait, use its disambiguator: `trait@T`
|
= note: this link resolved to a trait, which is not a struct

error: incompatible link kind for `m`
--> $DIR/intra-links-disambiguator-mismatch.rs:39:14
--> $DIR/intra-links-disambiguator-mismatch.rs:43:14
|
LL | /// Link to [derive@m]
| ^^^^^^^^ help: to link to the macro, use its disambiguator: `m!`
|
= note: this link resolved to a macro, which is not a derive macro

error: incompatible link kind for `s`
--> $DIR/intra-links-disambiguator-mismatch.rs:44:14
--> $DIR/intra-links-disambiguator-mismatch.rs:48:14
|
LL | /// Link to [const@s]
| ^^^^^^^ help: to link to the static, use its disambiguator: `static@s`
|
= note: this link resolved to a static, which is not a constant

error: incompatible link kind for `c`
--> $DIR/intra-links-disambiguator-mismatch.rs:49:14
--> $DIR/intra-links-disambiguator-mismatch.rs:53:14
|
LL | /// Link to [static@c]
| ^^^^^^^^ help: to link to the constant, use its disambiguator: `const@c`
|
= note: this link resolved to a constant, which is not a static

error: incompatible link kind for `c`
--> $DIR/intra-links-disambiguator-mismatch.rs:54:14
--> $DIR/intra-links-disambiguator-mismatch.rs:58:14
|
LL | /// Link to [fn@c]
| ^^^^ help: to link to the constant, use its disambiguator: `const@c`
|
= note: this link resolved to a constant, which is not a function

error: incompatible link kind for `c`
--> $DIR/intra-links-disambiguator-mismatch.rs:59:14
--> $DIR/intra-links-disambiguator-mismatch.rs:63:14
|
LL | /// Link to [c()]
| ^^^ help: to link to the constant, use its disambiguator: `const@c`
|
= note: this link resolved to a constant, which is not a function

error: incompatible link kind for `f`
--> $DIR/intra-links-disambiguator-mismatch.rs:64:14
--> $DIR/intra-links-disambiguator-mismatch.rs:68:14
|
LL | /// Link to [const@f]
| ^^^^^^^ help: to link to the function, use its disambiguator: `f()`
Expand Down

0 comments on commit 0c99d80

Please sign in to comment.