Skip to content

Commit 4b859e6

Browse files
committed
Don't arena-allocate extended generic args
1 parent 17ec134 commit 4b859e6

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ fn normalize<'tcx>(
19081908

19091909
fn clean_trait_object_lifetime_bound<'tcx>(
19101910
region: ty::Region<'tcx>,
1911-
container: Option<ContainerTy<'tcx>>,
1911+
container: Option<ContainerTy<'_, 'tcx>>,
19121912
preds: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
19131913
tcx: TyCtxt<'tcx>,
19141914
) -> Option<Lifetime> {
@@ -1937,7 +1937,7 @@ fn clean_trait_object_lifetime_bound<'tcx>(
19371937

19381938
fn can_elide_trait_object_lifetime_bound<'tcx>(
19391939
region: ty::Region<'tcx>,
1940-
container: Option<ContainerTy<'tcx>>,
1940+
container: Option<ContainerTy<'_, 'tcx>>,
19411941
preds: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
19421942
tcx: TyCtxt<'tcx>,
19431943
) -> bool {
@@ -1984,18 +1984,18 @@ fn can_elide_trait_object_lifetime_bound<'tcx>(
19841984
}
19851985

19861986
#[derive(Debug)]
1987-
pub(crate) enum ContainerTy<'tcx> {
1987+
pub(crate) enum ContainerTy<'a, 'tcx> {
19881988
Ref(ty::Region<'tcx>),
19891989
Regular {
19901990
ty: DefId,
19911991
/// The arguments *have* to contain an arg for the self type if the corresponding generics
19921992
/// contain a self type.
1993-
args: ty::Binder<'tcx, &'tcx [ty::GenericArg<'tcx>]>,
1993+
args: ty::Binder<'tcx, &'a [ty::GenericArg<'tcx>]>,
19941994
arg: usize,
19951995
},
19961996
}
19971997

1998-
impl<'tcx> ContainerTy<'tcx> {
1998+
impl<'tcx> ContainerTy<'_, 'tcx> {
19991999
fn object_lifetime_default(self, tcx: TyCtxt<'tcx>) -> ObjectLifetimeDefault<'tcx> {
20002000
match self {
20012001
Self::Ref(region) => ObjectLifetimeDefault::Arg(region),
@@ -2044,7 +2044,7 @@ pub(crate) fn clean_middle_ty<'tcx>(
20442044
bound_ty: ty::Binder<'tcx, Ty<'tcx>>,
20452045
cx: &mut DocContext<'tcx>,
20462046
parent_def_id: Option<DefId>,
2047-
container: Option<ContainerTy<'tcx>>,
2047+
container: Option<ContainerTy<'_, 'tcx>>,
20482048
) -> Type {
20492049
let bound_ty = normalize(cx, bound_ty).unwrap_or(bound_ty);
20502050
match *bound_ty.skip_binder().kind() {

src/librustdoc/clean/utils.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ pub(crate) fn clean_middle_generic_args<'tcx>(
8181
mut has_self: bool,
8282
owner: DefId,
8383
) -> Vec<GenericArg> {
84-
if args.skip_binder().is_empty() {
84+
let (args, bound_vars) = (args.skip_binder(), args.bound_vars());
85+
if args.is_empty() {
8586
// Fast path which avoids executing the query `generics_of`.
8687
return Vec::new();
8788
}
@@ -90,24 +91,21 @@ pub(crate) fn clean_middle_generic_args<'tcx>(
9091
let mut elision_has_failed_once_before = false;
9192

9293
let offset = if has_self { 1 } else { 0 };
93-
let mut clean_args = Vec::with_capacity(args.skip_binder().len().saturating_sub(offset));
94+
let mut clean_args = Vec::with_capacity(args.len().saturating_sub(offset));
9495

9596
// If the container is a trait object type, the arguments won't contain the self type but the
9697
// generics of the corresponding trait will. In such a case, prepend a dummy self type in order
9798
// to align the arguments and parameters for the iteration below and to enable us to correctly
9899
// instantiate the generic parameter default later.
99100
let args = if !has_self && generics.parent.is_none() && generics.has_self {
100101
has_self = true;
101-
// FIXME(fmease): Don't arena-allocate the args (blocked on further refactorings)!
102-
args.map_bound(|args| {
103-
&*cx.tcx.arena.alloc_from_iter(
104-
[cx.tcx.types.trait_object_dummy_self.into()]
105-
.into_iter()
106-
.chain(args.iter().copied()),
107-
)
108-
})
102+
[cx.tcx.types.trait_object_dummy_self.into()]
103+
.into_iter()
104+
.chain(args.iter().copied())
105+
.collect::<Vec<_>>()
106+
.into()
109107
} else {
110-
args
108+
std::borrow::Cow::from(args)
111109
};
112110

113111
let clean_arg = |(index, arg): (usize, &ty::GenericArg<'tcx>)| match arg.unpack() {
@@ -116,23 +114,28 @@ pub(crate) fn clean_middle_generic_args<'tcx>(
116114
}
117115
GenericArgKind::Type(_) if has_self && index == 0 => None,
118116
GenericArgKind::Type(ty) => {
117+
let ty = ty::Binder::bind_with_vars(ty, bound_vars);
118+
119119
if !elision_has_failed_once_before
120120
&& let Some(default) = generics.param_at(index, cx.tcx).default_value(cx.tcx)
121121
{
122-
let default = args.map_bound(|args| default.instantiate(cx.tcx, args).expect_ty());
123-
124-
if can_elide_generic_arg(args.rebind(ty), default) {
122+
let default = default.instantiate(cx.tcx, args.as_ref()).expect_ty();
123+
if can_elide_generic_arg(ty, ty.rebind(default)) {
125124
return None;
126125
}
127126

128127
elision_has_failed_once_before = true;
129128
}
130129

131130
Some(GenericArg::Type(clean_middle_ty(
132-
args.rebind(ty),
131+
ty,
133132
cx,
134133
None,
135-
Some(crate::clean::ContainerTy::Regular { ty: owner, args, arg: index }),
134+
Some(crate::clean::ContainerTy::Regular {
135+
ty: owner,
136+
args: ty.rebind(args.as_ref()),
137+
arg: index,
138+
}),
136139
)))
137140
}
138141
GenericArgKind::Const(ct) => {
@@ -142,24 +145,24 @@ pub(crate) fn clean_middle_generic_args<'tcx>(
142145
return None;
143146
}
144147

148+
let ct = ty::Binder::bind_with_vars(ct, bound_vars);
149+
145150
if !elision_has_failed_once_before
146151
&& let Some(default) = generics.param_at(index, cx.tcx).default_value(cx.tcx)
147152
{
148-
let default =
149-
args.map_bound(|args| default.instantiate(cx.tcx, args).expect_const());
150-
151-
if can_elide_generic_arg(args.rebind(ct), default) {
153+
let default = default.instantiate(cx.tcx, args.as_ref()).expect_const();
154+
if can_elide_generic_arg(ct, ct.rebind(default)) {
152155
return None;
153156
}
154157

155158
elision_has_failed_once_before = true;
156159
}
157160

158-
Some(GenericArg::Const(Box::new(clean_middle_const(args.rebind(ct), cx))))
161+
Some(GenericArg::Const(Box::new(clean_middle_const(ct, cx))))
159162
}
160163
};
161164

162-
clean_args.extend(args.skip_binder().iter().enumerate().rev().filter_map(clean_arg));
165+
clean_args.extend(args.iter().enumerate().rev().filter_map(clean_arg));
163166
clean_args.reverse();
164167
clean_args
165168
}

0 commit comments

Comments
 (0)