Skip to content

Commit 3174a69

Browse files
authored
Rollup merge of #98105 - notriddle:notriddle/tuple-links, r=jsha
rustdoc: remove tuple link on round braces This is #98069 but for tuples. The reasoning is the same: * This PR also changes it so that tuples with all-generic elements still link to the primitive.tuple.html page, just like slices. So there still plenty of on-ramps for anybody who doesn't know about it. * It's too hard to see when round braces are a separate link from the type inside of them. * It's too hard to click even if you do notice them. Before: * impl [ToSocketAddrs](https://doc.rust-lang.org/nightly/std/net/trait.ToSocketAddrs.html) for [(](https://doc.rust-lang.org/nightly/std/primitive.tuple.html)[IpAddr](https://doc.rust-lang.org/nightly/std/net/enum.IpAddr.html), [u16](https://doc.rust-lang.org/nightly/std/primitive.u16.html)[)](https://doc.rust-lang.org/nightly/std/primitive.tuple.html) * impl<K, V> [FromIterator](https://notriddle.com/notriddle-rustdoc-test/std/iter/trait.FromIterator.html)<[(](https://notriddle.com/notriddle-rustdoc-test/std/primitive.tuple.html)K, V[)](https://notriddle.com/notriddle-rustdoc-test/std/primitive.tuple.html)> for [BTreeMap](https://notriddle.com/notriddle-rustdoc-test/std/collections/struct.BTreeMap.html)<K, V> After: * impl [ToSocketAddrs](https://doc.rust-lang.org/nightly/std/net/trait.ToSocketAddrs.html) for ([IpAddr](https://doc.rust-lang.org/nightly/std/net/enum.IpAddr.html), [u16](https://doc.rust-lang.org/nightly/std/primitive.u16.html)) * impl<K, V> [FromIterator](https://notriddle.com/notriddle-rustdoc-test/std/iter/trait.FromIterator.html)<[(K, V)](https://notriddle.com/notriddle-rustdoc-test/std/primitive.tuple.html)> for [BTreeMap](https://notriddle.com/notriddle-rustdoc-test/std/collections/struct.BTreeMap.html)<K, V>
2 parents 15fc228 + 29a9f36 commit 3174a69

9 files changed

+53
-13
lines changed

src/etc/htmldocck.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def check_snapshot(snapshot_name, actual_tree, normalize_to_text):
417417
snapshot_path = '{}.{}.{}'.format(rust_test_path[:-3], snapshot_name, 'html')
418418
try:
419419
with open(snapshot_path, 'r') as snapshot_file:
420-
expected_str = snapshot_file.read()
420+
expected_str = snapshot_file.read().replace("{{channel}}", channel)
421421
except FileNotFoundError:
422422
if bless:
423423
expected_str = None
@@ -429,8 +429,6 @@ def check_snapshot(snapshot_name, actual_tree, normalize_to_text):
429429
else:
430430
actual_str = flatten(actual_tree)
431431

432-
expected_str = expected_str.replace("{{channel}}", channel)
433-
434432
# Conditions:
435433
# 1. Is --bless
436434
# 2. Are actual and expected tree different

src/librustdoc/html/format.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use rustc_span::symbol::kw;
2323
use rustc_span::{sym, Symbol};
2424
use rustc_target::spec::abi::Abi;
2525

