Skip to content

Commit 4f2a67b

Browse files
committed
Auto merge of rust-lang#12513 - Veykril:ty-utils, r=Veykril
internal: Simplify `hir_ty::utils`
2 parents d513f65 + 9153f17 commit 4f2a67b

File tree

4 files changed

+48
-69
lines changed

4 files changed

+48
-69
lines changed

crates/hir-def/src/data.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct FunctionData {
2828
pub attrs: Attrs,
2929
pub visibility: RawVisibility,
3030
pub abi: Option<Interned<str>>,
31-
pub legacy_const_generics_indices: Vec<u32>,
31+
pub legacy_const_generics_indices: Box<[u32]>,
3232
flags: FnFlags,
3333
}
3434

@@ -131,7 +131,7 @@ impl FunctionData {
131131
}
132132
}
133133

134-
fn parse_rustc_legacy_const_generics(tt: &tt::Subtree) -> Vec<u32> {
134+
fn parse_rustc_legacy_const_generics(tt: &tt::Subtree) -> Box<[u32]> {
135135
let mut indices = Vec::new();
136136
for args in tt.token_trees.chunks(2) {
137137
match &args[0] {
@@ -150,7 +150,7 @@ fn parse_rustc_legacy_const_generics(tt: &tt::Subtree) -> Vec<u32> {
150150
}
151151
}
152152

153-
indices
153+
indices.into_boxed_slice()
154154
}
155155

156156
#[derive(Debug, Clone, PartialEq, Eq)]

crates/hir-def/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ impl From<TypeParamId> for TypeOrConstParamId {
356356
}
357357
}
358358

359-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
360359
/// A TypeOrConstParamId with an invariant that it actually belongs to a const
360+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
361361
pub struct ConstParamId(TypeOrConstParamId);
362362

363363
impl ConstParamId {

crates/hir-ty/src/infer/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,28 +1211,28 @@ impl<'a> InferenceContext<'a> {
12111211
}
12121212

12131213
/// Returns the argument indices to skip.
1214-
fn check_legacy_const_generics(&mut self, callee: Ty, args: &[ExprId]) -> Vec<u32> {
1214+
fn check_legacy_const_generics(&mut self, callee: Ty, args: &[ExprId]) -> Box<[u32]> {
12151215
let (func, subst) = match callee.kind(Interner) {
12161216
TyKind::FnDef(fn_id, subst) => {
12171217
let callable = CallableDefId::from_chalk(self.db, *fn_id);
12181218
let func = match callable {
12191219
CallableDefId::FunctionId(f) => f,
1220-
_ => return Vec::new(),
1220+
_ => return Default::default(),
12211221
};
12221222
(func, subst)
12231223
}
1224-
_ => return Vec::new(),
1224+
_ => return Default::default(),
12251225
};
12261226

12271227
let data = self.db.function_data(func);
12281228
if data.legacy_const_generics_indices.is_empty() {
1229-
return Vec::new();
1229+
return Default::default();
12301230
}
12311231

12321232
// only use legacy const generics if the param count matches with them
12331233
if data.params.len() + data.legacy_const_generics_indices.len() != args.len() {
12341234
if args.len() <= data.params.len() {
1235-
return Vec::new();
1235+
return Default::default();
12361236
} else {
12371237
// there are more parameters than there should be without legacy
12381238
// const params; use them

crates/hir-ty/src/utils.rs

Lines changed: 39 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,19 @@ pub(crate) fn generics(db: &dyn DefDatabase, def: GenericDefId) -> Generics {
177177
let parent_generics = parent_generic_def(db, def).map(|def| Box::new(generics(db, def)));
178178
if parent_generics.is_some() && matches!(def, GenericDefId::TypeAliasId(_)) {
179179
let params = db.generic_params(def);
180-
if params
181-
.type_or_consts
182-
.iter()
183-
.any(|(_, x)| matches!(x, TypeOrConstParamData::ConstParamData(_)))
184-
{
180+
let has_consts =
181+
params.iter().any(|(_, x)| matches!(x, TypeOrConstParamData::ConstParamData(_)));
182+
return if has_consts {
185183
// XXX: treat const generic associated types as not existing to avoid crashes (#11769)
186184
//
187185
// Chalk expects the inner associated type's parameters to come
188186
// *before*, not after the trait's generics as we've always done it.
189187
// Adapting to this requires a larger refactoring
190188
cov_mark::hit!(ignore_gats);
191-
return Generics { def, params: Interned::new(Default::default()), parent_generics };
189+
Generics { def, params: Interned::new(Default::default()), parent_generics }
192190
} else {
193-
return Generics { def, params, parent_generics };
194-
}
191+
Generics { def, params, parent_generics }
192+
};
195193
}
196194
Generics { def, params: db.generic_params(def), parent_generics }
197195
}
@@ -219,68 +217,46 @@ impl Generics {
219217
pub(crate) fn iter<'a>(
220218
&'a self,
221219
) -> impl DoubleEndedIterator<Item = (TypeOrConstParamId, &'a TypeOrConstParamData)> + 'a {
222-
self.parent_generics
223-
.as_ref()
220+
let to_toc_id = |it: &'a Generics| {
221+
move |(local_id, p)| (TypeOrConstParamId { parent: it.def, local_id }, p)
222+
};
223+
self.parent_generics()
224224
.into_iter()
225-
.flat_map(|it| {
226-
it.params
227-
.iter()
228-
.map(move |(local_id, p)| (TypeOrConstParamId { parent: it.def, local_id }, p))
229-
})
230-
.chain(
231-
self.params.iter().map(move |(local_id, p)| {
232-
(TypeOrConstParamId { parent: self.def, local_id }, p)
233-
}),
234-
)
225+
.flat_map(move |it| it.params.iter().map(to_toc_id(it)))
226+
.chain(self.params.iter().map(to_toc_id(self)))
235227
}
236228

237229
/// Iterator over types and const params of parent.
238230
pub(crate) fn iter_parent<'a>(
239231
&'a self,
240232
) -> impl Iterator<Item = (TypeOrConstParamId, &'a TypeOrConstParamData)> + 'a {
241-
self.parent_generics.as_ref().into_iter().flat_map(|it| {
242-
it.params
243-
.type_or_consts
244-
.iter()
245-
.map(move |(local_id, p)| (TypeOrConstParamId { parent: it.def, local_id }, p))
233+
self.parent_generics().into_iter().flat_map(|it| {
234+
let to_toc_id =
235+
move |(local_id, p)| (TypeOrConstParamId { parent: it.def, local_id }, p);
236+
it.params.iter().map(to_toc_id)
246237
})
247238
}
248239

249240
pub(crate) fn len(&self) -> usize {
250-
self.len_split().0
251-
}
252-
253-
/// (total, parents, child)
254-
pub(crate) fn len_split(&self) -> (usize, usize, usize) {
255-
let parent = self.parent_generics.as_ref().map_or(0, |p| p.len());
241+
let parent = self.parent_generics().map_or(0, Generics::len);
256242
let child = self.params.type_or_consts.len();
257-
(parent + child, parent, child)
243+
parent + child
258244
}
259245

260246
/// (parent total, self param, type param list, const param list, impl trait)
261247
pub(crate) fn provenance_split(&self) -> (usize, usize, usize, usize, usize) {
262-
let parent = self.parent_generics.as_ref().map_or(0, |p| p.len());
263-
let self_params = self
264-
.params
265-
.iter()
266-
.filter_map(|x| x.1.type_param())
267-
.filter(|p| p.provenance == TypeParamProvenance::TraitSelf)
268-
.count();
269-
let type_params = self
270-
.params
271-
.type_or_consts
272-
.iter()
273-
.filter_map(|x| x.1.type_param())
274-
.filter(|p| p.provenance == TypeParamProvenance::TypeParamList)
275-
.count();
248+
let ty_iter = || self.params.iter().filter_map(|x| x.1.type_param());
249+
250+
let self_params =
251+
ty_iter().filter(|p| p.provenance == TypeParamProvenance::TraitSelf).count();
252+
let type_params =
253+
ty_iter().filter(|p| p.provenance == TypeParamProvenance::TypeParamList).count();
254+
let impl_trait_params =
255+
ty_iter().filter(|p| p.provenance == TypeParamProvenance::ArgumentImplTrait).count();
276256
let const_params = self.params.iter().filter_map(|x| x.1.const_param()).count();
277-
let impl_trait_params = self
278-
.params
279-
.iter()
280-
.filter_map(|x| x.1.type_param())
281-
.filter(|p| p.provenance == TypeParamProvenance::ArgumentImplTrait)
282-
.count();
283-
(parent, self_params, type_params, const_params, impl_trait_params)
257+
258+
let parent_len = self.parent_generics().map_or(0, Generics::len);
259+
(parent_len, self_params, type_params, const_params, impl_trait_params)
284260
}
285261

286262
pub(crate) fn param_idx(&self, param: TypeOrConstParamId) -> Option<usize> {
@@ -291,18 +267,21 @@ impl Generics {
291267
if param.parent == self.def {
292268
let (idx, (_local_id, data)) = self
293269
.params
294-
.type_or_consts
295270
.iter()
296271
.enumerate()
297272
.find(|(_, (idx, _))| *idx == param.local_id)
298273
.unwrap();
299-
let (_total, parent_len, _child) = self.len_split();
274+
let parent_len = self.parent_generics().map_or(0, Generics::len);
300275
Some((parent_len + idx, data))
301276
} else {
302-
self.parent_generics.as_ref().and_then(|g| g.find_param(param))
277+
self.parent_generics().and_then(|g| g.find_param(param))
303278
}
304279
}
305280

281+
fn parent_generics(&self) -> Option<&Generics> {
282+
self.parent_generics.as_ref().map(|it| &**it)
283+
}
284+
306285
/// Returns a Substitution that replaces each parameter by a bound variable.
307286
pub(crate) fn bound_vars_subst(
308287
&self,
@@ -377,10 +356,10 @@ pub fn is_fn_unsafe_to_call(db: &dyn HirDatabase, func: FunctionId) -> bool {
377356
// Function in an `extern` block are always unsafe to call, except when it has
378357
// `"rust-intrinsic"` ABI there are a few exceptions.
379358
let id = block.lookup(db.upcast()).id;
380-
match id.item_tree(db.upcast())[id.value].abi.as_deref() {
381-
Some("rust-intrinsic") => is_intrinsic_fn_unsafe(&data.name),
382-
_ => true,
383-
}
359+
!matches!(
360+
id.item_tree(db.upcast())[id.value].abi.as_deref(),
361+
Some("rust-intrinsic") if !is_intrinsic_fn_unsafe(&data.name)
362+
)
384363
}
385364
_ => false,
386365
}

0 commit comments

Comments
 (0)