Skip to content

Commit 61698eb

Browse files
committed
Refactorings: better self ty creation, avoid needless cloning of params, code cleanup
1 parent 1ede08a commit 61698eb

3 files changed

Lines changed: 33 additions & 50 deletions

File tree

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
7878
&mut self,
7979
item_id: NodeId,
8080
span: Span,
81-
mut params: ThinVec<GenericParam>,
81+
params: &mut ThinVec<GenericParam>,
8282
) -> &'hir hir::Generics<'hir> {
83-
for p in &mut params {
83+
for p in params.iter_mut() {
8484
// We want to create completely new params, so we generate
8585
// a new id, otherwise assertions will be triggered.
8686
p.id = self.next_node_id();
@@ -337,7 +337,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
337337
) -> Option<Generics> {
338338
let id = if let Some(id) = id { id } else { return None };
339339

340-
// If args are user-specified we still maybe need to add self
340+
// If args are user-specified we still maybe need to add self.
341341
let mut generics = if user_specified {
342342
None
343343
} else {
@@ -387,10 +387,10 @@ impl<'hir> HirOrAstGenerics<'hir> {
387387
) -> &mut Self {
388388
match self {
389389
HirOrAstGenerics::Ast(generics) => {
390-
let mut process_params = |generics: &Option<Generics>| {
391-
generics.as_ref().map(|g| {
392-
ctx.lower_delegation_generic_params(item_id, span, g.params.clone())
393-
})
390+
let mut process_params = |generics: &mut Option<Generics>| {
391+
generics
392+
.as_mut()
393+
.map(|g| ctx.lower_delegation_generic_params(item_id, span, &mut g.params))
394394
};
395395

396396
let hir_generics = match generics {

compiler/rustc_hir_analysis/src/delegation.rs

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::ty::{
1010
self, EarlyBinder, GenericPredicates, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
1111
TypeVisitableExt,
1212
};
13-
use rustc_span::{ErrorGuaranteed, Span};
13+
use rustc_span::{ErrorGuaranteed, Span, kw};
1414

1515
type RemapTable = FxHashMap<u32, u32>;
1616

@@ -185,7 +185,7 @@ fn create_mapping<'tcx>(
185185
args_index += 1;
186186
}
187187

188-
// If self after lifetimes insert mapping, relying that self is at 0 in sig parent
188+
// If self after lifetimes insert mapping, relying that self is at 0 in sig parent.
189189
if matches!(self_pos_kind, SelfPositionKind::AfterLifetimes) {
190190
mapping.insert(0, args_index as u32);
191191
args_index += 1;
@@ -277,35 +277,25 @@ pub(crate) fn get_delegation_self_ty<'tcx>(
277277
let (caller_kind, callee_kind) = get_caller_and_callee_kind(tcx, delegation_id, sig_id);
278278

279279
match (caller_kind, callee_kind) {
280-
(FnKind::Free, FnKind::Free) => None,
281-
282280
(FnKind::Free, FnKind::AssocTrait)
283281
| (FnKind::AssocInherentImpl, FnKind::Free)
282+
| (FnKind::Free, FnKind::Free)
284283
| (FnKind::AssocTrait, FnKind::Free)
285284
| (FnKind::AssocTrait, FnKind::AssocTrait) => {
286285
match create_self_position_kind(caller_kind, callee_kind) {
287286
SelfPositionKind::None => None,
288287
SelfPositionKind::AfterLifetimes => {
289-
ty::GenericArgs::identity_for_item(tcx, delegation_id)
290-
.iter()
291-
.skip_while(|a| a.as_region().is_some())
292-
.next()
293-
.and_then(|a| a.as_type())
288+
// Both sig parent and child lifetimes are in included in this count.
289+
Some(tcx.generics_of(delegation_id).own_counts().lifetimes)
294290
}
295-
SelfPositionKind::Zero => ty::GenericArgs::identity_for_item(tcx, delegation_id)
296-
.first()
297-
.and_then(|a| a.as_type()),
291+
SelfPositionKind::Zero => Some(0),
298292
}
293+
.map(|self_index| Ty::new_param(tcx, self_index as u32, kw::SelfUpper))
299294
}
300295

301-
(FnKind::AssocTraitImpl, FnKind::AssocTrait) => {
302-
create_trait_impl_to_trait_parent_args(tcx, delegation_id)
303-
.first()
304-
.and_then(|a| a.as_type())
305-
}
306-
307-
(FnKind::AssocInherentImpl, FnKind::AssocTrait) => {
308-
Some(create_inherent_impl_to_trait_self_ty(tcx, delegation_id))
296+
(FnKind::AssocTraitImpl, FnKind::AssocTrait)
297+
| (FnKind::AssocInherentImpl, FnKind::AssocTrait) => {
298+
Some(create_parent_self_ty(tcx, delegation_id))
309299
}
310300

311301
unsupported_caller_callee_kinds!() => unreachable!(),
@@ -320,10 +310,7 @@ fn create_trait_impl_to_trait_parent_args<'tcx>(
320310
tcx.impl_trait_header(parent).trait_ref.instantiate_identity().args
321311
}
322312

323-
fn create_inherent_impl_to_trait_self_ty<'tcx>(
324-
tcx: TyCtxt<'tcx>,
325-
delegation_id: LocalDefId,
326-
) -> Ty<'tcx> {
313+
fn create_parent_self_ty<'tcx>(tcx: TyCtxt<'tcx>, delegation_id: LocalDefId) -> Ty<'tcx> {
327314
let parent = tcx.parent(delegation_id.into());
328315
tcx.type_of(parent).instantiate_identity()
329316
}
@@ -373,7 +360,7 @@ fn create_generic_args<'tcx>(
373360
}
374361

375362
(FnKind::AssocInherentImpl, FnKind::AssocTrait) => {
376-
let self_ty = create_inherent_impl_to_trait_self_ty(tcx, delegation_id);
363+
let self_ty = create_parent_self_ty(tcx, delegation_id);
377364

378365
tcx.mk_args_from_iter(
379366
std::iter::once(ty::GenericArg::from(self_ty)).chain(delegation_args.iter()),
@@ -436,20 +423,18 @@ fn create_generic_args<'tcx>(
436423
}
437424

438425
new_args.extend_from_slice(&child_args[child_lifetimes_count..]);
439-
} else {
440-
if !parent_args.is_empty() {
441-
let child_args = &delegation_args[delegation_parent_args_count..];
426+
} else if !parent_args.is_empty() {
427+
let child_args = &delegation_args[delegation_parent_args_count..];
442428

443-
let child_lifetimes_count =
444-
child_args.iter().take_while(|a| a.as_region().is_some()).count();
445-
446-
for i in 0..child_lifetimes_count {
447-
new_args.insert(lifetimes_end_pos + i, child_args[i]);
448-
}
429+
let child_lifetimes_count =
430+
child_args.iter().take_while(|a| a.as_region().is_some()).count();
449431

450-
let skip_self = matches!(self_pos_kind, SelfPositionKind::AfterLifetimes);
451-
new_args.extend(&child_args[child_lifetimes_count + skip_self as usize..]);
432+
for i in 0..child_lifetimes_count {
433+
new_args.insert(lifetimes_end_pos + i, child_args[i]);
452434
}
435+
436+
let skip_self = matches!(self_pos_kind, SelfPositionKind::AfterLifetimes);
437+
new_args.extend(&child_args[child_lifetimes_count + skip_self as usize..]);
453438
}
454439

455440
new_args

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,14 +2948,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
29482948
let child_args = get_segment(info.child_args_segment_id).map(|(segment, def_id)| {
29492949
let parent_args = if let Some(parent_args) = parent_args {
29502950
parent_args
2951+
} else if let Some(parent) = self.tcx().opt_parent(def_id)
2952+
&& matches!(self.tcx().def_kind(parent), DefKind::Trait)
2953+
{
2954+
ty::GenericArgs::identity_for_item(self.tcx(), parent).as_slice()
29512955
} else {
2952-
if let Some(parent) = self.tcx().opt_parent(def_id)
2953-
&& matches!(self.tcx().def_kind(parent), DefKind::Trait)
2954-
{
2955-
ty::GenericArgs::identity_for_item(self.tcx(), parent).as_slice()
2956-
} else {
2957-
&[]
2958-
}
2956+
&[]
29592957
};
29602958

29612959
// FIXME(fn_delegation): execute this call with silenced dcx, as now warnings are duplicated, remove

0 commit comments

Comments
 (0)