Skip to content

Commit

Permalink
Rollup merge of rust-lang#102525 - notriddle:notriddle/array-link, r=…
Browse files Browse the repository at this point in the history
…GuillaumeGomez,jsha

rustdoc: remove orphaned link on array bracket

This is rust-lang#98069, but for arrays instead.

For non-generics, this retains links to the array page, but instead of trying to link it all, it only links the length part, which distinguishes arrays from slices.

For generics, the entire thing becomes a link, just like slices.

| Type | Before | After |
|--|--|--|
| u32 | <code>pub fn alpha() -&gt; &amp;'static <a class="primitive" href="http://doc.rust-lang.org/nightly/core/primitive.array.html">[</a><a class="primitive" href="http://doc.rust-lang.org/nightly/core/primitive.u32.html">u32</a><a class="primitive" href="http://doc.rust-lang.org/nightly/core/primitive.array.html">; 1]</a></code> | <code>pub fn alpha() -&gt; &amp;'static [<a class="primitive" href="http://doc.rust-lang.org/nightly/core/primitive.u32.html">u32</a>; <a class="primitive" href="http://doc.rust-lang.org/nightly/core/primitive.array.html">1</a>]</code>
| generic | <code>pub fn beta&lt;T&gt;() -&gt; &amp;'static <a class="primitive" href="http://doc.rust-lang.org/nightly/core/primitive.array.html">[</a>T<a class="primitive" href="http://doc.rust-lang.org/nightly/core/primitive.array.html">; 1]</a></code> | <code>pub fn beta&lt;T&gt;() -&gt; &amp;'static <a class="primitive" href="http://doc.rust-lang.org/nightly/core/primitive.array.html">[T; 1]</a></code>
  • Loading branch information
matthiaskrgr authored Oct 2, 2022
2 parents baba839 + 598a02c commit 3d71ff4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,15 +1010,25 @@ fn fmt_type<'cx>(
write!(f, "]")
}
},
clean::Array(ref t, ref n) => {
primitive_link(f, PrimitiveType::Array, "[", cx)?;
fmt::Display::fmt(&t.print(cx), f)?;
if f.alternate() {
primitive_link(f, PrimitiveType::Array, &format!("; {}]", n), cx)
} else {
primitive_link(f, PrimitiveType::Array, &format!("; {}]", Escape(n)), cx)
clean::Array(ref t, ref n) => match **t {
clean::Generic(name) if !f.alternate() => primitive_link(
f,
PrimitiveType::Array,
&format!("[{name}; {n}]", n = Escape(n)),
cx,
),
_ => {
write!(f, "[")?;
fmt::Display::fmt(&t.print(cx), f)?;
if f.alternate() {
write!(f, "; {n}")?;
} else {
write!(f, "; ")?;
primitive_link(f, PrimitiveType::Array, &format!("{n}", n = Escape(n)), cx)?;
}
write!(f, "]")
}
}
},
clean::RawPointer(m, ref t) => {
let m = match m {
hir::Mutability::Mut => "mut",
Expand Down
1 change: 1 addition & 0 deletions src/test/rustdoc/array-links.link_box_generic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<code>pub fn delta&lt;T&gt;() -&gt; <a class="struct" href="struct.MyBox.html" title="struct foo::MyBox">MyBox</a>&lt;<a class="primitive" href="{{channel}}/core/primitive.array.html">[T; 1]</a>&gt;</code>
1 change: 1 addition & 0 deletions src/test/rustdoc/array-links.link_box_u32.html
Original file line number Diff line number Diff line change
@@ -0,0 +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.u32.html">u32</a>; <a class="primitive" href="{{channel}}/core/primitive.array.html">1</a>]&gt;</code>
1 change: 1 addition & 0 deletions src/test/rustdoc/array-links.link_slice_generic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<code>pub fn beta&lt;T&gt;() -&gt; &amp;'static <a class="primitive" href="{{channel}}/core/primitive.array.html">[T; 1]</a></code>
1 change: 1 addition & 0 deletions src/test/rustdoc/array-links.link_slice_u32.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<code>pub fn alpha() -&gt; &amp;'static [<a class="primitive" href="{{channel}}/core/primitive.u32.html">u32</a>; <a class="primitive" href="{{channel}}/core/primitive.array.html">1</a>]</code>
28 changes: 28 additions & 0 deletions src/test/rustdoc/array-links.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![crate_name = "foo"]
#![no_std]

pub struct MyBox<T: ?Sized>(*const T);

// @has 'foo/fn.alpha.html'
// @snapshot link_slice_u32 - '//pre[@class="rust fn"]/code'
pub fn alpha() -> &'static [u32; 1] {
loop {}
}

// @has 'foo/fn.beta.html'
// @snapshot link_slice_generic - '//pre[@class="rust fn"]/code'
pub fn beta<T>() -> &'static [T; 1] {
loop {}
}

// @has 'foo/fn.gamma.html'
// @snapshot link_box_u32 - '//pre[@class="rust fn"]/code'
pub fn gamma() -> MyBox<[u32; 1]> {
loop {}
}

// @has 'foo/fn.delta.html'
// @snapshot link_box_generic - '//pre[@class="rust fn"]/code'
pub fn delta<T>() -> MyBox<[T; 1]> {
loop {}
}

0 comments on commit 3d71ff4

Please sign in to comment.