Skip to content

Commit 3ed13b4

Browse files
authored
Merge pull request #19440 from Veykril/push-lotzuulstwpw
refactor: Replace custom `ThinVec` with `thin-vec` crate
2 parents dd2354c + dcd7423 commit 3ed13b4

File tree

15 files changed

+99
-543
lines changed

15 files changed

+99
-543
lines changed

Cargo.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir-def/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mbe.workspace = true
4343
cfg.workspace = true
4444
tt.workspace = true
4545
span.workspace = true
46+
thin-vec = "0.2.14"
4647

4748
[dev-dependencies]
4849
expect-test.workspace = true

crates/hir-def/src/generics.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ use hir_expand::{
1212
};
1313
use intern::sym;
1414
use la_arena::{Arena, RawIdx};
15-
use stdx::{
16-
impl_from,
17-
thin_vec::{EmptyOptimizedThinVec, ThinVec},
18-
};
15+
use stdx::impl_from;
1916
use syntax::ast::{self, HasGenericParams, HasName, HasTypeBounds};
17+
use thin_vec::ThinVec;
2018
use triomphe::Arc;
2119

2220
use crate::{
@@ -753,12 +751,17 @@ fn copy_type_ref(
753751
) -> TypeRefId {
754752
let result = match &from[type_ref] {
755753
TypeRef::Fn(fn_) => {
756-
let params = fn_.params().iter().map(|(name, param_type)| {
754+
let params = fn_.params.iter().map(|(name, param_type)| {
757755
(name.clone(), copy_type_ref(*param_type, from, from_source_map, to, to_source_map))
758756
});
759-
TypeRef::Fn(FnType::new(fn_.is_varargs(), fn_.is_unsafe(), fn_.abi().clone(), params))
757+
TypeRef::Fn(Box::new(FnType {
758+
params: params.collect(),
759+
is_varargs: fn_.is_varargs,
760+
is_unsafe: fn_.is_unsafe,
761+
abi: fn_.abi.clone(),
762+
}))
760763
}
761-
TypeRef::Tuple(types) => TypeRef::Tuple(EmptyOptimizedThinVec::from_iter(
764+
TypeRef::Tuple(types) => TypeRef::Tuple(ThinVec::from_iter(
762765
types.iter().map(|&t| copy_type_ref(t, from, from_source_map, to, to_source_map)),
763766
)),
764767
&TypeRef::RawPtr(type_ref, mutbl) => TypeRef::RawPtr(
@@ -817,13 +820,17 @@ fn copy_path(
817820
Path::BarePath(mod_path) => Path::BarePath(mod_path.clone()),
818821
Path::Normal(path) => {
819822
let type_anchor = path
820-
.type_anchor()
823+
.type_anchor
821824
.map(|type_ref| copy_type_ref(type_ref, from, from_source_map, to, to_source_map));
822-
let mod_path = path.mod_path().clone();
823-
let generic_args = path.generic_args().iter().map(|generic_args| {
825+
let mod_path = path.mod_path.clone();
826+
let generic_args = path.generic_args.iter().map(|generic_args| {
824827
copy_generic_args(generic_args, from, from_source_map, to, to_source_map)
825828
});
826-
Path::Normal(NormalPath::new(type_anchor, mod_path, generic_args))
829+
Path::Normal(Box::new(NormalPath {
830+
generic_args: generic_args.collect(),
831+
type_anchor,
832+
mod_path,
833+
}))
827834
}
828835
Path::LangItem(lang_item, name) => Path::LangItem(*lang_item, name.clone()),
829836
}
@@ -879,7 +886,7 @@ fn copy_type_bounds<'a>(
879886
from_source_map: &'a TypesSourceMap,
880887
to: &'a mut TypesMap,
881888
to_source_map: &'a mut TypesSourceMap,
882-
) -> impl stdx::thin_vec::TrustedLen<Item = TypeBound> + 'a {
889+
) -> impl Iterator<Item = TypeBound> + 'a {
883890
bounds.iter().map(|bound| copy_type_bound(bound, from, from_source_map, to, to_source_map))
884891
}
885892

crates/hir-def/src/hir/type_ref.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use hir_expand::{
1212
use intern::{Symbol, sym};
1313
use la_arena::{Arena, ArenaMap, Idx};
1414
use span::Edition;
15-
use stdx::thin_vec::{EmptyOptimizedThinVec, ThinVec, thin_vec_with_header_struct};
1615
use syntax::{
1716
AstPtr,
1817
ast::{self, HasGenericArgs, HasName, IsString},
1918
};
19+
use thin_vec::ThinVec;
2020

2121
use crate::{
2222
SyntheticSyntax,
@@ -120,13 +120,12 @@ impl TraitRef {
120120
}
121121
}
122122

123-
thin_vec_with_header_struct! {
124-
pub new(pub(crate)) struct FnType, FnTypeHeader {
125-
pub params: [(Option<Name>, TypeRefId)],
126-
pub is_varargs: bool,
127-
pub is_unsafe: bool,
128-
pub abi: Option<Symbol>; ref,
129-
}
123+
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
124+
pub struct FnType {
125+
pub params: Box<[(Option<Name>, TypeRefId)]>,
126+
pub is_varargs: bool,
127+
pub is_unsafe: bool,
128+
pub abi: Option<Symbol>,
130129
}
131130

132131
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
@@ -148,14 +147,14 @@ pub struct RefType {
148147
pub enum TypeRef {
149148
Never,
150149
Placeholder,
151-
Tuple(EmptyOptimizedThinVec<TypeRefId>),
150+
Tuple(ThinVec<TypeRefId>),
152151
Path(Path),
153152
RawPtr(TypeRefId, Mutability),
154153
Reference(Box<RefType>),
155154
Array(Box<ArrayType>),
156155
Slice(TypeRefId),
157156
/// A fn pointer. Last element of the vector is the return type.
158-
Fn(FnType),
157+
Fn(Box<FnType>),
159158
ImplTrait(ThinVec<TypeBound>),
160159
DynTrait(ThinVec<TypeBound>),
161160
Macro(AstId<ast::MacroCall>),
@@ -273,9 +272,9 @@ impl TypeRef {
273272
pub fn from_ast(ctx: &mut LowerCtx<'_>, node: ast::Type) -> TypeRefId {
274273
let ty = match &node {
275274
ast::Type::ParenType(inner) => return TypeRef::from_ast_opt(ctx, inner.ty()),
276-
ast::Type::TupleType(inner) => TypeRef::Tuple(EmptyOptimizedThinVec::from_iter(
277-
Vec::from_iter(inner.fields().map(|it| TypeRef::from_ast(ctx, it))),
278-
)),
275+
ast::Type::TupleType(inner) => TypeRef::Tuple(ThinVec::from_iter(Vec::from_iter(
276+
inner.fields().map(|it| TypeRef::from_ast(ctx, it)),
277+
))),
279278
ast::Type::NeverType(..) => TypeRef::Never,
280279
ast::Type::PathType(inner) => {
281280
// FIXME: Use `Path::from_src`
@@ -342,7 +341,12 @@ impl TypeRef {
342341

343342
let abi = inner.abi().map(lower_abi);
344343
params.push((None, ret_ty));
345-
TypeRef::Fn(FnType::new(is_varargs, inner.unsafe_token().is_some(), abi, params))
344+
TypeRef::Fn(Box::new(FnType {
345+
params: params.into(),
346+
is_varargs,
347+
is_unsafe: inner.unsafe_token().is_some(),
348+
abi,
349+
}))
346350
}
347351
// for types are close enough for our purposes to the inner type for now...
348352
ast::Type::ForType(inner) => return TypeRef::from_ast_opt(ctx, inner.ty()),
@@ -375,7 +379,7 @@ impl TypeRef {
375379
}
376380

377381
pub(crate) fn unit() -> TypeRef {
378-
TypeRef::Tuple(EmptyOptimizedThinVec::empty())
382+
TypeRef::Tuple(ThinVec::new())
379383
}
380384

381385
pub fn walk(this: TypeRefId, map: &TypesMap, f: &mut impl FnMut(&TypeRef)) {
@@ -386,7 +390,7 @@ impl TypeRef {
386390
f(type_ref);
387391
match type_ref {
388392
TypeRef::Fn(fn_) => {
389-
fn_.params().iter().for_each(|&(_, param_type)| go(param_type, f, map))
393+
fn_.params.iter().for_each(|&(_, param_type)| go(param_type, f, map))
390394
}
391395
TypeRef::Tuple(types) => types.iter().for_each(|&t| go(t, f, map)),
392396
TypeRef::RawPtr(type_ref, _) | TypeRef::Slice(type_ref) => go(*type_ref, f, map),

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use intern::{Symbol, sym};
1212
use la_arena::Arena;
1313
use rustc_hash::FxHashMap;
1414
use span::{AstIdMap, SyntaxContext};
15-
use stdx::thin_vec::ThinVec;
1615
use syntax::{
1716
AstNode,
1817
ast::{self, HasModuleItem, HasName, HasTypeBounds, IsString},
1918
};
19+
use thin_vec::ThinVec;
2020
use triomphe::Arc;
2121

2222
use crate::{

crates/hir-def/src/lower.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::{cell::OnceCell, mem};
33

44
use hir_expand::{AstId, HirFileId, InFile, span_map::SpanMap};
55
use span::{AstIdMap, AstIdNode, Edition, EditionedFileId, FileId, RealSpanMap};
6-
use stdx::thin_vec::ThinVec;
76
use syntax::ast;
7+
use thin_vec::ThinVec;
88
use triomphe::Arc;
99

1010
use crate::{

crates/hir-def/src/path.rs

+27-25
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use crate::{
1616
use hir_expand::name::Name;
1717
use intern::Interned;
1818
use span::Edition;
19-
use stdx::thin_vec::thin_vec_with_header_struct;
2019
use syntax::ast;
2120

2221
pub use hir_expand::mod_path::{ModPath, PathKind, path};
@@ -58,7 +57,7 @@ pub enum Path {
5857
/// this is not a problem since many more paths have generics than a type anchor).
5958
BarePath(Interned<ModPath>),
6059
/// `Path::Normal` will always have either generics or type anchor.
61-
Normal(NormalPath),
60+
Normal(Box<NormalPath>),
6261
/// A link to a lang item. It is used in desugaring of things like `it?`. We can show these
6362
/// links via a normal path since they might be private and not accessible in the usage place.
6463
LangItem(LangItemTarget, Option<Name>),
@@ -71,12 +70,11 @@ const _: () = {
7170
assert!(size_of::<Option<Path>>() == 16);
7271
};
7372

74-
thin_vec_with_header_struct! {
75-
pub new(pub(crate)) struct NormalPath, NormalPathHeader {
76-
pub generic_args: [Option<GenericArgs>],
77-
pub type_anchor: Option<TypeRefId>,
78-
pub mod_path: Interned<ModPath>; ref,
79-
}
73+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
74+
pub struct NormalPath {
75+
pub generic_args: Box<[Option<GenericArgs>]>,
76+
pub type_anchor: Option<TypeRefId>,
77+
pub mod_path: Interned<ModPath>,
8078
}
8179

8280
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -143,7 +141,11 @@ impl Path {
143141

144142
/// Converts a known mod path to `Path`.
145143
pub fn from_known_path(path: ModPath, generic_args: Vec<Option<GenericArgs>>) -> Path {
146-
Path::Normal(NormalPath::new(None, Interned::new(path), generic_args))
144+
Path::Normal(Box::new(NormalPath {
145+
generic_args: generic_args.into_boxed_slice(),
146+
type_anchor: None,
147+
mod_path: Interned::new(path),
148+
}))
147149
}
148150

149151
/// Converts a known mod path to `Path`.
@@ -155,23 +157,23 @@ impl Path {
155157
pub fn kind(&self) -> &PathKind {
156158
match self {
157159
Path::BarePath(mod_path) => &mod_path.kind,
158-
Path::Normal(path) => &path.mod_path().kind,
160+
Path::Normal(path) => &path.mod_path.kind,
159161
Path::LangItem(..) => &PathKind::Abs,
160162
}
161163
}
162164

163165
#[inline]
164166
pub fn type_anchor(&self) -> Option<TypeRefId> {
165167
match self {
166-
Path::Normal(path) => path.type_anchor(),
168+
Path::Normal(path) => path.type_anchor,
167169
Path::LangItem(..) | Path::BarePath(_) => None,
168170
}
169171
}
170172

171173
#[inline]
172174
pub fn generic_args(&self) -> Option<&[Option<GenericArgs>]> {
173175
match self {
174-
Path::Normal(path) => Some(path.generic_args()),
176+
Path::Normal(path) => Some(&path.generic_args),
175177
Path::LangItem(..) | Path::BarePath(_) => None,
176178
}
177179
}
@@ -182,8 +184,8 @@ impl Path {
182184
PathSegments { segments: mod_path.segments(), generic_args: None }
183185
}
184186
Path::Normal(path) => PathSegments {
185-
segments: path.mod_path().segments(),
186-
generic_args: Some(path.generic_args()),
187+
segments: path.mod_path.segments(),
188+
generic_args: Some(&path.generic_args),
187189
},
188190
Path::LangItem(_, seg) => PathSegments { segments: seg.as_slice(), generic_args: None },
189191
}
@@ -192,7 +194,7 @@ impl Path {
192194
pub fn mod_path(&self) -> Option<&ModPath> {
193195
match self {
194196
Path::BarePath(mod_path) => Some(mod_path),
195-
Path::Normal(path) => Some(path.mod_path()),
197+
Path::Normal(path) => Some(&path.mod_path),
196198
Path::LangItem(..) => None,
197199
}
198200
}
@@ -209,12 +211,12 @@ impl Path {
209211
))))
210212
}
211213
Path::Normal(path) => {
212-
let mod_path = path.mod_path();
214+
let mod_path = &path.mod_path;
213215
if mod_path.is_ident() {
214216
return None;
215217
}
216-
let type_anchor = path.type_anchor();
217-
let generic_args = path.generic_args();
218+
let type_anchor = path.type_anchor;
219+
let generic_args = &path.generic_args;
218220
let qualifier_mod_path = Interned::new(ModPath::from_segments(
219221
mod_path.kind,
220222
mod_path.segments()[..mod_path.segments().len() - 1].iter().cloned(),
@@ -223,11 +225,11 @@ impl Path {
223225
if type_anchor.is_none() && qualifier_generic_args.iter().all(|it| it.is_none()) {
224226
Some(Path::BarePath(qualifier_mod_path))
225227
} else {
226-
Some(Path::Normal(NormalPath::new(
228+
Some(Path::Normal(Box::new(NormalPath {
227229
type_anchor,
228-
qualifier_mod_path,
229-
qualifier_generic_args.iter().cloned(),
230-
)))
230+
mod_path: qualifier_mod_path,
231+
generic_args: qualifier_generic_args.iter().cloned().collect(),
232+
})))
231233
}
232234
}
233235
Path::LangItem(..) => None,
@@ -238,9 +240,9 @@ impl Path {
238240
match self {
239241
Path::BarePath(mod_path) => mod_path.is_Self(),
240242
Path::Normal(path) => {
241-
path.type_anchor().is_none()
242-
&& path.mod_path().is_Self()
243-
&& path.generic_args().iter().all(|args| args.is_none())
243+
path.type_anchor.is_none()
244+
&& path.mod_path.is_Self()
245+
&& path.generic_args.iter().all(|args| args.is_none())
244246
}
245247
Path::LangItem(..) => false,
246248
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use hir_expand::{
99
name::{AsName, Name},
1010
};
1111
use intern::{Interned, sym};
12-
use stdx::thin_vec::EmptyOptimizedThinVec;
1312
use syntax::ast::{self, AstNode, HasGenericArgs, HasTypeBounds};
13+
use thin_vec::ThinVec;
1414

1515
use crate::{
1616
path::{
@@ -213,7 +213,11 @@ pub(super) fn lower_path(ctx: &mut LowerCtx<'_>, mut path: ast::Path) -> Option<
213213
if type_anchor.is_none() && generic_args.is_empty() {
214214
return Some(Path::BarePath(mod_path));
215215
} else {
216-
return Some(Path::Normal(NormalPath::new(type_anchor, mod_path, generic_args)));
216+
return Some(Path::Normal(Box::new(NormalPath {
217+
generic_args: generic_args.into_boxed_slice(),
218+
type_anchor,
219+
mod_path,
220+
})));
217221
}
218222

219223
fn qualifier(path: &ast::Path) -> Option<ast::Path> {
@@ -344,7 +348,7 @@ fn lower_generic_args_from_fn_path(
344348
param_types.push(type_ref);
345349
}
346350
let args = Box::new([GenericArg::Type(
347-
ctx.alloc_type_ref_desugared(TypeRef::Tuple(EmptyOptimizedThinVec::from_iter(param_types))),
351+
ctx.alloc_type_ref_desugared(TypeRef::Tuple(ThinVec::from_iter(param_types))),
348352
)]);
349353
let bindings = if let Some(ret_type) = ret_type {
350354
let type_ref = TypeRef::from_ast_opt(ctx, ret_type.ty());

0 commit comments

Comments
 (0)