Skip to content

Commit

Permalink
rustdoc: remove link on slice brackets
Browse files Browse the repository at this point in the history
Since #97668 was merged, the slice::get function now looks like this:

![image](https://user-images.githubusercontent.com/1593513/173430685-1dd2b275-2439-4392-b7d4-96bcb355a377.png)

That whole thing, `[T]`, is a single link to `primitive.slice.html`. This
definitely fixes it for this case, but it's not obvious what we should do for
slices of concrete types:

![image](https://user-images.githubusercontent.com/1593513/173430968-7eed1aec-b688-4f84-a492-9210aff0037a.png)

There are actually three links in that `[u8]`: the opening brace `[` is a
link to `primitive.slice.html`, the `u8` is a link to `primitive.u8.html`,
and the final `]` is a link to `primitive.slice.html`. This is a serious
[usability bug](https://usability.yale.edu/web-accessibility/articles/links):
the square braces are much too small for anyone who doesn't have perfect
motor control using mouse or touch, provide an excessive number of tab stops
for anyone using keyboard, and no visual indication whatsoever that they're
separate links.

Now that slices of generic types are linked, it seems reasonable to err on
the side of less clutter and stop linking concrete slices to the slice page.
  • Loading branch information
notriddle committed Jun 13, 2022
1 parent 083721a commit 682889f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 30 deletions.
29 changes: 2 additions & 27 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,9 +886,9 @@ fn fmt_type<'cx>(
primitive_link(f, PrimitiveType::Slice, &format!("[{name}]"), cx)
}
_ => {
primitive_link(f, PrimitiveType::Slice, "[", cx)?;
write!(f, "[")?;
fmt::Display::fmt(&t.print(cx), f)?;
primitive_link(f, PrimitiveType::Slice, "]", cx)
write!(f, "]")
}
},
clean::Array(ref t, ref n) => {
Expand Down Expand Up @@ -926,31 +926,6 @@ fn fmt_type<'cx>(
let m = mutability.print_with_space();
let amp = if f.alternate() { "&".to_string() } else { "&amp;".to_string() };
match **ty {
clean::Slice(ref bt) => {
// `BorrowedRef{ ... Slice(T) }` is `&[T]`
match **bt {
clean::Generic(name) => primitive_link(
f,
PrimitiveType::Slice,
&format!("{amp}{lt}{m}[{name}]"),
cx,
),
_ => {
primitive_link(
f,
PrimitiveType::Slice,
&format!("{}{}{}[", amp, lt, m),
cx,
)?;
if f.alternate() {
write!(f, "{:#}", bt.print(cx))?;
} else {
write!(f, "{}", bt.print(cx))?;
}
primitive_link(f, PrimitiveType::Slice, "]", cx)
}
}
}
clean::DynTrait(ref bounds, ref trait_lt)
if bounds.len() > 1 || trait_lt.is_some() =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc/slice-links.link_box_u32.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<code>pub fn gamma() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;<a class="primitive" href="{{channel}}/core/primitive.slice.html">[</a><a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a><a class="primitive" href="{{channel}}/core/primitive.slice.html">]</a>&gt;</code>
<code>pub fn gamma() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;[<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>]&gt;</code>
2 changes: 1 addition & 1 deletion src/test/rustdoc/slice-links.link_slice_generic.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<code>pub fn beta&lt;T&gt;() -&gt; <a class="primitive" href="{{channel}}/core/primitive.slice.html">&amp;'static [T]</a></code>
<code>pub fn beta&lt;T&gt;() -&gt; &amp;'static <a class="primitive" href="{{channel}}/core/primitive.slice.html">[T]</a></code>
2 changes: 1 addition & 1 deletion src/test/rustdoc/slice-links.link_slice_u32.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<code>pub fn alpha() -&gt; <a class="primitive" href="{{channel}}/core/primitive.slice.html">&amp;'static [</a><a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a><a class="primitive" href="{{channel}}/core/primitive.slice.html">]</a></code>
<code>pub fn alpha() -&gt; &amp;'static [<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>]</code>

0 comments on commit 682889f

Please sign in to comment.