Skip to content

Commit b99fad4

Browse files
committed
Refactorings: better usage of SelfPositionKind, remove not needed conditions
1 parent 00fec6c commit b99fad4

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

compiler/rustc_hir_analysis/src/delegation.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ParamIndexRemapper<'tcx> {
6363
enum SelfPositionKind {
6464
AfterLifetimes,
6565
Zero,
66+
None,
6667
}
6768

6869
macro_rules! unsupported_caller_callee_kinds {
@@ -80,9 +81,9 @@ fn create_self_position_kind(caller_kind: FnKind, callee_kind: FnKind) -> SelfPo
8081
| (FnKind::AssocTrait, FnKind::AssocTrait)
8182
| (FnKind::AssocTrait, FnKind::Free) => SelfPositionKind::Zero,
8283

83-
unsupported_caller_callee_kinds!() => unreachable!(),
84+
(FnKind::Free, FnKind::AssocTrait) => SelfPositionKind::AfterLifetimes,
8485

85-
_ => SelfPositionKind::AfterLifetimes,
86+
_ => SelfPositionKind::None,
8687
}
8788
}
8889

@@ -146,10 +147,9 @@ fn create_mapping<'tcx>(
146147
let (caller_kind, callee_kind) = get_caller_and_callee_kind(tcx, def_id, sig_id);
147148
let self_pos_kind = create_self_position_kind(caller_kind, callee_kind);
148149
let is_self_at_zero = matches!(self_pos_kind, SelfPositionKind::Zero);
149-
let process_sig_parent_generics = matches!(callee_kind, FnKind::AssocTrait);
150150

151151
// Is self at zero? If so insert mapping, self in sig parent is always at 0.
152-
if is_self_at_zero && process_sig_parent_generics {
152+
if is_self_at_zero {
153153
mapping.insert(0, 0);
154154
}
155155

@@ -159,6 +159,8 @@ fn create_mapping<'tcx>(
159159
args_index += get_delegation_parent_args_count_without_self(tcx, def_id, sig_id);
160160

161161
let sig_generics = tcx.generics_of(sig_id);
162+
let process_sig_parent_generics = matches!(callee_kind, FnKind::AssocTrait);
163+
162164
if process_sig_parent_generics {
163165
for i in (sig_generics.has_self as usize)..sig_generics.parent_count {
164166
let param = sig_generics.param_at(i, tcx);
@@ -184,7 +186,7 @@ fn create_mapping<'tcx>(
184186
}
185187

186188
// If self after lifetimes insert mapping, relying that self is at 0 in sig parent
187-
if !is_self_at_zero && process_sig_parent_generics {
189+
if matches!(self_pos_kind, SelfPositionKind::AfterLifetimes) {
188190
mapping.insert(0, args_index as u32);
189191
args_index += 1;
190192
}
@@ -284,16 +286,20 @@ pub(crate) fn get_delegation_self_ty<'tcx>(
284286
| (FnKind::AssocInherentImpl, FnKind::Free)
285287
| (FnKind::AssocTrait, FnKind::Free)
286288
| (FnKind::AssocTrait, FnKind::AssocTrait) => {
287-
let args = ty::GenericArgs::identity_for_item(tcx, delegation_id);
288289
match create_self_position_kind(caller_kind, callee_kind) {
289-
SelfPositionKind::AfterLifetimes => args
290-
.iter()
291-
.skip_while(|a| a.as_region().is_some())
292-
.next()
290+
SelfPositionKind::None => None,
291+
SelfPositionKind::AfterLifetimes => {
292+
ty::GenericArgs::identity_for_item(tcx, delegation_id)
293+
.iter()
294+
.skip_while(|a| a.as_region().is_some())
295+
.next()
296+
.map(|a| a.as_type())
297+
.flatten()
298+
}
299+
SelfPositionKind::Zero => ty::GenericArgs::identity_for_item(tcx, delegation_id)
300+
.first()
293301
.map(|a| a.as_type())
294302
.flatten(),
295-
296-
SelfPositionKind::Zero => args.first().map(|a| a.as_type()).flatten(),
297303
}
298304
}
299305

@@ -347,11 +353,9 @@ fn create_generic_args<'tcx>(
347353
let args = match (caller_kind, callee_kind) {
348354
(FnKind::Free, FnKind::Free)
349355
| (FnKind::Free, FnKind::AssocTrait)
350-
| (FnKind::AssocInherentImpl, FnKind::Free) => delegation_args,
351-
352-
(FnKind::AssocTrait, FnKind::Free) | (FnKind::AssocTrait, FnKind::AssocTrait) => {
353-
delegation_args
354-
}
356+
| (FnKind::AssocInherentImpl, FnKind::Free)
357+
| (FnKind::AssocTrait, FnKind::Free)
358+
| (FnKind::AssocTrait, FnKind::AssocTrait) => delegation_args,
355359

356360
(FnKind::AssocTraitImpl, FnKind::AssocTrait) => {
357361
let parent = tcx.parent(delegation_id.into());
@@ -404,6 +408,8 @@ fn create_generic_args<'tcx>(
404408

405409
lifetimes_end_pos += deleg_parent_args_without_self_count;
406410
}
411+
// If we have parent args then we obtained them from trait, then self must be somewhere
412+
SelfPositionKind::None => unreachable!(),
407413
};
408414
} else {
409415
let self_impact = matches!(self_pos_kind, SelfPositionKind::Zero) as usize;

0 commit comments

Comments
 (0)