Slim down GenericArgs by one usize once more

This commit is contained in:
Lukas Wirth 2023-02-14 17:37:20 +01:00
parent 853ae1927d
commit 4aee911ce4
4 changed files with 26 additions and 15 deletions

View File

@ -49,7 +49,7 @@ pub struct Path {
/// also includes bindings of associated types, like in `Iterator<Item = Foo>`. /// also includes bindings of associated types, like in `Iterator<Item = Foo>`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericArgs { pub struct GenericArgs {
pub args: Vec<GenericArg>, pub args: Box<[GenericArg]>,
/// This specifies whether the args contain a Self type as the first /// This specifies whether the args contain a Self type as the first
/// element. This is the case for path segments like `<T as Trait>`, where /// element. This is the case for path segments like `<T as Trait>`, where
/// `T` is actually a type parameter for the path `Trait` specifying the /// `T` is actually a type parameter for the path `Trait` specifying the
@ -212,7 +212,7 @@ impl GenericArgs {
pub(crate) fn empty() -> GenericArgs { pub(crate) fn empty() -> GenericArgs {
GenericArgs { GenericArgs {
args: Vec::new(), args: Box::default(),
has_self_type: false, has_self_type: false,
bindings: Box::default(), bindings: Box::default(),
desugared_from_fn: false, desugared_from_fn: false,

View File

@ -1,5 +1,7 @@
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene //! Transforms syntax into `Path` objects, ideally with accounting for hygiene
use std::iter;
use crate::type_ref::ConstScalarOrPath; use crate::type_ref::ConstScalarOrPath;
use either::Either; use either::Either;
@ -86,15 +88,26 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path
generic_args.resize(segments.len(), None); generic_args.resize(segments.len(), None);
} }
let self_type = GenericArg::Type(self_type);
// Insert the type reference (T in the above example) as Self parameter for the trait // Insert the type reference (T in the above example) as Self parameter for the trait
let last_segment = generic_args.get_mut(segments.len() - num_segments)?; let last_segment = generic_args.get_mut(segments.len() - num_segments)?;
let mut args_inner = match last_segment { *last_segment = Some(Interned::new(match last_segment.take() {
Some(it) => it.as_ref().clone(), Some(it) => GenericArgs {
None => GenericArgs::empty(), args: iter::once(self_type)
}; .chain(it.args.iter().cloned())
args_inner.has_self_type = true; .collect(),
args_inner.args.insert(0, GenericArg::Type(self_type));
*last_segment = Some(Interned::new(args_inner)); has_self_type: true,
bindings: it.bindings.clone(),
desugared_from_fn: it.desugared_from_fn,
},
None => GenericArgs {
args: Box::new([self_type]),
has_self_type: true,
..GenericArgs::empty()
},
}));
} }
} }
} }
@ -209,7 +222,7 @@ pub(super) fn lower_generic_args(
return None; return None;
} }
Some(GenericArgs { Some(GenericArgs {
args, args: args.into_boxed_slice(),
has_self_type: false, has_self_type: false,
bindings: bindings.into_boxed_slice(), bindings: bindings.into_boxed_slice(),
desugared_from_fn: false, desugared_from_fn: false,
@ -223,15 +236,13 @@ fn lower_generic_args_from_fn_path(
params: Option<ast::ParamList>, params: Option<ast::ParamList>,
ret_type: Option<ast::RetType>, ret_type: Option<ast::RetType>,
) -> Option<GenericArgs> { ) -> Option<GenericArgs> {
let mut args = 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() {
let type_ref = TypeRef::from_ast_opt(ctx, param.ty()); let type_ref = TypeRef::from_ast_opt(ctx, param.ty());
param_types.push(type_ref); param_types.push(type_ref);
} }
let arg = GenericArg::Type(TypeRef::Tuple(param_types)); let args = Box::new([GenericArg::Type(TypeRef::Tuple(param_types))]);
args.push(arg);
let bindings = 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());
Box::new([AssociatedTypeBinding { Box::new([AssociatedTypeBinding {

View File

@ -292,7 +292,7 @@ impl TypeRef {
} }
for segment in path.segments().iter() { for segment in path.segments().iter() {
if let Some(args_and_bindings) = segment.args_and_bindings { if let Some(args_and_bindings) = segment.args_and_bindings {
for arg in &args_and_bindings.args { for arg in args_and_bindings.args.iter() {
match arg { match arg {
crate::path::GenericArg::Type(type_ref) => { crate::path::GenericArg::Type(type_ref) => {
go(type_ref, f); go(type_ref, f);

View File

@ -1419,7 +1419,7 @@ impl HirDisplay for Path {
write!(f, "<")?; write!(f, "<")?;
let mut first = true; let mut first = true;
for arg in &generic_args.args { for arg in generic_args.args.iter() {
if first { if first {
first = false; first = false;
if generic_args.has_self_type { if generic_args.has_self_type {