Skip to content

Commit 255434d

Browse files
authored
Rollup merge of #75237 - nbdd0121:rustdoc, r=jyn514
Display elided lifetime for non-reference type in doc In edition 2018 we encourage writing `<'_>` explicitly, so rustdoc should display like such as well. Fixes #75225 ~~Somehow when I run the compiled rustdoc using `cargo +stage2 doc` on other crates, it correctly produces `<'_>`, but I couldn't get the std doc to do the same with `./x.py doc --stage 2`. Might this be related to the recent change to x.py about how the doc is built?~~
2 parents f5d2ffd + 541fbbb commit 255434d

File tree

5 files changed

+78
-18
lines changed

5 files changed

+78
-18
lines changed

Diff for: src/librustdoc/clean/mod.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -1395,10 +1395,13 @@ impl Clean<Type> for hir::Ty<'_> {
13951395
_ => None,
13961396
});
13971397
if let Some(lt) = lifetime.cloned() {
1398-
if !lt.is_elided() {
1399-
let lt_def_id = cx.tcx.hir().local_def_id(param.hir_id);
1400-
lt_substs.insert(lt_def_id.to_def_id(), lt.clean(cx));
1401-
}
1398+
let lt_def_id = cx.tcx.hir().local_def_id(param.hir_id);
1399+
let cleaned = if !lt.is_elided() {
1400+
lt.clean(cx)
1401+
} else {
1402+
self::types::Lifetime::elided()
1403+
};
1404+
lt_substs.insert(lt_def_id.to_def_id(), cleaned);
14021405
}
14031406
indices.lifetimes += 1;
14041407
}
@@ -1957,21 +1960,17 @@ impl Clean<GenericArgs> for hir::GenericArgs<'_> {
19571960
output: if output != Type::Tuple(Vec::new()) { Some(output) } else { None },
19581961
}
19591962
} else {
1960-
let elide_lifetimes = self.args.iter().all(|arg| match arg {
1961-
hir::GenericArg::Lifetime(lt) => lt.is_elided(),
1962-
_ => true,
1963-
});
19641963
GenericArgs::AngleBracketed {
19651964
args: self
19661965
.args
19671966
.iter()
1968-
.filter_map(|arg| match arg {
1969-
hir::GenericArg::Lifetime(lt) if !elide_lifetimes => {
1970-
Some(GenericArg::Lifetime(lt.clean(cx)))
1967+
.map(|arg| match arg {
1968+
hir::GenericArg::Lifetime(lt) if !lt.is_elided() => {
1969+
GenericArg::Lifetime(lt.clean(cx))
19711970
}
1972-
hir::GenericArg::Lifetime(_) => None,
1973-
hir::GenericArg::Type(ty) => Some(GenericArg::Type(ty.clean(cx))),
1974-
hir::GenericArg::Const(ct) => Some(GenericArg::Const(ct.clean(cx))),
1971+
hir::GenericArg::Lifetime(_) => GenericArg::Lifetime(Lifetime::elided()),
1972+
hir::GenericArg::Type(ty) => GenericArg::Type(ty.clean(cx)),
1973+
hir::GenericArg::Const(ct) => GenericArg::Const(ct.clean(cx)),
19751974
})
19761975
.collect(),
19771976
bindings: self.bindings.clean(cx),

Diff for: src/librustdoc/clean/types.rs

+4
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,10 @@ impl Lifetime {
750750
pub fn statik() -> Lifetime {
751751
Lifetime("'static".to_string())
752752
}
753+
754+
pub fn elided() -> Lifetime {
755+
Lifetime("'_".to_string())
756+
}
753757
}
754758

755759
#[derive(Clone, Debug)]

Diff for: src/librustdoc/clean/utils.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::clean::auto_trait::AutoTraitFinder;
22
use crate::clean::blanket_impl::BlanketImplFinder;
33
use crate::clean::{
44
inline, Clean, Crate, Deprecation, ExternalCrate, FnDecl, FnRetTy, Generic, GenericArg,
5-
GenericArgs, GenericBound, Generics, GetDefId, ImportSource, Item, ItemEnum, MacroKind, Path,
6-
PathSegment, Primitive, PrimitiveType, ResolvedPath, Span, Stability, Type, TypeBinding,
7-
TypeKind, Visibility, WherePredicate,
5+
GenericArgs, GenericBound, Generics, GetDefId, ImportSource, Item, ItemEnum, Lifetime,
6+
MacroKind, Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Span, Stability, Type,
7+
TypeBinding, TypeKind, Visibility, WherePredicate,
88
};
99
use crate::core::DocContext;
1010

@@ -121,7 +121,10 @@ pub fn external_generic_args(
121121
let args: Vec<_> = substs
122122
.iter()
123123
.filter_map(|kind| match kind.unpack() {
124-
GenericArgKind::Lifetime(lt) => lt.clean(cx).map(GenericArg::Lifetime),
124+
GenericArgKind::Lifetime(lt) => match lt {
125+
ty::ReLateBound(_, ty::BrAnon(_)) => Some(GenericArg::Lifetime(Lifetime::elided())),
126+
_ => lt.clean(cx).map(GenericArg::Lifetime),
127+
},
125128
GenericArgKind::Type(_) if skip_self => {
126129
skip_self = false;
127130
None

Diff for: src/test/rustdoc/auxiliary/elided-lifetime.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![crate_name = "bar"]
2+
3+
pub struct Ref<'a>(&'a u32);
4+
5+
pub fn test5(a: &u32) -> Ref {
6+
Ref(a)
7+
}
8+
9+
pub fn test6(a: &u32) -> Ref<'_> {
10+
Ref(a)
11+
}

Diff for: src/test/rustdoc/elided-lifetime.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// aux-build:elided-lifetime.rs
2+
//
3+
// rust-lang/rust#75225
4+
//
5+
// Since Rust 2018 we encourage writing out <'_> explicitly to make it clear
6+
// that borrowing is occuring. Make sure rustdoc is following the same idiom.
7+
8+
#![crate_name = "foo"]
9+
10+
pub struct Ref<'a>(&'a u32);
11+
type ARef<'a> = Ref<'a>;
12+
13+
// @has foo/fn.test1.html
14+
// @matches - "Ref</a>&lt;'_&gt;"
15+
pub fn test1(a: &u32) -> Ref {
16+
Ref(a)
17+
}
18+
19+
// @has foo/fn.test2.html
20+
// @matches - "Ref</a>&lt;'_&gt;"
21+
pub fn test2(a: &u32) -> Ref<'_> {
22+
Ref(a)
23+
}
24+
25+
// @has foo/fn.test3.html
26+
// @matches - "Ref</a>&lt;'_&gt;"
27+
pub fn test3(a: &u32) -> ARef {
28+
Ref(a)
29+
}
30+
31+
// @has foo/fn.test4.html
32+
// @matches - "Ref</a>&lt;'_&gt;"
33+
pub fn test4(a: &u32) -> ARef<'_> {
34+
Ref(a)
35+
}
36+
37+
// Ensure external paths in inlined docs also display elided lifetime
38+
// @has foo/bar/fn.test5.html
39+
// @matches - "Ref</a>&lt;'_&gt;"
40+
// @has foo/bar/fn.test6.html
41+
// @matches - "Ref</a>&lt;'_&gt;"
42+
#[doc(inline)]
43+
pub extern crate bar;

0 commit comments

Comments
 (0)