Skip to content

Commit 60beb59

Browse files
authored
Rollup merge of rust-lang#132043 - compiler-errors:simplify-rbv, r=cjgillot
Simplify param handling in `resolve_bound_vars` I always found the flow of the `ResolvedArg` constructors to be a bit confusing; turns out they're also kinda redundantly passing around their data, too. Also, deduplicate some code handling early-bound var to late-bound var conversion between return type notation's two styles: `where <T as Trait>::method(..): Bound` and `where T: Trait<method(..): Bound>`.
2 parents 5f5c243 + 1b7a91e commit 60beb59

File tree

1 file changed

+75
-79
lines changed

1 file changed

+75
-79
lines changed

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+75-79
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,33 @@ 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,
291+
}
292+
}
293+
294+
/// Turn a [`ty::GenericParamDef`] into a bound arg. Generally, this should only
295+
/// be used when turning early-bound vars into late-bound vars when lowering
296+
/// return type notation.
297+
fn generic_param_def_as_bound_arg(param: &ty::GenericParamDef) -> ty::BoundVariableKind {
298+
match param.kind {
299+
ty::GenericParamDefKind::Lifetime => {
300+
ty::BoundVariableKind::Region(ty::BoundRegionKind::BrNamed(param.def_id, param.name))
301+
}
302+
ty::GenericParamDefKind::Type { .. } => {
303+
ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(param.def_id, param.name))
304+
}
305+
ty::GenericParamDefKind::Const { .. } => ty::BoundVariableKind::Const,
303306
}
304307
}
305308

@@ -360,10 +363,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
360363
let mut bound_vars: FxIndexMap<LocalDefId, ResolvedArg> = FxIndexMap::default();
361364
let binders_iter =
362365
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
366+
let arg = ResolvedArg::late(initial_bound_vars + late_bound_idx as u32, param);
367+
bound_vars.insert(param.def_id, arg);
368+
late_arg_as_bound_arg(self.tcx, param)
367369
});
368370
binders.extend(binders_iter);
369371

@@ -458,9 +460,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
458460
.iter()
459461
.enumerate()
460462
.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)
463+
(
464+
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
465+
late_arg_as_bound_arg(self.tcx, param),
466+
)
464467
})
465468
.unzip();
466469

@@ -492,8 +495,8 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
492495
let mut bound_vars = FxIndexMap::default();
493496
debug!(?opaque.generics.params);
494497
for param in opaque.generics.params {
495-
let (def_id, reg) = ResolvedArg::early(param);
496-
bound_vars.insert(def_id, reg);
498+
let arg = ResolvedArg::early(param);
499+
bound_vars.insert(param.def_id, arg);
497500
}
498501

499502
let hir_id = self.tcx.local_def_id_to_hir_id(opaque.def_id);
@@ -618,9 +621,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
618621
.iter()
619622
.enumerate()
620623
.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)
624+
(
625+
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
626+
late_arg_as_bound_arg(self.tcx, param),
627+
)
624628
})
625629
.unzip();
626630

@@ -870,9 +874,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
870874
.iter()
871875
.enumerate()
872876
.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)
877+
(
878+
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
879+
late_arg_as_bound_arg(self.tcx, param),
880+
)
876881
})
877882
.unzip();
878883

@@ -1052,19 +1057,21 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
10521057
let bound_vars: FxIndexMap<LocalDefId, ResolvedArg> = generics
10531058
.params
10541059
.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 {
1060+
.map(|param| {
1061+
(param.def_id, match param.kind {
1062+
GenericParamKind::Lifetime { .. } => {
1063+
if self.tcx.is_late_bound(param.hir_id) {
1064+
let late_bound_idx = named_late_bound_vars;
1065+
named_late_bound_vars += 1;
1066+
ResolvedArg::late(late_bound_idx, param)
1067+
} else {
1068+
ResolvedArg::early(param)
1069+
}
1070+
}
1071+
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
10621072
ResolvedArg::early(param)
10631073
}
1064-
}
1065-
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
1066-
ResolvedArg::early(param)
1067-
}
1074+
})
10681075
})
10691076
.collect();
10701077

@@ -1075,11 +1082,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
10751082
matches!(param.kind, GenericParamKind::Lifetime { .. })
10761083
&& self.tcx.is_late_bound(param.hir_id)
10771084
})
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-
})
1085+
.map(|param| late_arg_as_bound_arg(self.tcx, param))
10831086
.collect();
10841087
self.record_late_bound_vars(hir_id, binders);
10851088
let scope = Scope::Binder {
@@ -1096,7 +1099,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
10961099
where
10971100
F: for<'b, 'c> FnOnce(&'b mut BoundVarContext<'c, 'tcx>),
10981101
{
1099-
let bound_vars = generics.params.iter().map(ResolvedArg::early).collect();
1102+
let bound_vars =
1103+
generics.params.iter().map(|param| (param.def_id, ResolvedArg::early(param))).collect();
11001104
self.record_late_bound_vars(hir_id, vec![]);
11011105
let scope = Scope::Binder {
11021106
hir_id,
@@ -1639,17 +1643,13 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
16391643
constraint.ident,
16401644
ty::AssocKind::Fn,
16411645
) {
1642-
bound_vars.extend(self.tcx.generics_of(assoc_fn.def_id).own_params.iter().map(
1643-
|param| match param.kind {
1644-
ty::GenericParamDefKind::Lifetime => ty::BoundVariableKind::Region(
1645-
ty::BoundRegionKind::BrNamed(param.def_id, param.name),
1646-
),
1647-
ty::GenericParamDefKind::Type { .. } => ty::BoundVariableKind::Ty(
1648-
ty::BoundTyKind::Param(param.def_id, param.name),
1649-
),
1650-
ty::GenericParamDefKind::Const { .. } => ty::BoundVariableKind::Const,
1651-
},
1652-
));
1646+
bound_vars.extend(
1647+
self.tcx
1648+
.generics_of(assoc_fn.def_id)
1649+
.own_params
1650+
.iter()
1651+
.map(|param| generic_param_def_as_bound_arg(param)),
1652+
);
16531653
bound_vars.extend(
16541654
self.tcx.fn_sig(assoc_fn.def_id).instantiate_identity().bound_vars(),
16551655
);
@@ -1968,17 +1968,13 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
19681968
// Append the early-bound vars on the function, and then the late-bound ones.
19691969
// We actually turn type parameters into higher-ranked types here, but we
19701970
// deny them later in HIR lowering.
1971-
bound_vars.extend(self.tcx.generics_of(item_def_id).own_params.iter().map(|param| {
1972-
match param.kind {
1973-
ty::GenericParamDefKind::Lifetime => ty::BoundVariableKind::Region(
1974-
ty::BoundRegionKind::BrNamed(param.def_id, param.name),
1975-
),
1976-
ty::GenericParamDefKind::Type { .. } => {
1977-
ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(param.def_id, param.name))
1978-
}
1979-
ty::GenericParamDefKind::Const { .. } => ty::BoundVariableKind::Const,
1980-
}
1981-
}));
1971+
bound_vars.extend(
1972+
self.tcx
1973+
.generics_of(item_def_id)
1974+
.own_params
1975+
.iter()
1976+
.map(|param| generic_param_def_as_bound_arg(param)),
1977+
);
19821978
bound_vars.extend(self.tcx.fn_sig(item_def_id).instantiate_identity().bound_vars());
19831979

19841980
// SUBTLE: Stash the old bound vars onto the *item segment* before appending

0 commit comments

Comments
 (0)