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

View File

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

View File

@ -208,7 +208,12 @@ pub(super) fn lower_generic_args(
if args.is_empty() && bindings.is_empty() { if args.is_empty() && bindings.is_empty() {
return None; 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) /// 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>, ret_type: Option<ast::RetType>,
) -> Option<GenericArgs> { ) -> Option<GenericArgs> {
let mut args = Vec::new(); let mut args = Vec::new();
let mut bindings = Vec::new();
let params = params?; let params = params?;
let mut param_types = Vec::new(); let mut param_types = Vec::new();
for param in params.params() { for param in params.params() {
@ -228,23 +232,23 @@ fn lower_generic_args_from_fn_path(
} }
let arg = GenericArg::Type(TypeRef::Tuple(param_types)); let arg = GenericArg::Type(TypeRef::Tuple(param_types));
args.push(arg); 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()); let type_ref = TypeRef::from_ast_opt(ctx, ret_type.ty());
bindings.push(AssociatedTypeBinding { Box::new([AssociatedTypeBinding {
name: name![Output], name: name![Output],
args: None, args: None,
type_ref: Some(type_ref), type_ref: Some(type_ref),
bounds: Box::default(), bounds: Box::default(),
}); }])
} else { } else {
// -> () // -> ()
let type_ref = TypeRef::Tuple(Vec::new()); let type_ref = TypeRef::Tuple(Vec::new());
bindings.push(AssociatedTypeBinding { Box::new([AssociatedTypeBinding {
name: name![Output], name: name![Output],
args: None, args: None,
type_ref: Some(type_ref), type_ref: Some(type_ref),
bounds: Box::default(), bounds: Box::default(),
}); }])
} };
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: true }) 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; first = false;
print_generic_arg(arg, buf)?; print_generic_arg(arg, buf)?;
} }
for binding in &generics.bindings { for binding in generics.bindings.iter() {
if !first { if !first {
write!(buf, ", ")?; write!(buf, ", ")?;
} }

View File

@ -301,7 +301,7 @@ impl TypeRef {
| crate::path::GenericArg::Lifetime(_) => {} | 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 { if let Some(type_ref) = &binding.type_ref {
go(type_ref, f); go(type_ref, f);
} }

View File

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

View File

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