Skip to content

Commit 3023cc4

Browse files
authored
Rollup merge of #42594 - ollie27:rustdoc_assoc_type_links, r=steveklabnik
rustdoc: Link directly to associated types Rather than just linking to the trait. Also simplifies the logic used to decide whether to render the full QPath.
2 parents 8742619 + 429dc51 commit 3023cc4

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/librustdoc/html/format.rs

+19-23
Original file line numberDiff line numberDiff line change
@@ -459,22 +459,10 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
459459
/// rendering function with the necessary arguments for linking to a local path.
460460
fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
461461
print_all: bool, use_absolute: bool) -> fmt::Result {
462-
let empty = clean::PathSegment {
463-
name: String::new(),
464-
params: clean::PathParameters::Parenthesized {
465-
inputs: Vec::new(),
466-
output: None,
467-
}
468-
};
469-
let last = path.segments.last()
470-
.unwrap_or(&empty);
471-
let rel_root = if path.segments.is_empty() {
472-
None
473-
} else {
474-
match &*path.segments[0].name {
475-
"self" => Some("./".to_string()),
476-
_ => None,
477-
}
462+
let last = path.segments.last().unwrap();
463+
let rel_root = match &*path.segments[0].name {
464+
"self" => Some("./".to_string()),
465+
_ => None,
478466
};
479467

480468
if print_all {
@@ -508,7 +496,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
508496
Some((_, _, fqp)) => {
509497
format!("{}::{}",
510498
fqp[..fqp.len() - 1].join("::"),
511-
HRef::new(did, fqp.last().unwrap_or(&String::new())))
499+
HRef::new(did, fqp.last().unwrap()))
512500
}
513501
None => format!("{}", HRef::new(did, &last.name)),
514502
}
@@ -740,10 +728,8 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
740728
}
741729
clean::QPath { ref name, ref self_type, ref trait_ } => {
742730
let should_show_cast = match *trait_ {
743-
box clean::ResolvedPath { .. } => {
744-
let path = clean::Path::singleton(name.clone());
745-
!path.segments.is_empty() && &format!("{:#}", trait_) != "()" &&
746-
&format!("{:#}", self_type) != "Self"
731+
box clean::ResolvedPath { ref path, .. } => {
732+
!path.segments.is_empty() && !self_type.is_self_type()
747733
}
748734
_ => true,
749735
};
@@ -772,8 +758,18 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
772758
// everything comes in as a fully resolved QPath (hard to
773759
// look at).
774760
box clean::ResolvedPath { did, ref typarams, .. } => {
775-
let path = clean::Path::singleton(name.clone());
776-
resolved_path(f, did, &path, true, use_absolute)?;
761+
match href(did) {
762+
Some((ref url, _, ref path)) if !f.alternate() => {
763+
write!(f,
764+
"<a class=\"type\" href=\"{url}#{shortty}.{name}\" \
765+
title=\"type {path}::{name}\">{name}</a>",
766+
url = url,
767+
shortty = ItemType::AssociatedType,
768+
name = name,
769+
path = path.join("::"))?;
770+
}
771+
_ => write!(f, "{}", name)?,
772+
}
777773

778774
// FIXME: `typarams` are not rendered, and this seems bad?
779775
drop(typarams);

src/test/rustdoc/assoc-types.rs

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// ignore-tidy-linelength
12+
1113
#![crate_type="lib"]
1214

1315
// @has assoc_types/trait.Index.html
@@ -18,11 +20,14 @@ pub trait Index<I: ?Sized> {
1820
// @has - '//*[@id="index.v"]//code' 'fn index'
1921
// @has - '//*[@id="tymethod.index"]//code' \
2022
// "fn index<'a>(&'a self, index: I) -> &'a Self::Output"
23+
// @has - '//*[@id="tymethod.index"]//code//a[@href="../assoc_types/trait.Index.html#associatedtype.Output"]' \
24+
// "Output"
2125
fn index<'a>(&'a self, index: I) -> &'a Self::Output;
2226
}
2327

2428
// @has assoc_types/fn.use_output.html
2529
// @has - '//*[@class="rust fn"]' '-> &T::Output'
30+
// @has - '//*[@class="rust fn"]//a[@href="../assoc_types/trait.Index.html#associatedtype.Output"]' 'Output'
2631
pub fn use_output<T: Index<usize>>(obj: &T, index: usize) -> &T::Output {
2732
obj.index(index)
2833
}
@@ -33,10 +38,12 @@ pub trait Feed {
3338

3439
// @has assoc_types/fn.use_input.html
3540
// @has - '//*[@class="rust fn"]' 'T::Input'
41+
// @has - '//*[@class="rust fn"]//a[@href="../assoc_types/trait.Feed.html#associatedtype.Input"]' 'Input'
3642
pub fn use_input<T: Feed>(_feed: &T, _element: T::Input) { }
3743

3844
// @has assoc_types/fn.cmp_input.html
3945
// @has - '//*[@class="rust fn"]' 'where T::Input: PartialEq<U::Input>'
46+
// @has - '//*[@class="rust fn"]//a[@href="../assoc_types/trait.Feed.html#associatedtype.Input"]' 'Input'
4047
pub fn cmp_input<T: Feed, U: Feed>(a: &T::Input, b: &U::Input) -> bool
4148
where T::Input: PartialEq<U::Input>
4249
{

0 commit comments

Comments
 (0)