Skip to content

Commit 3a3f99a

Browse files
committed
rustdoc: Higher-ranked lifetimes can't have bounds
This cleans up the other spot I found where rustdoc was rendering bounds into the lifetime name itself. However, in this case, I don't think it could have actually happened because higher-ranked lifetime definitions aren't currently allowed to have bounds.
1 parent 2a60229 commit 3a3f99a

File tree

4 files changed

+45
-29
lines changed

4 files changed

+45
-29
lines changed

src/librustdoc/clean/mod.rs

+25-29
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rustc_target::spec::abi::Abi;
3030
use rustc_typeck::check::intrinsic::intrinsic_operation_unsafety;
3131
use rustc_typeck::hir_ty_to_ty;
3232

33+
use std::assert_matches::assert_matches;
3334
use std::collections::hash_map::Entry;
3435
use std::default::Default;
3536
use std::hash::Hash;
@@ -242,30 +243,6 @@ impl Clean<Lifetime> for hir::Lifetime {
242243
}
243244
}
244245

245-
impl Clean<Lifetime> for hir::GenericParam<'_> {
246-
fn clean(&self, _: &mut DocContext<'_>) -> Lifetime {
247-
match self.kind {
248-
hir::GenericParamKind::Lifetime { .. } => {
249-
if !self.bounds.is_empty() {
250-
let mut bounds = self.bounds.iter().map(|bound| match bound {
251-
hir::GenericBound::Outlives(lt) => lt,
252-
_ => panic!(),
253-
});
254-
let name = bounds.next().expect("no more bounds").name.ident();
255-
let mut s = format!("{}: {}", self.name.ident(), name);
256-
for bound in bounds {
257-
s.push_str(&format!(" + {}", bound.name.ident()));
258-
}
259-
Lifetime(Symbol::intern(&s))
260-
} else {
261-
Lifetime(self.name.ident().name)
262-
}
263-
}
264-
_ => panic!(),
265-
}
266-
}
267-
}
268-
269246
impl Clean<Constant> for hir::ConstArg {
270247
fn clean(&self, cx: &mut DocContext<'_>) -> Constant {
271248
Constant {
@@ -303,11 +280,30 @@ impl Clean<Option<Lifetime>> for ty::RegionKind {
303280
impl Clean<WherePredicate> for hir::WherePredicate<'_> {
304281
fn clean(&self, cx: &mut DocContext<'_>) -> WherePredicate {
305282
match *self {
306-
hir::WherePredicate::BoundPredicate(ref wbp) => WherePredicate::BoundPredicate {
307-
ty: wbp.bounded_ty.clean(cx),
308-
bounds: wbp.bounds.clean(cx),
309-
bound_params: wbp.bound_generic_params.into_iter().map(|x| x.clean(cx)).collect(),
310-
},
283+
hir::WherePredicate::BoundPredicate(ref wbp) => {
284+
let bound_params = wbp
285+
.bound_generic_params
286+
.into_iter()
287+
.map(|param| {
288+
// Higher-ranked params must be lifetimes.
289+
// Higher-ranked lifetimes can't have bounds.
290+
assert_matches!(
291+
param,
292+
hir::GenericParam {
293+
kind: hir::GenericParamKind::Lifetime { .. },
294+
bounds: [],
295+
..
296+
}
297+
);
298+
Lifetime(param.name.ident().name)
299+
})
300+
.collect();
301+
WherePredicate::BoundPredicate {
302+
ty: wbp.bounded_ty.clean(cx),
303+
bounds: wbp.bounds.clean(cx),
304+
bound_params,
305+
}
306+
}
311307

312308
hir::WherePredicate::RegionPredicate(ref wrp) => WherePredicate::RegionPredicate {
313309
lifetime: wrp.lifetime.clean(cx),

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
)]
55
#![feature(rustc_private)]
66
#![feature(array_methods)]
7+
#![feature(assert_matches)]
78
#![feature(box_patterns)]
89
#![feature(control_flow_enum)]
910
#![feature(in_band_lifetimes)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This test ensures that rustdoc doesn't panic on higher-ranked lifetimes
2+
// with bounds, because an error should have already been emitted by rustc.
3+
4+
pub fn hrlt<'b, 'c>()
5+
where
6+
for<'a: 'b + 'c> &'a (): std::fmt::Debug,
7+
//~^ ERROR lifetime bounds cannot be used in this context
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: lifetime bounds cannot be used in this context
2+
--> $DIR/bounded-hr-lifetime.rs:6:13
3+
|
4+
LL | for<'a: 'b + 'c> &'a (): std::fmt::Debug,
5+
| ^^ ^^
6+
7+
error: Compilation failed, aborting rustdoc
8+
9+
error: aborting due to 2 previous errors
10+

0 commit comments

Comments
 (0)