Skip to content

Commit 00fec6c

Browse files
committed
Refactorings: return ParamIndexRemapper for less changes
1 parent afb17ae commit 00fec6c

1 file changed

Lines changed: 27 additions & 28 deletions

File tree

compiler/rustc_hir_analysis/src/delegation.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,52 @@ use rustc_middle::ty::{
1212
};
1313
use rustc_span::{ErrorGuaranteed, Span};
1414

15-
struct ExistingMappingFolder<'tcx> {
15+
type RemapTable = FxHashMap<u32, u32>;
16+
17+
struct ParamIndexRemapper<'tcx> {
1618
tcx: TyCtxt<'tcx>,
17-
mapping: FxHashMap<u32, u32>,
19+
remap_table: RemapTable,
1820
}
1921

20-
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ExistingMappingFolder<'tcx> {
22+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ParamIndexRemapper<'tcx> {
2123
fn cx(&self) -> TyCtxt<'tcx> {
2224
self.tcx
2325
}
2426

25-
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
26-
if let ty::ConstKind::Param(param) = ct.kind() {
27-
let index = self.mapping[&param.index];
28-
return ty::Const::new_param(self.tcx, ty::ParamConst::new(index, param.name));
29-
}
30-
31-
ct.super_fold_with(self)
32-
}
33-
3427
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
3528
if !ty.has_param() {
3629
return ty;
3730
}
3831

39-
if let ty::Param(param) = ty.kind() {
40-
return if let Some(index) = self.mapping.get(&param.index) {
41-
Ty::new_param(self.tcx, *index, param.name)
42-
} else {
43-
ty
44-
};
32+
if let ty::Param(param) = ty.kind()
33+
&& let Some(index) = self.remap_table.get(&param.index)
34+
{
35+
return Ty::new_param(self.tcx, *index, param.name);
4536
}
46-
4737
ty.super_fold_with(self)
4838
}
4939

5040
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
51-
if let ty::ReEarlyParam(param) = r.kind() {
52-
let index = self.mapping[&param.index];
53-
41+
if let ty::ReEarlyParam(param) = r.kind()
42+
&& let Some(index) = self.remap_table.get(&param.index).copied()
43+
{
5444
return ty::Region::new_early_param(
5545
self.tcx,
5646
ty::EarlyParamRegion { index, name: param.name },
5747
);
5848
}
59-
6049
r
6150
}
51+
52+
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
53+
if let ty::ConstKind::Param(param) = ct.kind()
54+
&& let Some(idx) = self.remap_table.get(&param.index)
55+
{
56+
let param = ty::ParamConst::new(*idx, param.name);
57+
return ty::Const::new_param(self.tcx, param);
58+
}
59+
ct.super_fold_with(self)
60+
}
6261
}
6362

6463
enum SelfPositionKind {
@@ -459,14 +458,14 @@ pub(crate) fn inherit_predicates_for_delegation_item<'tcx>(
459458
tcx: TyCtxt<'tcx>,
460459
preds: Vec<(ty::Clause<'tcx>, Span)>,
461460
args: Vec<ty::GenericArg<'tcx>>,
462-
folder: ExistingMappingFolder<'tcx>,
461+
folder: ParamIndexRemapper<'tcx>,
463462
}
464463

465464
impl<'tcx> PredicatesCollector<'tcx> {
466465
fn new(
467466
tcx: TyCtxt<'tcx>,
468467
args: Vec<ty::GenericArg<'tcx>>,
469-
folder: ExistingMappingFolder<'tcx>,
468+
folder: ParamIndexRemapper<'tcx>,
470469
) -> PredicatesCollector<'tcx> {
471470
PredicatesCollector { tcx, preds: vec![], args, folder }
472471
}
@@ -540,11 +539,11 @@ fn create_folder_and_args<'tcx>(
540539
sig_id: DefId,
541540
parent_args: &'tcx [ty::GenericArg<'tcx>],
542541
child_args: &'tcx [ty::GenericArg<'tcx>],
543-
) -> (ExistingMappingFolder<'tcx>, Vec<ty::GenericArg<'tcx>>) {
542+
) -> (ParamIndexRemapper<'tcx>, Vec<ty::GenericArg<'tcx>>) {
544543
let args = create_generic_args(tcx, sig_id, def_id, parent_args, child_args);
545-
let mapping = create_mapping(tcx, sig_id, def_id, &args);
544+
let remap_table = create_mapping(tcx, sig_id, def_id, &args);
546545

547-
(ExistingMappingFolder { tcx, mapping }, args)
546+
(ParamIndexRemapper { tcx, remap_table }, args)
548547
}
549548

550549
fn check_constraints<'tcx>(

0 commit comments

Comments
 (0)