Skip to content

Commit

Permalink
Rollup merge of rust-lang#85957 - BoxyUwU:rustdoc-const-generic-defau…
Browse files Browse the repository at this point in the history
…lts, r=oli-obk

Display defaults on const params- rustdoc

previously rustdoc would render this struct declaration:
`pub struct Foo<const N: usize = 10>;`
as:
`pub struct Foo<const N: usize>;`
this PR changes it to render correctly
  • Loading branch information
GuillaumeGomez committed Jun 7, 2021
2 parents e4a6032 + f7117f8 commit b7ae6cd
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
}
}
GenericParamDefKind::Lifetime => {}
GenericParamDefKind::Const { .. } => {}
GenericParamDefKind::Const { ref mut default, .. } => {
// We never want something like `impl<const N: usize = 10>`
default.take();
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,15 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
},
)
}
ty::GenericParamDefKind::Const { .. } => (
ty::GenericParamDefKind::Const { has_default, .. } => (
self.name,
GenericParamDefKind::Const {
did: self.def_id,
ty: cx.tcx.type_of(self.def_id).clean(cx),
default: match has_default {
true => Some(cx.tcx.const_param_default(self.def_id).to_string()),
false => None,
},
},
),
};
Expand Down Expand Up @@ -487,12 +491,15 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
synthetic,
},
),
hir::GenericParamKind::Const { ref ty, default: _ } => (
hir::GenericParamKind::Const { ref ty, default } => (
self.name.ident().name,
GenericParamDefKind::Const {
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
ty: ty.clean(cx),
// FIXME(const_generics_defaults): add `default` field here for docs
default: default.map(|ct| {
let def_id = cx.tcx.hir().local_def_id(ct.hir_id);
ty::Const::from_anon_const(cx.tcx, def_id).to_string()
}),
},
),
};
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ crate enum GenericParamDefKind {
Const {
did: DefId,
ty: Type,
default: Option<String>,
},
}

Expand Down
16 changes: 13 additions & 3 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,22 @@ impl clean::GenericParamDef {

Ok(())
}
clean::GenericParamDefKind::Const { ref ty, .. } => {
clean::GenericParamDefKind::Const { ref ty, ref default, .. } => {
if f.alternate() {
write!(f, "const {}: {:#}", self.name, ty.print(cx))
write!(f, "const {}: {:#}", self.name, ty.print(cx))?;
} else {
write!(f, "const {}:&nbsp;{}", self.name, ty.print(cx))
write!(f, "const {}:&nbsp;{}", self.name, ty.print(cx))?;
}

if let Some(default) = default {
if f.alternate() {
write!(f, " = {:#}", default)?;
} else {
write!(f, "&nbsp;=&nbsp;{}", default)?;
}
}

Ok(())
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
default: default.map(|x| x.into_tcx(tcx)),
},
Const { did: _, ty } => GenericParamDefKind::Const(ty.into_tcx(tcx)),
Const { did: _, ty, default } => {
GenericParamDefKind::Const { ty: ty.into_tcx(tcx), default }
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
)
})
.collect(),
format_version: 5,
format_version: 6,
};
let mut p = self.out_path.clone();
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
Expand Down
2 changes: 1 addition & 1 deletion src/rustdoc-json-types/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ pub struct GenericParamDef {
pub enum GenericParamDefKind {
Lifetime,
Type { bounds: Vec<GenericBound>, default: Option<Type> },
Const(Type),
Const { ty: Type, default: Option<String> },
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
Expand Down
6 changes: 6 additions & 0 deletions src/test/rustdoc/const-generics/const-generic-defaults.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![crate_name = "foo"]
#![feature(const_generics_defaults)]

// @has foo/struct.Foo.html '//pre[@class="rust struct"]' \
// 'pub struct Foo<const M: usize = 10_usize, const N: usize = M, T = i32>(_);'
pub struct Foo<const M: usize = 10, const N: usize = M, T = i32>(T);

0 comments on commit b7ae6cd

Please sign in to comment.