Skip to content

Commit 23f9e1a

Browse files
committed
rustdoc: change memory repr of generic params
In anticipation of adding variance to them.
1 parent bdc8c5c commit 23f9e1a

File tree

6 files changed

+120
-131
lines changed

6 files changed

+120
-131
lines changed

src/librustdoc/clean/auto_trait.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -632,19 +632,19 @@ where
632632
existing_predicates.extend(final_bounds);
633633

634634
for param in generic_params.iter_mut() {
635-
match param.kind {
636-
GenericParamDefKind::Type { ref mut default, ref mut bounds, .. } => {
635+
match &mut param.kind {
636+
GenericParamDefKind::Type(ty_param) => {
637637
// We never want something like `impl<T=Foo>`.
638-
default.take();
638+
ty_param.default.take();
639639
let generic_ty = Type::Generic(param.name);
640640
if !has_sized.contains(&generic_ty) {
641-
bounds.insert(0, GenericBound::maybe_sized(self.cx));
641+
ty_param.bounds.insert(0, GenericBound::maybe_sized(self.cx));
642642
}
643643
}
644644
GenericParamDefKind::Lifetime { .. } => {}
645-
GenericParamDefKind::Const { ref mut default, .. } => {
645+
GenericParamDefKind::Const(ct_param) => {
646646
// We never want something like `impl<const N: usize = 10>`
647-
default.take();
647+
ct_param.default.take();
648648
}
649649
}
650650
}

src/librustdoc/clean/mod.rs

+40-51
Original file line numberDiff line numberDiff line change
@@ -506,32 +506,31 @@ fn clean_generic_param_def<'tcx>(
506506
) -> GenericParamDef {
507507
let (name, kind) = match def.kind {
508508
ty::GenericParamDefKind::Lifetime => {
509-
(def.name, GenericParamDefKind::Lifetime { outlives: ThinVec::new() })
509+
(def.name, LifetimeParam { outlives: ThinVec::new() }.into())
510510
}
511511
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
512-
let default = if has_default {
513-
Some(clean_middle_ty(
512+
let default = has_default.then(|| {
513+
clean_middle_ty(
514514
ty::Binder::dummy(cx.tcx.type_of(def.def_id).instantiate_identity()),
515515
cx,
516516
Some(def.def_id),
517517
None,
518-
))
519-
} else {
520-
None
521-
};
518+
)
519+
});
522520
(
523521
def.name,
524-
GenericParamDefKind::Type {
525-
bounds: ThinVec::new(), // These are filled in from the where-clauses.
526-
default: default.map(Box::new),
522+
TypeParam {
523+
bounds: ThinVec::new(), // These are filled in from the where-clause.
524+
default,
527525
synthetic,
528-
},
526+
}
527+
.into(),
529528
)
530529
}
531530
ty::GenericParamDefKind::Const { has_default, is_host_effect } => (
532531
def.name,
533-
GenericParamDefKind::Const {
534-
ty: Box::new(clean_middle_ty(
532+
ConstParam {
533+
ty: clean_middle_ty(
535534
ty::Binder::dummy(
536535
cx.tcx
537536
.type_of(def.def_id)
@@ -541,15 +540,13 @@ fn clean_generic_param_def<'tcx>(
541540
cx,
542541
Some(def.def_id),
543542
None,
544-
)),
545-
default: match has_default {
546-
true => Some(Box::new(
547-
cx.tcx.const_param_default(def.def_id).instantiate_identity().to_string(),
548-
)),
549-
false => None,
550-
},
543+
),
544+
default: has_default.then(|| {
545+
cx.tcx.const_param_default(def.def_id).instantiate_identity().to_string()
546+
}),
551547
is_host_effect,
552-
},
548+
}
549+
.into(),
553550
),
554551
};
555552

@@ -576,7 +573,7 @@ fn clean_generic_param<'tcx>(
576573
} else {
577574
ThinVec::new()
578575
};
579-
(param.name.ident().name, GenericParamDefKind::Lifetime { outlives })
576+
(param.name.ident().name, LifetimeParam { outlives }.into())
580577
}
581578
hir::GenericParamKind::Type { ref default, synthetic } => {
582579
let bounds = if let Some(generics) = generics {
@@ -591,21 +588,18 @@ fn clean_generic_param<'tcx>(
591588
};
592589
(
593590
param.name.ident().name,
594-
GenericParamDefKind::Type {
595-
bounds,
596-
default: default.map(|t| clean_ty(t, cx)).map(Box::new),
597-
synthetic,
598-
},
591+
TypeParam { bounds, default: default.map(|t| clean_ty(t, cx)), synthetic }.into(),
599592
)
600593
}
601594
hir::GenericParamKind::Const { ty, default, is_host_effect } => (
602595
param.name.ident().name,
603-
GenericParamDefKind::Const {
604-
ty: Box::new(clean_ty(ty, cx)),
596+
ConstParam {
597+
ty: clean_ty(ty, cx),
605598
default: default
606-
.map(|ct| Box::new(ty::Const::from_anon_const(cx.tcx, ct.def_id).to_string())),
599+
.map(|ct| ty::Const::from_anon_const(cx.tcx, ct.def_id).to_string()),
607600
is_host_effect,
608-
},
601+
}
602+
.into(),
609603
),
610604
};
611605

