Slim down GenericArgs by one usize

This commit is contained in:
Lukas Wirth 2023-02-14 17:24:40 +01:00
parent 4c2aef650a
commit 853ae1927d
7 changed files with 22 additions and 17 deletions

View File

@ -659,15 +659,16 @@ fn desugar_future_path(orig: TypeRef) -> Path {
let path = path![core::future::Future];
let mut generic_args: Vec<_> =
std::iter::repeat(None).take(path.segments().len() - 1).collect();
let mut last = GenericArgs::empty();
let binding = AssociatedTypeBinding {
name: name![Output],
args: None,
type_ref: Some(orig),
bounds: Box::default(),
};
last.bindings.push(binding);
generic_args.push(Some(Interned::new(last)));
generic_args.push(Some(Interned::new(GenericArgs {
bindings: Box::new([binding]),
..GenericArgs::empty()
})));
Path::from_known_path(path, generic_args)
}

View File

@ -57,7 +57,7 @@ pub struct GenericArgs {
/// is left out.
pub has_self_type: bool,
/// Associated type bindings like in `Iterator<Item = T>`.
pub bindings: Vec<AssociatedTypeBinding>,
pub bindings: Box<[AssociatedTypeBinding]>,
/// Whether these generic args were desugared from `Trait(Arg) -> Output`
/// parenthesis notation typically used for the `Fn` traits.
pub desugared_from_fn: bool,
@ -214,7 +214,7 @@ impl GenericArgs {
GenericArgs {
args: Vec::new(),
has_self_type: false,
bindings: Vec::new(),
bindings: Box::default(),
desugared_from_fn: false,
}
}

View File

@ -208,7 +208,12 @@ pub(super) fn lower_generic_args(
if args.is_empty() && bindings.is_empty() {
return None;
}
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: false })
Some(GenericArgs {
args,
has_self_type: false,
bindings: bindings.into_boxed_slice(),
desugared_from_fn: false,
})
}
/// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
@ -219,7 +224,6 @@ fn lower_generic_args_from_fn_path(
ret_type: Option<ast::RetType>,
) -> Option<GenericArgs> {
let mut args = Vec::new();
let mut bindings = Vec::new();
let params = params?;
let mut param_types = Vec::new();
for param in params.params() {
@ -228,23 +232,23 @@ fn lower_generic_args_from_fn_path(
}
let arg = GenericArg::Type(TypeRef::Tuple(param_types));
args.push(arg);
if let Some(ret_type) = ret_type {
let bindings = if let Some(ret_type) = ret_type {
let type_ref = TypeRef::from_ast_opt(ctx, ret_type.ty());
bindings.push(AssociatedTypeBinding {
Box::new([AssociatedTypeBinding {
name: name![Output],
args: None,
type_ref: Some(type_ref),
bounds: Box::default(),
});
}])
} else {
// -> ()
let type_ref = TypeRef::Tuple(Vec::new());
bindings.push(AssociatedTypeBinding {
Box::new([AssociatedTypeBinding {
name: name![Output],
args: None,
type_ref: Some(type_ref),
bounds: Box::default(),
});
}
}])
};
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: true })
}

View File

@ -71,7 +71,7 @@ pub(crate) fn print_generic_args(generics: &GenericArgs, buf: &mut dyn Write) ->
first = false;
print_generic_arg(arg, buf)?;
}
for binding in &generics.bindings {
for binding in generics.bindings.iter() {
if !first {
write!(buf, ", ")?;
}

View File

@ -301,7 +301,7 @@ impl TypeRef {
| crate::path::GenericArg::Lifetime(_) => {}
}
}
for binding in &args_and_bindings.bindings {
for binding in args_and_bindings.bindings.iter() {
if let Some(type_ref) = &binding.type_ref {
go(type_ref, f);
}

View File

@ -1431,7 +1431,7 @@ impl HirDisplay for Path {
}
arg.hir_fmt(f)?;
}
for binding in &generic_args.bindings {
for binding in generic_args.bindings.iter() {
if first {
first = false;
} else {

View File

@ -1025,7 +1025,7 @@ impl<'a> TyLoweringContext<'a> {
last_segment
.into_iter()
.filter_map(|segment| segment.args_and_bindings)
.flat_map(|args_and_bindings| &args_and_bindings.bindings)
.flat_map(|args_and_bindings| args_and_bindings.bindings.iter())
.flat_map(move |binding| {
let found = associated_type_by_name_including_super_traits(
self.db,