Skip to content

Commit 1b7a91e

Browse files
Deduplicate handling of early-bound params in RTN
1 parent 0b52ec3 commit 1b7a91e

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+29-22
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ fn late_arg_as_bound_arg<'tcx>(
291291
}
292292
}
293293

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,
306+
}
307+
}
308+
294309
impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
295310
/// Returns the binders in scope and the type of `Binder` that should be created for a poly trait ref.
296311
fn poly_trait_ref_binder_info(&mut self) -> (Vec<ty::BoundVariableKind>, BinderScopeType) {
@@ -1628,17 +1643,13 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
16281643
constraint.ident,
16291644
ty::AssocKind::Fn,
16301645
) {
1631-
bound_vars.extend(self.tcx.generics_of(assoc_fn.def_id).own_params.iter().map(
1632-
|param| match param.kind {
1633-
ty::GenericParamDefKind::Lifetime => ty::BoundVariableKind::Region(
1634-
ty::BoundRegionKind::BrNamed(param.def_id, param.name),
1635-
),
1636-
ty::GenericParamDefKind::Type { .. } => ty::BoundVariableKind::Ty(
1637-
ty::BoundTyKind::Param(param.def_id, param.name),
1638-
),
1639-
ty::GenericParamDefKind::Const { .. } => ty::BoundVariableKind::Const,
1640-
},
1641-
));
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+
);
16421653
bound_vars.extend(
16431654
self.tcx.fn_sig(assoc_fn.def_id).instantiate_identity().bound_vars(),
16441655
);
@@ -1957,17 +1968,13 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
19571968
// Append the early-bound vars on the function, and then the late-bound ones.
19581969
// We actually turn type parameters into higher-ranked types here, but we
19591970
// deny them later in HIR lowering.
1960-
bound_vars.extend(self.tcx.generics_of(item_def_id).own_params.iter().map(|param| {
1961-
match param.kind {
1962-
ty::GenericParamDefKind::Lifetime => ty::BoundVariableKind::Region(
1963-
ty::BoundRegionKind::BrNamed(param.def_id, param.name),
1964-
),
1965-
ty::GenericParamDefKind::Type { .. } => {
1966-
ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(param.def_id, param.name))
1967-
}
1968-
ty::GenericParamDefKind::Const { .. } => ty::BoundVariableKind::Const,
1969-
}
1970-
}));
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+
);
19711978
bound_vars.extend(self.tcx.fn_sig(item_def_id).instantiate_identity().bound_vars());
19721979

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

0 commit comments

Comments
 (0)