26+
use itertools::Itertools;
27+
2628
use crate::clean::{
2729
self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId,
2830
PrimitiveType,
@@ -874,20 +876,42 @@ fn fmt_type<'cx>(
874876
match &typs[..] {
875877
&[] => primitive_link(f, PrimitiveType::Unit, "()", cx),
876878
&[ref one] => {
877-
primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
878-
// Carry `f.alternate()` into this display w/o branching manually.
879-
fmt::Display::fmt(&one.print(cx), f)?;
880-
primitive_link(f, PrimitiveType::Tuple, ",)", cx)
879+
if let clean::Generic(name) = one {
880+
primitive_link(f, PrimitiveType::Tuple, &format!("({name},)"), cx)
881+
} else {
882+
write!(f, "(")?;
883+
// Carry `f.alternate()` into this display w/o branching manually.
884+
fmt::Display::fmt(&one.print(cx), f)?;
885+
write!(f, ",)")
886+
}
881887
}
882888
many => {
883-
primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
884-
for (i, item) in many.iter().enumerate() {
885-
if i != 0 {
886-
write!(f, ", ")?;
889+
let generic_names: Vec<Symbol> = many
890+
.iter()
891+
.filter_map(|t| match t {
892+
clean::Generic(name) => Some(*name),
893+
_ => None,
894+
})
895+
.collect();
896+
let is_generic = generic_names.len() == many.len();
897+
if is_generic {
898+
primitive_link(
899+
f,
900+
PrimitiveType::Tuple,
901+
&format!("({})", generic_names.iter().map(|s| s.as_str()).join(", ")),
902+
cx,
903+
)
904+
} else {
905+
write!(f, "(")?;
906+
for (i, item) in many.iter().enumerate() {
907+
if i != 0 {
908+
write!(f, ", ")?;
909+
}
910+
// Carry `f.alternate()` into this display w/o branching manually.
911+
fmt::Display::fmt(&item.print(cx), f)?;
887912
}
888-
fmt::Display::fmt(&item.print(cx), f)?;
913+
write!(f, ")")
889914
}
890-
primitive_link(f, PrimitiveType::Tuple, ")", cx)
891915
}
892916
}
893917
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<code>pub fn tuple1(x: (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>,)) -&gt; (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>,)</code>

src/test/rustdoc/tuples.link1_t.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<code>pub fn tuple1_t&lt;T&gt;(x: <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T,)</a>) -&gt; <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T,)</a></code>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<code>pub fn tuple2(x: (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>, <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>)) -&gt; (<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>, <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>)</code>

src/test/rustdoc/tuples.link2_t.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<code>pub fn tuple2_t&lt;T&gt;(x: <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, T)</a>) -&gt; <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, T)</a></code>

src/test/rustdoc/tuples.link2_tu.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<code>pub fn tuple2_tu&lt;T, U&gt;(x: <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, U)</a>) -&gt; <a class="primitive" href="{{channel}}/std/primitive.tuple.html">(T, U)</a></code>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<code>pub fn tuple0(x: <a class="primitive" href="{{channel}}/std/primitive.unit.html">()</a>)</code>

src/test/rustdoc/tuples.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
#![crate_name = "foo"]
22

33
// @has foo/fn.tuple0.html //pre 'pub fn tuple0(x: ())'
4+
// @snapshot link_unit - '//pre[@class="rust fn"]/code'
45
pub fn tuple0(x: ()) -> () { x }
56
// @has foo/fn.tuple1.html //pre 'pub fn tuple1(x: (i32,)) -> (i32,)'
7+
// @snapshot link1_i32 - '//pre[@class="rust fn"]/code'
68
pub fn tuple1(x: (i32,)) -> (i32,) { x }
79
// @has foo/fn.tuple2.html //pre 'pub fn tuple2(x: (i32, i32)) -> (i32, i32)'
10+
// @snapshot link2_i32 - '//pre[@class="rust fn"]/code'
811
pub fn tuple2(x: (i32, i32)) -> (i32, i32) { x }
12+
// @has foo/fn.tuple1_t.html //pre 'pub fn tuple1_t<T>(x: (T,)) -> (T,)'
13+
// @snapshot link1_t - '//pre[@class="rust fn"]/code'
14+
pub fn tuple1_t<T>(x: (T,)) -> (T,) { x }
15+
// @has foo/fn.tuple2_t.html //pre 'pub fn tuple2_t<T>(x: (T, T)) -> (T, T)'
16+
// @snapshot link2_t - '//pre[@class="rust fn"]/code'
17+
pub fn tuple2_t<T>(x: (T, T)) -> (T, T) { x }
18+
// @has foo/fn.tuple2_tu.html //pre 'pub fn tuple2_tu<T, U>(x: (T, U)) -> (T, U)'
19+
// @snapshot link2_tu - '//pre[@class="rust fn"]/code'
20+
pub fn tuple2_tu<T, U>(x: (T, U)) -> (T, U) { x }

0 commit comments

Comments
 (0)