Skip to content

Commit 4aee911

Browse files
committed
Slim down GenericArgs by one usize once more
1 parent 853ae19 commit 4aee911

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

crates/hir-def/src/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub struct Path {
4949
/// also includes bindings of associated types, like in `Iterator<Item = Foo>`.
5050
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5151
pub struct GenericArgs {
52-
pub args: Vec<GenericArg>,
52+
pub args: Box<[GenericArg]>,
5353
/// This specifies whether the args contain a Self type as the first
5454
/// element. This is the case for path segments like `<T as Trait>`, where
5555
/// `T` is actually a type parameter for the path `Trait` specifying the
@@ -212,7 +212,7 @@ impl GenericArgs {
212212

213213
pub(crate) fn empty() -> GenericArgs {
214214
GenericArgs {
215-
args: Vec::new(),
215+
args: Box::default(),
216216
has_self_type: false,
217217
bindings: Box::default(),
218218
desugared_from_fn: false,

crates/hir-def/src/path/lower.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene
22
3+
use std::iter;
4+
35
use crate::type_ref::ConstScalarOrPath;
46

57
use either::Either;
@@ -86,15 +88,26 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path
8688
generic_args.resize(segments.len(), None);
8789
}
8890

91+
let self_type = GenericArg::Type(self_type);
92+
8993
// Insert the type reference (T in the above example) as Self parameter for the trait
9094
let last_segment = generic_args.get_mut(segments.len() - num_segments)?;
91-
let mut args_inner = match last_segment {
92-
Some(it) => it.as_ref().clone(),
93-
None => GenericArgs::empty(),
94-
};
95-
args_inner.has_self_type = true;
96-
args_inner.args.insert(0, GenericArg::Type(self_type));
97-
*last_segment = Some(Interned::new(args_inner));
95+
*last_segment = Some(Interned::new(match last_segment.take() {
96+
Some(it) => GenericArgs {
97+
args: iter::once(self_type)
98+
.chain(it.args.iter().cloned())
99+
.collect(),
100+
101+
has_self_type: true,
102+
bindings: it.bindings.clone(),
103+
desugared_from_fn: it.desugared_from_fn,
104+
},
105+
None => GenericArgs {
106+
args: Box::new([self_type]),
107+
has_self_type: true,
108+
..GenericArgs::empty()
109+
},
110+
}));
98111
}
99112
}
100113
}
@@ -209,7 +222,7 @@ pub(super) fn lower_generic_args(
209222
return None;
210223
}
211224
Some(GenericArgs {
212-
args,
225+
args: args.into_boxed_slice(),
213226
has_self_type: false,
214227
bindings: bindings.into_boxed_slice(),
215228
desugared_from_fn: false,
@@ -223,15 +236,13 @@ fn lower_generic_args_from_fn_path(
223236
params: Option<ast::ParamList>,
224237
ret_type: Option<ast::RetType>,
225238
) -> Option<GenericArgs> {
226-
let mut args = Vec::new();
227239
let params = params?;
228240
let mut param_types = Vec::new();
229241
for param in params.params() {
230242
let type_ref = TypeRef::from_ast_opt(ctx, param.ty());
231243
param_types.push(type_ref);
232244
}
233-
let arg = GenericArg::Type(TypeRef::Tuple(param_types));
234-
args.push(arg);
245+
let args = Box::new([GenericArg::Type(TypeRef::Tuple(param_types))]);
235246
let bindings = if let Some(ret_type) = ret_type {
236247
let type_ref = TypeRef::from_ast_opt(ctx, ret_type.ty());
237248
Box::new([AssociatedTypeBinding {

crates/hir-def/src/type_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl TypeRef {
292292
}
293293
for segment in path.segments().iter() {
294294
if let Some(args_and_bindings) = segment.args_and_bindings {
295-
for arg in &args_and_bindings.args {
295+
for arg in args_and_bindings.args.iter() {
296296
match arg {
297297
crate::path::GenericArg::Type(type_ref) => {
298298
go(type_ref, f);

crates/hir-ty/src/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ impl HirDisplay for Path {
14191419

14201420
write!(f, "<")?;
14211421
let mut first = true;
1422-
for arg in &generic_args.args {
1422+
for arg in generic_args.args.iter() {
14231423
if first {
14241424
first = false;
14251425
if generic_args.has_self_type {

0 commit comments

Comments
 (0)