Auto merge of #14152 - Veykril:path-opt, r=Veykril

Replace some often empty `Vec`s with boxed slices
This commit is contained in:
bors 2023-02-14 16:58:58 +00:00
commit 37608f306c
7 changed files with 55 additions and 39 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: Vec::new(),
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

@ -49,7 +49,7 @@ pub struct Path {
/// also includes bindings of associated types, like in `Iterator<Item = Foo>`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericArgs {
pub args: Vec<GenericArg>,
pub args: Box<[GenericArg]>,
/// 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
/// `T` is actually a type parameter for the path `Trait` specifying the
@ -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,
@ -77,7 +77,7 @@ pub struct AssociatedTypeBinding {
/// Bounds for the associated type, like in `Iterator<Item:
/// SomeOtherTrait>`. (This is the unstable `associated_type_bounds`
/// feature.)
pub bounds: Vec<Interned<TypeBound>>,
pub bounds: Box<[Interned<TypeBound>]>,
}
/// A single generic argument.
@ -212,9 +212,9 @@ impl GenericArgs {
pub(crate) fn empty() -> GenericArgs {
GenericArgs {
args: Vec::new(),
args: Box::default(),
has_self_type: false,
bindings: Vec::new(),
bindings: Box::default(),
desugared_from_fn: false,
}
}

View File

@ -1,5 +1,7 @@
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene
use std::iter;
use crate::type_ref::ConstScalarOrPath;
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);
}
let self_type = GenericArg::Type(self_type);
// 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 mut args_inner = match last_segment {
Some(it) => it.as_ref().clone(),
None => GenericArgs::empty(),
};
args_inner.has_self_type = true;
args_inner.args.insert(0, GenericArg::Type(self_type));
*last_segment = Some(Interned::new(args_inner));
*last_segment = Some(Interned::new(match last_segment.take() {
Some(it) => GenericArgs {
args: iter::once(self_type)
.chain(it.args.iter().cloned())
.collect(),
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()
},
}));
}
}
}
@ -187,7 +200,7 @@ pub(super) fn lower_generic_args(
.map(|it| Interned::new(TypeBound::from_ast(lower_ctx, it)))
.collect()
} else {
Vec::new()
Box::default()
};
bindings.push(AssociatedTypeBinding { name, args, type_ref, bounds });
}
@ -208,7 +221,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: args.into_boxed_slice(),
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)
@ -218,33 +236,30 @@ fn lower_generic_args_from_fn_path(
params: Option<ast::ParamList>,
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() {
let type_ref = TypeRef::from_ast_opt(ctx, param.ty());
param_types.push(type_ref);
}
let arg = GenericArg::Type(TypeRef::Tuple(param_types));
args.push(arg);
if let Some(ret_type) = ret_type {
let args = Box::new([GenericArg::Type(TypeRef::Tuple(param_types))]);
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: Vec::new(),
});
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: Vec::new(),
});
}
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

@ -292,7 +292,7 @@ impl TypeRef {
}
for segment in path.segments().iter() {
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 {
crate::path::GenericArg::Type(type_ref) => {
go(type_ref, f);
@ -301,11 +301,11 @@ 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);
}
for bound in &binding.bounds {
for bound in binding.bounds.iter() {
match bound.as_ref() {
TypeBound::Path(path, _) | TypeBound::ForLifetime(_, path) => {
go_path(path, f)

View File

@ -1419,7 +1419,7 @@ impl HirDisplay for Path {
write!(f, "<")?;
let mut first = true;
for arg in &generic_args.args {
for arg in generic_args.args.iter() {
if first {
first = false;
if generic_args.has_self_type {
@ -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 {
@ -1445,7 +1445,7 @@ impl HirDisplay for Path {
}
None => {
write!(f, ": ")?;
f.write_joined(&binding.bounds, " + ")?;
f.write_joined(binding.bounds.iter(), " + ")?;
}
}
}

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,
@ -1068,7 +1068,7 @@ impl<'a> TyLoweringContext<'a> {
AliasEq { alias: AliasTy::Projection(projection_ty.clone()), ty };
preds.push(crate::wrap_empty_binders(WhereClause::AliasEq(alias_eq)));
}
for bound in &binding.bounds {
for bound in binding.bounds.iter() {
preds.extend(self.lower_type_bound(
bound,
TyKind::Alias(AliasTy::Projection(projection_ty.clone())).intern(Interner),