Skip to content

Commit

Permalink
rustdoc: Higher-ranked lifetimes can't have bounds
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
camelid committed Sep 2, 2021
1 parent 2a60229 commit 3a3f99a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use rustc_target::spec::abi::Abi;
use rustc_typeck::check::intrinsic::intrinsic_operation_unsafety;
use rustc_typeck::hir_ty_to_ty;

use std::assert_matches::assert_matches;
use std::collections::hash_map::Entry;
use std::default::Default;
use std::hash::Hash;
Expand Down Expand Up @@ -242,30 +243,6 @@ impl Clean<Lifetime> for hir::Lifetime {
}
}

impl Clean<Lifetime> for hir::GenericParam<'_> {
fn clean(&self, _: &mut DocContext<'_>) -> Lifetime {
match self.kind {
hir::GenericParamKind::Lifetime { .. } => {
if !self.bounds.is_empty() {
let mut bounds = self.bounds.iter().map(|bound| match bound {
hir::GenericBound::Outlives(lt) => lt,
_ => panic!(),
});
let name = bounds.next().expect("no more bounds").name.ident();
let mut s = format!("{}: {}", self.name.ident(), name);
for bound in bounds {
s.push_str(&format!(" + {}", bound.name.ident()));
}
Lifetime(Symbol::intern(&s))
} else {
Lifetime(self.name.ident().name)
}
}
_ => panic!(),
}
}
}

impl Clean<Constant> for hir::ConstArg {
fn clean(&self, cx: &mut DocContext<'_>) -> Constant {
Constant {
Expand Down Expand Up @@ -303,11 +280,30 @@ impl Clean<Option<Lifetime>> for ty::RegionKind {
impl Clean<WherePredicate> for hir::WherePredicate<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> WherePredicate {
match *self {
hir::WherePredicate::BoundPredicate(ref wbp) => WherePredicate::BoundPredicate {
ty: wbp.bounded_ty.clean(cx),
bounds: wbp.bounds.clean(cx),
bound_params: wbp.bound_generic_params.into_iter().map(|x| x.clean(cx)).collect(),
},
hir::WherePredicate::BoundPredicate(ref wbp) => {
let bound_params = wbp
.bound_generic_params
.into_iter()
.map(|param| {
// Higher-ranked params must be lifetimes.
// Higher-ranked lifetimes can't have bounds.
assert_matches!(
param,
hir::GenericParam {
kind: hir::GenericParamKind::Lifetime { .. },
bounds: [],
..
}
);
Lifetime(param.name.ident().name)
})
.collect();
WherePredicate::BoundPredicate {
ty: wbp.bounded_ty.clean(cx),
bounds: wbp.bounds.clean(cx),
bound_params,
}
}

hir::WherePredicate::RegionPredicate(ref wrp) => WherePredicate::RegionPredicate {
lifetime: wrp.lifetime.clean(cx),
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
)]
#![feature(rustc_private)]
#![feature(array_methods)]
#![feature(assert_matches)]
#![feature(box_patterns)]
#![feature(control_flow_enum)]
#![feature(in_band_lifetimes)]
Expand Down
9 changes: 9 additions & 0 deletions src/test/rustdoc-ui/bounded-hr-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This test ensures that rustdoc doesn't panic on higher-ranked lifetimes
// with bounds, because an error should have already been emitted by rustc.

pub fn hrlt<'b, 'c>()
where
for<'a: 'b + 'c> &'a (): std::fmt::Debug,
//~^ ERROR lifetime bounds cannot be used in this context
{
}
10 changes: 10 additions & 0 deletions src/test/rustdoc-ui/bounded-hr-lifetime.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: lifetime bounds cannot be used in this context
--> $DIR/bounded-hr-lifetime.rs:6:13
|
LL | for<'a: 'b + 'c> &'a (): std::fmt::Debug,
| ^^ ^^

error: Compilation failed, aborting rustdoc

error: aborting due to 2 previous errors

0 comments on commit 3a3f99a

Please sign in to comment.