Skip to content

Commit 63c0d9c

Browse files
committed
Display elided lifetime for non-reference type in doc
1 parent c15bae5 commit 63c0d9c

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

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),

src/librustdoc/clean/types.rs

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

754758
#[derive(Clone, Debug)]

src/test/rustdoc/elided-lifetime.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![crate_name = "foo"]
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+
pub struct Ref<'a>(&'a u32);
9+
type ARef<'a> = Ref<'a>;
10+
11+
// @has foo/fn.test1.html
12+
// @matches - "Ref</a>&lt;'_&gt;"
13+
pub fn test1(a: &u32) -> Ref {
14+
Ref(a)
15+
}
16+
17+
// @has foo/fn.test2.html
18+
// @matches - "Ref</a>&lt;'_&gt;"
19+
pub fn test2(a: &u32) -> Ref<'_> {
20+
Ref(a)
21+
}
22+
23+
// @has foo/fn.test3.html
24+
// @matches - "Ref</a>&lt;'_&gt;"
25+
pub fn test3(a: &u32) -> ARef {
26+
Ref(a)
27+
}
28+
29+
// @has foo/fn.test4.html
30+
// @matches - "Ref</a>&lt;'_&gt;"
31+
pub fn test4(a: &u32) -> ARef<'_> {
32+
Ref(a)
33+
}

0 commit comments

Comments
 (0)