Rollup merge of #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>`.
This commit is contained in:
commit
60beb5979b
1 changed files with 75 additions and 79 deletions
|
@ -33,18 +33,12 @@ use crate::errors;
|
||||||
|
|
||||||
#[extension(trait RegionExt)]
|
#[extension(trait RegionExt)]
|
||||||
impl ResolvedArg {
|
impl ResolvedArg {
|
||||||
fn early(param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) {
|
fn early(param: &GenericParam<'_>) -> ResolvedArg {
|
||||||
debug!("ResolvedArg::early: def_id={:?}", param.def_id);
|
ResolvedArg::EarlyBound(param.def_id)
|
||||||
(param.def_id, ResolvedArg::EarlyBound(param.def_id))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn late(idx: u32, param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) {
|
fn late(idx: u32, param: &GenericParam<'_>) -> ResolvedArg {
|
||||||
let depth = ty::INNERMOST;
|
ResolvedArg::LateBound(ty::INNERMOST, idx, param.def_id)
|
||||||
debug!(
|
|
||||||
"ResolvedArg::late: idx={:?}, param={:?} depth={:?} def_id={:?}",
|
|
||||||
idx, param, depth, param.def_id,
|
|
||||||
);
|
|
||||||
(param.def_id, ResolvedArg::LateBound(depth, idx, param.def_id))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn id(&self) -> Option<LocalDefId> {
|
fn id(&self) -> Option<LocalDefId> {
|
||||||
|
@ -282,12 +276,9 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
|
||||||
|
|
||||||
fn late_arg_as_bound_arg<'tcx>(
|
fn late_arg_as_bound_arg<'tcx>(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
arg: &ResolvedArg,
|
|
||||||
param: &GenericParam<'tcx>,
|
param: &GenericParam<'tcx>,
|
||||||
) -> ty::BoundVariableKind {
|
) -> ty::BoundVariableKind {
|
||||||
match arg {
|
let def_id = param.def_id.to_def_id();
|
||||||
ResolvedArg::LateBound(_, _, def_id) => {
|
|
||||||
let def_id = def_id.to_def_id();
|
|
||||||
let name = tcx.item_name(def_id);
|
let name = tcx.item_name(def_id);
|
||||||
match param.kind {
|
match param.kind {
|
||||||
GenericParamKind::Lifetime { .. } => {
|
GenericParamKind::Lifetime { .. } => {
|
||||||
|
@ -299,7 +290,19 @@ fn late_arg_as_bound_arg<'tcx>(
|
||||||
GenericParamKind::Const { .. } => ty::BoundVariableKind::Const,
|
GenericParamKind::Const { .. } => ty::BoundVariableKind::Const,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => bug!("{:?} is not a late argument", arg),
|
|
||||||
|
/// Turn a [`ty::GenericParamDef`] into a bound arg. Generally, this should only
|
||||||
|
/// be used when turning early-bound vars into late-bound vars when lowering
|
||||||
|
/// return type notation.
|
||||||
|
fn generic_param_def_as_bound_arg(param: &ty::GenericParamDef) -> ty::BoundVariableKind {
|
||||||
|
match param.kind {
|
||||||
|
ty::GenericParamDefKind::Lifetime => {
|
||||||
|
ty::BoundVariableKind::Region(ty::BoundRegionKind::BrNamed(param.def_id, param.name))
|
||||||
|
}
|
||||||
|
ty::GenericParamDefKind::Type { .. } => {
|
||||||
|
ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(param.def_id, param.name))
|
||||||
|
}
|
||||||
|
ty::GenericParamDefKind::Const { .. } => ty::BoundVariableKind::Const,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,10 +363,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
|
||||||
let mut bound_vars: FxIndexMap<LocalDefId, ResolvedArg> = FxIndexMap::default();
|
let mut bound_vars: FxIndexMap<LocalDefId, ResolvedArg> = FxIndexMap::default();
|
||||||
let binders_iter =
|
let binders_iter =
|
||||||
trait_ref.bound_generic_params.iter().enumerate().map(|(late_bound_idx, param)| {
|
trait_ref.bound_generic_params.iter().enumerate().map(|(late_bound_idx, param)| {
|
||||||
let pair = ResolvedArg::late(initial_bound_vars + late_bound_idx as u32, param);
|
let arg = ResolvedArg::late(initial_bound_vars + late_bound_idx as u32, param);
|
||||||
let r = late_arg_as_bound_arg(self.tcx, &pair.1, param);
|
bound_vars.insert(param.def_id, arg);
|
||||||
bound_vars.insert(pair.0, pair.1);
|
late_arg_as_bound_arg(self.tcx, param)
|
||||||
r
|
|
||||||
});
|
});
|
||||||
binders.extend(binders_iter);
|
binders.extend(binders_iter);
|
||||||
|
|
||||||
|
@ -458,9 +460,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(late_bound_idx, param)| {
|
.map(|(late_bound_idx, param)| {
|
||||||
let pair = ResolvedArg::late(late_bound_idx as u32, param);
|
(
|
||||||
let r = late_arg_as_bound_arg(self.tcx, &pair.1, param);
|
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
|
||||||
(pair, r)
|
late_arg_as_bound_arg(self.tcx, param),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.unzip();
|
.unzip();
|
||||||
|
|
||||||
|
@ -492,8 +495,8 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
|
||||||
let mut bound_vars = FxIndexMap::default();
|
let mut bound_vars = FxIndexMap::default();
|
||||||
debug!(?opaque.generics.params);
|
debug!(?opaque.generics.params);
|
||||||
for param in opaque.generics.params {
|
for param in opaque.generics.params {
|
||||||
let (def_id, reg) = ResolvedArg::early(param);
|
let arg = ResolvedArg::early(param);
|
||||||
bound_vars.insert(def_id, reg);
|
bound_vars.insert(param.def_id, arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
let hir_id = self.tcx.local_def_id_to_hir_id(opaque.def_id);
|
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> {
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(late_bound_idx, param)| {
|
.map(|(late_bound_idx, param)| {
|
||||||
let pair = ResolvedArg::late(late_bound_idx as u32, param);
|
(
|
||||||
let r = late_arg_as_bound_arg(self.tcx, &pair.1, param);
|
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
|
||||||
(pair, r)
|
late_arg_as_bound_arg(self.tcx, param),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.unzip();
|
.unzip();
|
||||||
|
|
||||||
|
@ -870,9 +874,10 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(late_bound_idx, param)| {
|
.map(|(late_bound_idx, param)| {
|
||||||
let pair = ResolvedArg::late(late_bound_idx as u32, param);
|
(
|
||||||
let r = late_arg_as_bound_arg(self.tcx, &pair.1, param);
|
(param.def_id, ResolvedArg::late(late_bound_idx as u32, param)),
|
||||||
(pair, r)
|
late_arg_as_bound_arg(self.tcx, param),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.unzip();
|
.unzip();
|
||||||
|
|
||||||
|
@ -1052,7 +1057,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
|
||||||
let bound_vars: FxIndexMap<LocalDefId, ResolvedArg> = generics
|
let bound_vars: FxIndexMap<LocalDefId, ResolvedArg> = generics
|
||||||
.params
|
.params
|
||||||
.iter()
|
.iter()
|
||||||
.map(|param| match param.kind {
|
.map(|param| {
|
||||||
|
(param.def_id, match param.kind {
|
||||||
GenericParamKind::Lifetime { .. } => {
|
GenericParamKind::Lifetime { .. } => {
|
||||||
if self.tcx.is_late_bound(param.hir_id) {
|
if self.tcx.is_late_bound(param.hir_id) {
|
||||||
let late_bound_idx = named_late_bound_vars;
|
let late_bound_idx = named_late_bound_vars;
|
||||||
|
@ -1066,6 +1072,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
|
||||||
ResolvedArg::early(param)
|
ResolvedArg::early(param)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let binders: Vec<_> = generics
|
let binders: Vec<_> = generics
|
||||||
|
@ -1075,11 +1082,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
|
||||||
matches!(param.kind, GenericParamKind::Lifetime { .. })
|
matches!(param.kind, GenericParamKind::Lifetime { .. })
|
||||||
&& self.tcx.is_late_bound(param.hir_id)
|
&& self.tcx.is_late_bound(param.hir_id)
|
||||||
})
|
})
|
||||||
.enumerate()
|
.map(|param| late_arg_as_bound_arg(self.tcx, param))
|
||||||
.map(|(late_bound_idx, param)| {
|
|
||||||
let pair = ResolvedArg::late(late_bound_idx as u32, param);
|
|
||||||
late_arg_as_bound_arg(self.tcx, &pair.1, param)
|
|
||||||
})
|
|
||||||
.collect();
|
.collect();
|
||||||
self.record_late_bound_vars(hir_id, binders);
|
self.record_late_bound_vars(hir_id, binders);
|
||||||
let scope = Scope::Binder {
|
let scope = Scope::Binder {
|
||||||
|
@ -1096,7 +1099,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
|
||||||
where
|
where
|
||||||
F: for<'b, 'c> FnOnce(&'b mut BoundVarContext<'c, 'tcx>),
|
F: for<'b, 'c> FnOnce(&'b mut BoundVarContext<'c, 'tcx>),
|
||||||
{
|
{
|
||||||
let bound_vars = generics.params.iter().map(ResolvedArg::early).collect();
|
let bound_vars =
|
||||||
|
generics.params.iter().map(|param| (param.def_id, ResolvedArg::early(param))).collect();
|
||||||
self.record_late_bound_vars(hir_id, vec![]);
|
self.record_late_bound_vars(hir_id, vec![]);
|
||||||
let scope = Scope::Binder {
|
let scope = Scope::Binder {
|
||||||
hir_id,
|
hir_id,
|
||||||
|
@ -1639,17 +1643,13 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
|
||||||
constraint.ident,
|
constraint.ident,
|
||||||
ty::AssocKind::Fn,
|
ty::AssocKind::Fn,
|
||||||
) {
|
) {
|
||||||
bound_vars.extend(self.tcx.generics_of(assoc_fn.def_id).own_params.iter().map(
|
bound_vars.extend(
|
||||||
|param| match param.kind {
|
self.tcx
|
||||||
ty::GenericParamDefKind::Lifetime => ty::BoundVariableKind::Region(
|
.generics_of(assoc_fn.def_id)
|
||||||
ty::BoundRegionKind::BrNamed(param.def_id, param.name),
|
.own_params
|
||||||
),
|
.iter()
|
||||||
ty::GenericParamDefKind::Type { .. } => ty::BoundVariableKind::Ty(
|
.map(|param| generic_param_def_as_bound_arg(param)),
|
||||||
ty::BoundTyKind::Param(param.def_id, param.name),
|
);
|
||||||
),
|
|
||||||
ty::GenericParamDefKind::Const { .. } => ty::BoundVariableKind::Const,
|
|
||||||
},
|
|
||||||
));
|
|
||||||
bound_vars.extend(
|
bound_vars.extend(
|
||||||
self.tcx.fn_sig(assoc_fn.def_id).instantiate_identity().bound_vars(),
|
self.tcx.fn_sig(assoc_fn.def_id).instantiate_identity().bound_vars(),
|
||||||
);
|
);
|
||||||
|
@ -1968,17 +1968,13 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
|
||||||
// Append the early-bound vars on the function, and then the late-bound ones.
|
// Append the early-bound vars on the function, and then the late-bound ones.
|
||||||
// We actually turn type parameters into higher-ranked types here, but we
|
// We actually turn type parameters into higher-ranked types here, but we
|
||||||
// deny them later in HIR lowering.
|
// deny them later in HIR lowering.
|
||||||
bound_vars.extend(self.tcx.generics_of(item_def_id).own_params.iter().map(|param| {
|
bound_vars.extend(
|
||||||
match param.kind {
|
self.tcx
|
||||||
ty::GenericParamDefKind::Lifetime => ty::BoundVariableKind::Region(
|
.generics_of(item_def_id)
|
||||||
ty::BoundRegionKind::BrNamed(param.def_id, param.name),
|
.own_params
|
||||||
),
|
.iter()
|
||||||
ty::GenericParamDefKind::Type { .. } => {
|
.map(|param| generic_param_def_as_bound_arg(param)),
|
||||||
ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(param.def_id, param.name))
|
);
|
||||||
}
|
|
||||||
ty::GenericParamDefKind::Const { .. } => ty::BoundVariableKind::Const,
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
bound_vars.extend(self.tcx.fn_sig(item_def_id).instantiate_identity().bound_vars());
|
bound_vars.extend(self.tcx.fn_sig(item_def_id).instantiate_identity().bound_vars());
|
||||||
|
|
||||||
// SUBTLE: Stash the old bound vars onto the *item segment* before appending
|
// SUBTLE: Stash the old bound vars onto the *item segment* before appending
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue