rustdoc: Clean up handling of lifetime bounds
Previously, rustdoc recorded lifetime bounds by rendering them into the name of the lifetime parameter. Now, it leaves the name as the actual name and instead records lifetime bounds in an `outlives` list, similar to how type parameter bounds are recorded.
This commit is contained in:
@@ -331,9 +331,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
|
|||||||
match br {
|
match br {
|
||||||
// We only care about named late bound regions, as we need to add them
|
// We only care about named late bound regions, as we need to add them
|
||||||
// to the 'for<>' section
|
// to the 'for<>' section
|
||||||
ty::BrNamed(_, name) => {
|
ty::BrNamed(_, name) => Some(GenericParamDef {
|
||||||
Some(GenericParamDef { name, kind: GenericParamDefKind::Lifetime })
|
name,
|
||||||
}
|
kind: GenericParamDefKind::Lifetime { outlives: vec![] },
|
||||||
|
}),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -659,7 +660,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
|
|||||||
bounds.insert(0, GenericBound::maybe_sized(self.cx));
|
bounds.insert(0, GenericBound::maybe_sized(self.cx));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GenericParamDefKind::Lifetime => {}
|
GenericParamDefKind::Lifetime { .. } => {}
|
||||||
GenericParamDefKind::Const { ref mut default, .. } => {
|
GenericParamDefKind::Const { ref mut default, .. } => {
|
||||||
// We never want something like `impl<const N: usize = 10>`
|
// We never want something like `impl<const N: usize = 10>`
|
||||||
default.take();
|
default.take();
|
||||||
|
|||||||
@@ -199,9 +199,10 @@ impl Clean<GenericBound> for (ty::PolyTraitRef<'_>, &[TypeBinding]) {
|
|||||||
.collect_referenced_late_bound_regions(&poly_trait_ref)
|
.collect_referenced_late_bound_regions(&poly_trait_ref)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|br| match br {
|
.filter_map(|br| match br {
|
||||||
ty::BrNamed(_, name) => {
|
ty::BrNamed(_, name) => Some(GenericParamDef {
|
||||||
Some(GenericParamDef { name, kind: GenericParamDefKind::Lifetime })
|
name,
|
||||||
}
|
kind: GenericParamDefKind::Lifetime { outlives: vec![] },
|
||||||
|
}),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@@ -412,7 +413,9 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
|
|||||||
impl Clean<GenericParamDef> for ty::GenericParamDef {
|
impl Clean<GenericParamDef> for ty::GenericParamDef {
|
||||||
fn clean(&self, cx: &mut DocContext<'_>) -> GenericParamDef {
|
fn clean(&self, cx: &mut DocContext<'_>) -> GenericParamDef {
|
||||||
let (name, kind) = match self.kind {
|
let (name, kind) = match self.kind {
|
||||||
ty::GenericParamDefKind::Lifetime => (self.name, GenericParamDefKind::Lifetime),
|
ty::GenericParamDefKind::Lifetime => {
|
||||||
|
(self.name, GenericParamDefKind::Lifetime { outlives: vec![] })
|
||||||
|
}
|
||||||
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
|
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
|
||||||
let default = if has_default {
|
let default = if has_default {
|
||||||
let mut default = cx.tcx.type_of(self.def_id).clean(cx);
|
let mut default = cx.tcx.type_of(self.def_id).clean(cx);
|
||||||
@@ -462,21 +465,15 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
|
|||||||
fn clean(&self, cx: &mut DocContext<'_>) -> GenericParamDef {
|
fn clean(&self, cx: &mut DocContext<'_>) -> GenericParamDef {
|
||||||
let (name, kind) = match self.kind {
|
let (name, kind) = match self.kind {
|
||||||
hir::GenericParamKind::Lifetime { .. } => {
|
hir::GenericParamKind::Lifetime { .. } => {
|
||||||
let name = if !self.bounds.is_empty() {
|
let outlives = self
|
||||||
let mut bounds = self.bounds.iter().map(|bound| match bound {
|
.bounds
|
||||||
hir::GenericBound::Outlives(lt) => lt,
|
.iter()
|
||||||
|
.map(|bound| match bound {
|
||||||
|
hir::GenericBound::Outlives(lt) => lt.clean(cx),
|
||||||
_ => panic!(),
|
_ => panic!(),
|
||||||
});
|
})
|
||||||
let name = bounds.next().expect("no more bounds").name.ident();
|
.collect();
|
||||||
let mut s = format!("{}: {}", self.name.ident(), name);
|
(self.name.ident().name, GenericParamDefKind::Lifetime { outlives })
|
||||||
for bound in bounds {
|
|
||||||
s.push_str(&format!(" + {}", bound.name.ident()));
|
|
||||||
}
|
|
||||||
Symbol::intern(&s)
|
|
||||||
} else {
|
|
||||||
self.name.ident().name
|
|
||||||
};
|
|
||||||
(name, GenericParamDefKind::Lifetime)
|
|
||||||
}
|
}
|
||||||
hir::GenericParamKind::Type { ref default, synthetic } => (
|
hir::GenericParamKind::Type { ref default, synthetic } => (
|
||||||
self.name.ident().name,
|
self.name.ident().name,
|
||||||
@@ -536,7 +533,7 @@ impl Clean<Generics> for hir::Generics<'_> {
|
|||||||
.map(|param| {
|
.map(|param| {
|
||||||
let param: GenericParamDef = param.clean(cx);
|
let param: GenericParamDef = param.clean(cx);
|
||||||
match param.kind {
|
match param.kind {
|
||||||
GenericParamDefKind::Lifetime => unreachable!(),
|
GenericParamDefKind::Lifetime { .. } => unreachable!(),
|
||||||
GenericParamDefKind::Type { did, ref bounds, .. } => {
|
GenericParamDefKind::Type { did, ref bounds, .. } => {
|
||||||
cx.impl_trait_bounds.insert(did.into(), bounds.clone());
|
cx.impl_trait_bounds.insert(did.into(), bounds.clone());
|
||||||
}
|
}
|
||||||
@@ -569,7 +566,7 @@ impl Clean<Generics> for hir::Generics<'_> {
|
|||||||
{
|
{
|
||||||
for param in &mut generics.params {
|
for param in &mut generics.params {
|
||||||
match param.kind {
|
match param.kind {
|
||||||
GenericParamDefKind::Lifetime => {}
|
GenericParamDefKind::Lifetime { .. } => {}
|
||||||
GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
|
GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => {
|
||||||
if ¶m.name == name {
|
if ¶m.name == name {
|
||||||
mem::swap(bounds, ty_bounds);
|
mem::swap(bounds, ty_bounds);
|
||||||
|
|||||||
@@ -1231,7 +1231,9 @@ impl WherePredicate {
|
|||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
||||||
crate enum GenericParamDefKind {
|
crate enum GenericParamDefKind {
|
||||||
Lifetime,
|
Lifetime {
|
||||||
|
outlives: Vec<Lifetime>,
|
||||||
|
},
|
||||||
Type {
|
Type {
|
||||||
did: DefId,
|
did: DefId,
|
||||||
bounds: Vec<GenericBound>,
|
bounds: Vec<GenericBound>,
|
||||||
@@ -1257,7 +1259,7 @@ impl GenericParamDefKind {
|
|||||||
match self {
|
match self {
|
||||||
GenericParamDefKind::Type { default, .. } => default.clone(),
|
GenericParamDefKind::Type { default, .. } => default.clone(),
|
||||||
GenericParamDefKind::Const { ty, .. } => Some(ty.clone()),
|
GenericParamDefKind::Const { ty, .. } => Some(ty.clone()),
|
||||||
GenericParamDefKind::Lifetime => None,
|
GenericParamDefKind::Lifetime { .. } => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1271,7 +1273,7 @@ crate struct GenericParamDef {
|
|||||||
impl GenericParamDef {
|
impl GenericParamDef {
|
||||||
crate fn is_synthetic_type_param(&self) -> bool {
|
crate fn is_synthetic_type_param(&self) -> bool {
|
||||||
match self.kind {
|
match self.kind {
|
||||||
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => false,
|
GenericParamDefKind::Lifetime { .. } | GenericParamDefKind::Const { .. } => false,
|
||||||
GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(),
|
GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,9 +155,23 @@ impl clean::GenericParamDef {
|
|||||||
&'a self,
|
&'a self,
|
||||||
cx: &'a Context<'tcx>,
|
cx: &'a Context<'tcx>,
|
||||||
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
) -> impl fmt::Display + 'a + Captures<'tcx> {
|
||||||
display_fn(move |f| match self.kind {
|
display_fn(move |f| match &self.kind {
|
||||||
clean::GenericParamDefKind::Lifetime => write!(f, "{}", self.name),
|
clean::GenericParamDefKind::Lifetime { outlives } => {
|
||||||
clean::GenericParamDefKind::Type { ref bounds, ref default, .. } => {
|
write!(f, "{}", self.name)?;
|
||||||
|
|
||||||
|
if !outlives.is_empty() {
|
||||||
|
f.write_str(": ")?;
|
||||||
|
for (i, lt) in outlives.iter().enumerate() {
|
||||||
|
if i != 0 {
|
||||||
|
f.write_str(" + ")?;
|
||||||
|
}
|
||||||
|
write!(f, "{}", lt.print())?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
clean::GenericParamDefKind::Type { bounds, default, .. } => {
|
||||||
f.write_str(&*self.name.as_str())?;
|
f.write_str(&*self.name.as_str())?;
|
||||||
|
|
||||||
if !bounds.is_empty() {
|
if !bounds.is_empty() {
|
||||||
@@ -178,7 +192,7 @@ impl clean::GenericParamDef {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
clean::GenericParamDefKind::Const { ref ty, ref default, .. } => {
|
clean::GenericParamDefKind::Const { ty, default, .. } => {
|
||||||
if f.alternate() {
|
if f.alternate() {
|
||||||
write!(f, "const {}: {:#}", self.name, ty.print(cx))?;
|
write!(f, "const {}: {:#}", self.name, ty.print(cx))?;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -326,7 +326,9 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
|
|||||||
fn from_tcx(kind: clean::GenericParamDefKind, tcx: TyCtxt<'_>) -> Self {
|
fn from_tcx(kind: clean::GenericParamDefKind, tcx: TyCtxt<'_>) -> Self {
|
||||||
use clean::GenericParamDefKind::*;
|
use clean::GenericParamDefKind::*;
|
||||||
match kind {
|
match kind {
|
||||||
Lifetime => GenericParamDefKind::Lifetime,
|
Lifetime { outlives } => GenericParamDefKind::Lifetime {
|
||||||
|
outlives: outlives.into_iter().map(|lt| lt.0.to_string()).collect(),
|
||||||
|
},
|
||||||
Type { did: _, bounds, default, synthetic: _ } => GenericParamDefKind::Type {
|
Type { did: _, bounds, default, synthetic: _ } => GenericParamDefKind::Type {
|
||||||
bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
|
bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
|
||||||
default: default.map(|x| x.into_tcx(tcx)),
|
default: default.map(|x| x.into_tcx(tcx)),
|
||||||
|
|||||||
@@ -234,7 +234,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
format_version: 6,
|
format_version: 7,
|
||||||
};
|
};
|
||||||
let mut p = self.out_path.clone();
|
let mut p = self.out_path.clone();
|
||||||
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
|
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
|
||||||
|
|||||||
@@ -323,7 +323,7 @@ pub struct GenericParamDef {
|
|||||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub enum GenericParamDefKind {
|
pub enum GenericParamDefKind {
|
||||||
Lifetime,
|
Lifetime { outlives: Vec<String> },
|
||||||
Type { bounds: Vec<GenericBound>, default: Option<Type> },
|
Type { bounds: Vec<GenericBound>, default: Option<Type> },
|
||||||
Const { ty: Type, default: Option<String> },
|
Const { ty: Type, default: Option<String> },
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// @has with_primitives.json "$.index[*][?(@.name=='WithPrimitives')].visibility" \"public\"
|
// @has with_primitives.json "$.index[*][?(@.name=='WithPrimitives')].visibility" \"public\"
|
||||||
// @has - "$.index[*][?(@.name=='WithPrimitives')].kind" \"struct\"
|
// @has - "$.index[*][?(@.name=='WithPrimitives')].kind" \"struct\"
|
||||||
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.generics.params[0].name" \"\'a\"
|
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.generics.params[0].name" \"\'a\"
|
||||||
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.generics.params[0].kind" \"lifetime\"
|
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.generics.params[0].kind.lifetime.outlives" []
|
||||||
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.struct_type" \"plain\"
|
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.struct_type" \"plain\"
|
||||||
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.fields_stripped" true
|
// @has - "$.index[*][?(@.name=='WithPrimitives')].inner.fields_stripped" true
|
||||||
pub struct WithPrimitives<'a> {
|
pub struct WithPrimitives<'a> {
|
||||||
|
|||||||
Reference in New Issue
Block a user