@@ -643,8 +637,8 @@ pub(crate) fn clean_generics<'tcx>(
643637
.filter(|param| is_impl_trait(param))
644638
.map(|param| {
645639
let param = clean_generic_param(cx, Some(generics), param);
646-
let GenericParamDefKind::Type { bounds, .. } = &param.kind else { unreachable!() };
647-
cx.impl_trait_bounds.insert(param.def_id.into(), bounds.to_vec());
640+
let GenericParamDefKind::Type(ty_param) = &param.kind else { unreachable!() };
641+
cx.impl_trait_bounds.insert(param.def_id.into(), ty_param.bounds.to_vec());
648642
param
649643
})
650644
.collect::<Vec<_>>();
@@ -702,21 +696,21 @@ pub(crate) fn clean_generics<'tcx>(
702696
for param in generics.params.iter().filter(|p| !is_impl_trait(p) && !is_elided_lifetime(p)) {
703697
let mut param = clean_generic_param(cx, Some(generics), param);
704698
match &mut param.kind {
705-
GenericParamDefKind::Lifetime { outlives } => {
699+
GenericParamDefKind::Lifetime(lt_param) => {
706700
if let Some(region_pred) = region_predicates.get_mut(&Lifetime(param.name)) {
707701
// We merge bounds in the `where` clause.
708-
for outlive in outlives.drain(..) {
702+
for outlive in lt_param.outlives.drain(..) {
709703
let outlive = GenericBound::Outlives(outlive);
710704
if !region_pred.contains(&outlive) {
711705
region_pred.push(outlive);
712706
}
713707
}
714708
}
715709
}
716-
GenericParamDefKind::Type { bounds, synthetic: false, .. } => {
710+
GenericParamDefKind::Type(ty_param) if !ty_param.synthetic => {
717711
if let Some(bound_pred) = bound_predicates.get_mut(&Type::Generic(param.name)) {
718712
// We merge bounds in the `where` clause.
719-
for bound in bounds.drain(..) {
713+
for bound in ty_param.bounds.drain(..) {
720714
if !bound_pred.0.contains(&bound) {
721715
bound_pred.0.push(bound);
722716
}
@@ -1063,17 +1057,15 @@ fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[ast::Attrib
10631057
for (pos, literal) in meta_item_list.iter().filter_map(|meta| meta.lit()).enumerate() {
10641058
match literal.kind {
10651059
ast::LitKind::Int(a, _) => {
1066-
let gen = func.generics.params.remove(0);
1060+
let param = func.generics.params.remove(0);
10671061
if let GenericParamDef {
1068-
name,
1069-
kind: GenericParamDefKind::Const { ty, .. },
1070-
..
1071-
} = gen
1062+
name, kind: GenericParamDefKind::Const(param), ..
1063+
} = param
10721064
{
1073-
func.decl
1074-
.inputs
1075-
.values
1076-
.insert(a.get() as _, Argument { name, type_: *ty, is_const: true });
1065+
func.decl.inputs.values.insert(
1066+
a.get() as _,
1067+
Argument { name, type_: param.ty, is_const: true },
1068+
);
10771069
} else {
10781070
panic!("unexpected non const in position {pos}");
10791071
}
@@ -3149,11 +3141,8 @@ fn clean_bound_vars<'tcx>(
31493141
Some(GenericParamDef {
31503142
name,
31513143
def_id,
3152-
kind: GenericParamDefKind::Type {
3153-
bounds: ThinVec::new(),
3154-
default: None,
3155-
synthetic: false,
3156-
},
3144+
kind: TypeParam { bounds: ThinVec::new(), default: None, synthetic: false }
3145+
.into(),
31573146
})
31583147
}
31593148
// FIXME(non_lifetime_binders): Support higher-ranked const parameters.

src/librustdoc/clean/simplify.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,16 @@ pub(crate) fn move_bounds_to_generic_parameters(generics: &mut clean::Generics)
140140
let mut where_predicates = ThinVec::new();
141141
for mut pred in generics.where_predicates.drain(..) {
142142
if let WherePredicate::BoundPredicate { ty: Generic(arg), bounds, .. } = &mut pred
143-
&& let Some(GenericParamDef {
144-
kind: GenericParamDefKind::Type { bounds: param_bounds, .. },
145-
..
146-
}) = generics.params.iter_mut().find(|param| &param.name == arg)
143+
&& let Some(GenericParamDef { kind: GenericParamDefKind::Type(param), .. }) =
144+
generics.params.iter_mut().find(|param| &param.name == arg)
147145
{
148-
param_bounds.extend(bounds.drain(..));
146+
param.bounds.extend(bounds.drain(..));
149147
} else if let WherePredicate::RegionPredicate { lifetime: Lifetime(arg), bounds } =
150148
&mut pred
151-
&& let Some(GenericParamDef {
152-
kind: GenericParamDefKind::Lifetime { outlives: param_bounds },
153-
..
154-
}) = generics.params.iter_mut().find(|param| &param.name == arg)
149+
&& let Some(GenericParamDef { kind: GenericParamDefKind::Lifetime(param), .. }) =
150+
generics.params.iter_mut().find(|param| &param.name == arg)
155151
{
156-
param_bounds.extend(bounds.drain(..).map(|bound| match bound {
152+
param.outlives.extend(bounds.drain(..).map(|bound| match bound {
157153
GenericBound::Outlives(lifetime) => lifetime,
158154
_ => unreachable!(),
159155
}));

src/librustdoc/clean/types.rs

+48-12
Original file line numberDiff line numberDiff line change
@@ -1325,10 +1325,9 @@ impl WherePredicate {
13251325

13261326
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
13271327
pub(crate) enum GenericParamDefKind {
1328-
Lifetime { outlives: ThinVec<Lifetime> },
1329-
Type { bounds: ThinVec<GenericBound>, default: Option<Box<Type>>, synthetic: bool },
1330-
// Option<Box<String>> makes this type smaller than `Option<String>` would.
1331-
Const { ty: Box<Type>, default: Option<Box<String>>, is_host_effect: bool },
1328+
Lifetime(Box<LifetimeParam>),
1329+
Type(Box<TypeParam>),
1330+
Const(Box<ConstParam>),
13321331
}
13331332

13341333
impl GenericParamDefKind {
@@ -1337,6 +1336,43 @@ impl GenericParamDefKind {
13371336
}
13381337
}
13391338

1339+
impl From<LifetimeParam> for GenericParamDefKind {
1340+
fn from(param: LifetimeParam) -> Self {
1341+
Self::Lifetime(Box::new(param))
1342+
}
1343+
}
1344+
1345+
impl From<TypeParam> for GenericParamDefKind {
1346+
fn from(param: TypeParam) -> Self {
1347+
Self::Type(Box::new(param))
1348+
}
1349+
}
1350+
1351+
impl From<ConstParam> for GenericParamDefKind {
1352+
fn from(param: ConstParam) -> Self {
1353+
Self::Const(Box::new(param))
1354+
}
1355+
}
1356+
1357+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
1358+
pub(crate) struct LifetimeParam {
1359+
pub(crate) outlives: ThinVec<Lifetime>,
1360+
}
1361+
1362+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
1363+
pub(crate) struct TypeParam {
1364+
pub(crate) bounds: ThinVec<GenericBound>,
1365+
pub(crate) default: Option<Type>,
1366+
pub(crate) synthetic: bool,
1367+
}
1368+
1369+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
1370+
pub(crate) struct ConstParam {
1371+
pub(crate) ty: Type,
1372+
pub(crate) default: Option<String>,
1373+
pub(crate) is_host_effect: bool,
1374+
}
1375+
13401376
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
13411377
pub(crate) struct GenericParamDef {
13421378
pub(crate) name: Symbol,
@@ -1346,14 +1382,14 @@ pub(crate) struct GenericParamDef {
13461382

13471383
impl GenericParamDef {
13481384
pub(crate) fn lifetime(def_id: DefId, name: Symbol) -> Self {
1349-
Self { name, def_id, kind: GenericParamDefKind::Lifetime { outlives: ThinVec::new() } }
1385+
Self { name, def_id, kind: LifetimeParam { outlives: ThinVec::new() }.into() }
13501386
}
13511387

13521388
pub(crate) fn is_synthetic_param(&self) -> bool {
1353-
match self.kind {
1354-
GenericParamDefKind::Lifetime { .. } => false,
1355-
GenericParamDefKind::Const { is_host_effect, .. } => is_host_effect,
1356-
GenericParamDefKind::Type { synthetic, .. } => synthetic,
1389+
match &self.kind {
1390+
GenericParamDefKind::Lifetime(_) => false,
1391+
GenericParamDefKind::Const(param) => param.is_host_effect,
1392+
GenericParamDefKind::Type(param) => param.synthetic,
13571393
}
13581394
}
13591395

@@ -1362,8 +1398,8 @@ impl GenericParamDef {
13621398
}
13631399

13641400
pub(crate) fn get_bounds(&self) -> Option<&[GenericBound]> {
1365-
match self.kind {
1366-
GenericParamDefKind::Type { ref bounds, .. } => Some(bounds),
1401+
match &self.kind {
1402+
GenericParamDefKind::Type(param) => Some(&param.bounds),
13671403
_ => None,
13681404
}
13691405
}
@@ -2561,7 +2597,7 @@ mod size_asserts {
25612597
static_assert_size!(DocFragment, 32);
25622598
static_assert_size!(GenericArg, 32);
25632599
static_assert_size!(GenericArgs, 32);
2564-
static_assert_size!(GenericParamDef, 40);
2600+
static_assert_size!(GenericParamDef, 32);
25652601
static_assert_size!(Generics, 16);
25662602
static_assert_size!(Item, 56);
25672603
static_assert_size!(ItemKind, 56);

src/librustdoc/html/format.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ impl clean::GenericParamDef {
187187
cx: &'a Context<'tcx>,
188188
) -> impl Display + 'a + Captures<'tcx> {
189189
display_fn(move |f| match &self.kind {
190-
clean::GenericParamDefKind::Lifetime { outlives } => {
190+
clean::GenericParamDefKind::Lifetime(param) => {
191191
write!(f, "{}", self.name)?;
192192

193-
if !outlives.is_empty() {
193+
if !param.outlives.is_empty() {
194194
f.write_str(": ")?;
195-
for (i, lt) in outlives.iter().enumerate() {
195+
for (i, lt) in param.outlives.iter().enumerate() {
196196
if i != 0 {
197197
f.write_str(" + ")?;
198198
}
@@ -202,26 +202,26 @@ impl clean::GenericParamDef {
202202

203203
Ok(())
204204
}
205-
clean::GenericParamDefKind::Type { bounds, default, .. } => {
205+
clean::GenericParamDefKind::Type(param) => {
206206
f.write_str(self.name.as_str())?;
207207

208-
if !bounds.is_empty() {
208+
if !param.bounds.is_empty() {
209209
f.write_str(": ")?;
210-
print_generic_bounds(bounds, cx).fmt(f)?;
210+
print_generic_bounds(&param.bounds, cx).fmt(f)?;
211211
}
212212

213-
if let Some(ref ty) = default {
213+
if let Some(ref ty) = param.default {
214214
f.write_str(" = ")?;
215215
ty.print(cx).fmt(f)?;
216216
}
217217

218218
Ok(())
219219
}
220-
clean::GenericParamDefKind::Const { ty, default, .. } => {
220+
clean::GenericParamDefKind::Const(param) => {
221221
write!(f, "const {}: ", self.name)?;
222-
ty.print(cx).fmt(f)?;
222+
param.ty.print(cx).fmt(f)?;
223223

224-
if let Some(default) = default {
224+
if let Some(default) = &param.default {
225225
f.write_str(" = ")?;
226226
if f.alternate() {
227227
write!(f, "{default}")?;

0 commit comments

Comments
 (0)