Skip to content

Commit 0959d5a

Browse files
authored
Rollup merge of rust-lang#110765 - wackbyte:fix-defaultness-position, r=fmease,GuillaumeGomez
rustdoc: fix position of `default` in method rendering With the following code: ```rs #![feature(specialization)] pub trait A { unsafe fn a(); } impl A for () { default unsafe fn a() {} } ``` rustdoc would render the `impl` of `a` as ```rs unsafe default fn a() ``` which is inconsistent with the actual position of `default`. This PR fixes this issue.
2 parents b14fd23 + 13f58a8 commit 0959d5a

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/librustdoc/html/render/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ fn assoc_method(
820820
let header = meth.fn_header(tcx).expect("Trying to get header from a non-function item");
821821
let name = meth.name.as_ref().unwrap();
822822
let vis = visibility_print_with_space(meth.visibility(tcx), meth.item_id, cx).to_string();
823+
let defaultness = print_default_space(meth.is_default());
823824
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
824825
// this condition.
825826
let constness = match render_mode {
@@ -830,18 +831,17 @@ fn assoc_method(
830831
};
831832
let asyncness = header.asyncness.print_with_space();
832833
let unsafety = header.unsafety.print_with_space();
833-
let defaultness = print_default_space(meth.is_default());
834834
let abi = print_abi_with_space(header.abi).to_string();
835835
let href = assoc_href_attr(meth, link, cx);
836836

837837
// NOTE: `{:#}` does not print HTML formatting, `{}` does. So `g.print` can't be reused between the length calculation and `write!`.
838838
let generics_len = format!("{:#}", g.print(cx)).len();
839839
let mut header_len = "fn ".len()
840840
+ vis.len()
841+
+ defaultness.len()
841842
+ constness.len()
842843
+ asyncness.len()
843844
+ unsafety.len()
844-
+ defaultness.len()
845845
+ abi.len()
846846
+ name.as_str().len()
847847
+ generics_len;
@@ -860,14 +860,14 @@ fn assoc_method(
860860
w.reserve(header_len + "<a href=\"\" class=\"fn\">{".len() + "</a>".len());
861861
write!(
862862
w,
863-
"{indent}{vis}{constness}{asyncness}{unsafety}{defaultness}{abi}fn \
863+
"{indent}{vis}{defaultness}{constness}{asyncness}{unsafety}{abi}fn \
864864
<a{href} class=\"fn\">{name}</a>{generics}{decl}{notable_traits}{where_clause}",
865865
indent = indent_str,
866866
vis = vis,
867+
defaultness = defaultness,
867868
constness = constness,
868869
asyncness = asyncness,
869870
unsafety = unsafety,
870-
defaultness = defaultness,
871871
abi = abi,
872872
href = href,
873873
name = name,

tests/rustdoc/default-trait-method.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
11
#![feature(min_specialization)]
22

33
// @has default_trait_method/trait.Item.html
4-
// @has - '//*[@id="tymethod.foo"]' 'fn foo()'
5-
// @!has - '//*[@id="tymethod.foo"]' 'default fn foo()'
6-
// @has - '//*[@id="tymethod.bar"]' 'fn bar()'
7-
// @!has - '//*[@id="tymethod.bar"]' 'default fn bar()'
8-
// @has - '//*[@id="method.baz"]' 'fn baz()'
9-
// @!has - '//*[@id="method.baz"]' 'default fn baz()'
104
pub trait Item {
5+
// @has - '//*[@id="tymethod.foo"]' 'fn foo()'
6+
// @!has - '//*[@id="tymethod.foo"]' 'default fn foo()'
117
fn foo();
8+
9+
// @has - '//*[@id="tymethod.bar"]' 'fn bar()'
10+
// @!has - '//*[@id="tymethod.bar"]' 'default fn bar()'
1211
fn bar();
13-
fn baz() {}
12+
13+
// @has - '//*[@id="tymethod.baz"]' 'unsafe fn baz()'
14+
// @!has - '//*[@id="tymethod.baz"]' 'default unsafe fn baz()'
15+
unsafe fn baz();
16+
17+
// @has - '//*[@id="tymethod.quux"]' 'unsafe fn quux()'
18+
// @!has - '//*[@id="tymethod.quux"]' 'default unsafe fn quux()'
19+
unsafe fn quux();
20+
21+
// @has - '//*[@id="method.xyzzy"]' 'fn xyzzy()'
22+
// @!has - '//*[@id="method.xyzzy"]' 'default fn xyzzy()'
23+
fn xyzzy() {}
1424
}
1525

1626
// @has default_trait_method/struct.Foo.html
17-
// @has - '//*[@id="method.foo"]' 'default fn foo()'
18-
// @has - '//*[@id="method.bar"]' 'fn bar()'
19-
// @!has - '//*[@id="method.bar"]' 'default fn bar()'
20-
// @has - '//*[@id="method.baz"]' 'fn baz()'
21-
// @!has - '//*[@id="method.baz"]' 'default fn baz()'
2227
pub struct Foo;
2328
impl Item for Foo {
29+
// @has - '//*[@id="method.foo"]' 'default fn foo()'
2430
default fn foo() {}
31+
32+
// @has - '//*[@id="method.bar"]' 'fn bar()'
33+
// @!has - '//*[@id="method.bar"]' 'default fn bar()'
2534
fn bar() {}
35+
36+
// @has - '//*[@id="method.baz"]' 'default unsafe fn baz()'
37+
default unsafe fn baz() {}
38+
39+
// @has - '//*[@id="method.quux"]' 'unsafe fn quux()'
40+
// @!has - '//*[@id="method.quux"]' 'default unsafe fn quux()'
41+
unsafe fn quux() {}
42+
43+
// @has - '//*[@id="method.xyzzy"]' 'fn xyzzy()'
44+
// @!has - '//*[@id="method.xyzzy"]' 'default fn xyzzy()'
2645
}

0 commit comments

Comments
 (0)