Skip to content

Commit 4e858ce

Browse files
committed
Correct anchor for links to associated trait items
1 parent 98f0a91 commit 4e858ce

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/librustdoc/html/render.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2085,7 +2085,13 @@ fn render_assoc_item(w: &mut fmt::Formatter, meth: &clean::Item,
20852085
let href = match link {
20862086
AssocItemLink::Anchor => anchor,
20872087
AssocItemLink::GotoSource(did) => {
2088-
href(did).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor)
2088+
// We're creating a link from an impl-item to the corresponding
2089+
// trait-item and need to map the anchored type accordingly.
2090+
let shortty = match shortty(it) {
2091+
ItemType::Method => ItemType::TyMethod,
2092+
s@_ => s,
2093+
};
2094+
href(did).map(|p| format!("{}#{}.{}", p.0, shortty, name)).unwrap_or(anchor)
20892095
}
20902096
};
20912097
let vis_constness = match get_unstable_features_setting() {

src/test/rustdoc/issue-17476.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ extern crate issue_17476;
1616
pub struct Foo;
1717

1818
// @has issue_17476/struct.Foo.html \
19-
// '//*[@href="http://example.com/issue_17476/trait.Foo.html#method.foo"]' \
19+
// '//*[@href="http://example.com/issue_17476/trait.Foo.html#tymethod.foo"]' \
2020
// 'foo'
2121
impl issue_17476::Foo for Foo {}

src/test/rustdoc/issue-28478.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// @has issue_28478/trait.Bar.html
12+
pub trait Bar {
13+
fn bar();
14+
}
15+
16+
// @has issue_28478/struct.Foo.html
17+
pub struct Foo;
18+
19+
impl Foo {
20+
// @has - '//*[@href="#method.foo"]' 'foo'
21+
pub fn foo() {}
22+
}
23+
24+
impl Bar for Foo {
25+
// @has - '//*[@href="../issue_28478/trait.Bar.html#tymethod.bar"]' 'bar'
26+
fn bar() {}
27+
}

0 commit comments

Comments
 (0)