Skip to content

Commit 0b52ec3

Browse files
Simplify confusing ResolvedArg constructors
1 parent 916e9ce commit 0b52ec3

File tree

1 file changed

+46
-57
lines changed

1 file changed

+46
-57
lines changed

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,12 @@ use crate::errors;
3333

3434
#[extension(trait RegionExt)]
3535
impl ResolvedArg {
36-
fn early(param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) {
37-
debug!("ResolvedArg::early: def_id={:?}", param.def_id);
38-
(param.def_id, ResolvedArg::EarlyBound(param.def_id))
36+
fn early(param: &GenericParam<'_>) -> ResolvedArg {
37+
ResolvedArg::EarlyBound(param.def_id)
3938
}
4039

41-
fn late(idx: u32, param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) {
42-
let depth = ty::INNERMOST;
43-
debug!(
44-
"ResolvedArg::late: idx={:?}, param={:?} depth={:?} def_id={:?}",
45-
idx, param, depth, param.def_id,
46-
);
47-
(param.def_id, ResolvedArg::LateBound(depth, idx, param.def_id))
40+
fn late(idx: u32, param: &GenericParam<'_>) -> ResolvedArg {
41+
ResolvedArg::LateBound(ty::INNERMOST, idx, param.def_id)
4842
}
4943

5044
fn id(&self) -> Option<LocalDefId> {
@@ -282,24 +276,18 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
282276

283277
fn late_arg_as_bound_arg<'tcx>(
284278
tcx: TyCtxt<'tcx>,
285-
arg: &ResolvedArg,
286279
param: &GenericParam<'tcx>,
287280
) -> ty::BoundVariableKind {
288-
match arg {
289-
ResolvedArg::LateBound(_, _, def_id) => {
290-
let def_id = def_id.to_def_id();
291-
let name = tcx.item_name(def_id);
292-
match param.kind {
293-
GenericParamKind::Lifetime { .. } => {
294-
ty::BoundVariableKind::Region(ty::BrNamed(def_id, name))
295-
}
296-
GenericParamKind::Type { .. } => {
297-
ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(def_id, name))
298-
}
299-
GenericParamKind::Const { .. } => ty::BoundVariableKind::Const,
300-
}
281+
let def_id = param.def_id.to_def_id();
282+
let name = tcx.item_name(def_id);
283+
match param.kind {
284+
GenericParamKind::Lifetime { .. } => {
285+
ty::BoundVariableKind::Region(ty::BrNamed(def_id, name))
301286
}
302-
_ => bug!("{:?} is not a late argument", arg),
287+
GenericParamKind::Type { .. } => {
288+
ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(def_id, name))
289+
}
290+
GenericParamKind::Const { .. } => ty::BoundVariableKind::Const,
303291
}
304292
}
305293

@@ -360,10 +348,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
360348
let mut bound_vars: FxIndexMap<LocalDefId, ResolvedArg> = FxIndexMap::default();
361349
let binders_iter =
362350
trait_ref.bound_generic_params.iter().enumerate().map(|(late_bound_idx, param)| {
363-
let pair = ResolvedArg::late(initial_bound_vars + late_bound_idx as u32, param);
364-
let r = late_arg_as_bound_arg(self.tcx, &pair.1, param);
365-
bound_vars.insert(pair.0, pair.1);
366-
r
351+
let arg = ResolvedArg::late(initial_bound_vars + late_bound_idx as u32, param);
352+
bound_vars.insert(param.def_id, arg);
353+
late_arg_as_bound_arg(self.tcx, param)
367354
});
368355
binders.extend(binders_iter);
369356

@@ -458,9 +445,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
458445
.iter()
459446
.enumerate()
460447
.map(|(late_bound_idx, param)| {
461-
let pair = ResolvedArg::late(late_bound_idx as u32, param);
462-
let r = late_arg_as_bound_arg(self.tcx, &pair.1, param);
463-
(pair, r)
448+
(
449+
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
450+
late_arg_as_bound_arg(self.tcx, param),
451+
)
464452
})
465453
.unzip();
466454

@@ -492,8 +480,8 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
492480
let mut bound_vars = FxIndexMap::default();
493481
debug!(?opaque.generics.params);
494482
for param in opaque.generics.params {
495-
let (def_id, reg) = ResolvedArg::early(param);
496-
bound_vars.insert(def_id, reg);
483+
let arg = ResolvedArg::early(param);
484+
bound_vars.insert(param.def_id, arg);
497485
}
498486

499487
let hir_id = self.tcx.local_def_id_to_hir_id(opaque.def_id);
@@ -618,9 +606,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
618606
.iter()
619607
.enumerate()
620608
.map(|(late_bound_idx, param)| {
621-
let pair = ResolvedArg::late(late_bound_idx as u32, param);
622-
let r = late_arg_as_bound_arg(self.tcx, &pair.1, param);
623-
(pair, r)
609+
(
610+
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
611+
late_arg_as_bound_arg(self.tcx, param),
612+
)
624613
})
625614
.unzip();
626615

@@ -870,9 +859,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
870859
.iter()
871860
.enumerate()
872861
.map(|(late_bound_idx, param)| {
873-
let pair = ResolvedArg::late(late_bound_idx as u32, param);
874-
let r = late_arg_as_bound_arg(self.tcx, &pair.1, param);
875-
(pair, r)
862+
(
863+
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
864+
late_arg_as_bound_arg(self.tcx, param),
865+
)
876866
})
877867
.unzip();
878868

@@ -1052,19 +1042,21 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
10521042
let bound_vars: FxIndexMap<LocalDefId, ResolvedArg> = generics
10531043
.params
10541044
.iter()
1055-
.map(|param| match param.kind {
1056-
GenericParamKind::Lifetime { .. } => {
1057-
if self.tcx.is_late_bound(param.hir_id) {
1058-
let late_bound_idx = named_late_bound_vars;
1059-
named_late_bound_vars += 1;
1060-
ResolvedArg::late(late_bound_idx, param)
1061-
} else {
1045+
.map(|param| {
1046+
(param.def_id, match param.kind {
1047+
GenericParamKind::Lifetime { .. } => {
1048+
if self.tcx.is_late_bound(param.hir_id) {
1049+
let late_bound_idx = named_late_bound_vars;
1050+
named_late_bound_vars += 1;
1051+
ResolvedArg::late(late_bound_idx, param)
1052+
} else {
1053+
ResolvedArg::early(param)
1054+
}
1055+
}
1056+
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
10621057
ResolvedArg::early(param)
10631058
}
1064-
}
1065-
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
1066-
ResolvedArg::early(param)
1067-
}
1059+
})
10681060
})
10691061
.collect();
10701062

@@ -1075,11 +1067,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
10751067
matches!(param.kind, GenericParamKind::Lifetime { .. })
10761068
&& self.tcx.is_late_bound(param.hir_id)
10771069
})
1078-
.enumerate()
1079-
.map(|(late_bound_idx, param)| {
1080-
let pair = ResolvedArg::late(late_bound_idx as u32, param);
1081-
late_arg_as_bound_arg(self.tcx, &pair.1, param)
1082-
})
1070+
.map(|param| late_arg_as_bound_arg(self.tcx, param))
10831071
.collect();
10841072
self.record_late_bound_vars(hir_id, binders);
10851073
let scope = Scope::Binder {
@@ -1096,7 +1084,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
10961084
where
10971085
F: for<'b, 'c> FnOnce(&'b mut BoundVarContext<'c, 'tcx>),
10981086
{
1099-
let bound_vars = generics.params.iter().map(ResolvedArg::early).collect();
1087+
let bound_vars =
1088+
generics.params.iter().map(|param| (param.def_id, ResolvedArg::early(param))).collect();
11001089
self.record_late_bound_vars(hir_id, vec![]);
11011090
let scope = Scope::Binder {
11021091
hir_id,

0 commit comments

Comments
 (0)