Pull out into helper function

This commit is contained in:
Michael Goulet 2024-09-21 14:19:03 -04:00
parent 2dacf7ac61
commit 149bd877de

View file

@ -45,32 +45,64 @@ fn associated_type_bounds<'tcx>(
let item_trait_ref = ty::TraitRef::identity(tcx, tcx.parent(assoc_item_def_id.to_def_id())); let item_trait_ref = ty::TraitRef::identity(tcx, tcx.parent(assoc_item_def_id.to_def_id()));
let bounds_from_parent = let bounds_from_parent =
trait_predicates.predicates.iter().copied().filter_map(|(pred, span)| { trait_predicates.predicates.iter().copied().filter_map(|(clause, span)| {
let mut clause_ty = match pred.kind().skip_binder() { remap_gat_vars_and_recurse_into_nested_projections(
tcx,
filter,
item_trait_ref,
assoc_item_def_id,
span,
clause,
)
});
let all_bounds = tcx.arena.alloc_from_iter(bounds.clauses(tcx).chain(bounds_from_parent));
debug!(
"associated_type_bounds({}) = {:?}",
tcx.def_path_str(assoc_item_def_id.to_def_id()),
all_bounds
);
assert_only_contains_predicates_from(filter, all_bounds, item_ty);
all_bounds
}
/// The code below is quite involved, so let me explain.
///
/// We loop here, because we also want to collect vars for nested associated items as
/// well. For example, given a clause like `Self::A::B`, we want to add that to the
/// item bounds for `A`, so that we may use that bound in the case that `Self::A::B` is
/// rigid.
///
/// Secondly, regarding bound vars, when we see a where clause that mentions a GAT
/// like `for<'a, ...> Self::Assoc<'a, ...>: Bound<'b, ...>`, we want to turn that into
/// an item bound on the GAT, where all of the GAT args are substituted with the GAT's
/// param regions, and then keep all of the other late-bound vars in the bound around.
/// We need to "compress" the binder so that it doesn't mention any of those vars that
/// were mapped to params.
fn remap_gat_vars_and_recurse_into_nested_projections<'tcx>(
tcx: TyCtxt<'tcx>,
filter: PredicateFilter,
item_trait_ref: ty::TraitRef<'tcx>,
assoc_item_def_id: LocalDefId,
span: Span,
clause: ty::Clause<'tcx>,
) -> Option<(ty::Clause<'tcx>, Span)> {
let mut clause_ty = match clause.kind().skip_binder() {
ty::ClauseKind::Trait(tr) => tr.self_ty(), ty::ClauseKind::Trait(tr) => tr.self_ty(),
ty::ClauseKind::Projection(proj) => proj.projection_term.self_ty(), ty::ClauseKind::Projection(proj) => proj.projection_term.self_ty(),
ty::ClauseKind::TypeOutlives(outlives) => outlives.0, ty::ClauseKind::TypeOutlives(outlives) => outlives.0,
_ => return None, _ => return None,
}; };
// The code below is quite involved, so let me explain.
//
// We loop here, because we also want to collect vars for nested associated items as
// well. For example, given a clause like `Self::A::B`, we want to add that to the
// item bounds for `A`, so that we may use that bound in the case that `Self::A::B` is
// rigid.
//
// Secondly, regarding bound vars, when we see a where clause that mentions a GAT
// like `for<'a, ...> Self::Assoc<'a, ...>: Bound<'b, ...>`, we want to turn that into
// an item bound on the GAT, where all of the GAT args are substituted with the GAT's
// param regions, and then keep all of the other late-bound vars in the bound around.
// We need to "compress" the binder so that it doesn't mention any of those vars that
// were mapped to params.
let gat_vars = loop { let gat_vars = loop {
if let ty::Alias(ty::Projection, alias_ty) = *clause_ty.kind() { if let ty::Alias(ty::Projection, alias_ty) = *clause_ty.kind() {
if alias_ty.trait_ref(tcx) == item_trait_ref if alias_ty.trait_ref(tcx) == item_trait_ref
&& alias_ty.def_id == assoc_item_def_id.to_def_id() && alias_ty.def_id == assoc_item_def_id.to_def_id()
{ {
// We have found the GAT in question...
// Return the vars, since we may need to remap them.
break &alias_ty.args[item_trait_ref.args.len()..]; break &alias_ty.args[item_trait_ref.args.len()..];
} else { } else {
// Only collect *self* type bounds if the filter is for self. // Only collect *self* type bounds if the filter is for self.
@ -78,8 +110,7 @@ fn associated_type_bounds<'tcx>(
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => { PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => {
return None; return None;
} }
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => { PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {}
}
} }
clause_ty = alias_ty.self_ty(); clause_ty = alias_ty.self_ty();
@ -89,9 +120,10 @@ fn associated_type_bounds<'tcx>(
return None; return None;
}; };
// Special-case: No GAT vars, no mapping needed. // Special-case: No GAT vars, no mapping needed.
if gat_vars.is_empty() { if gat_vars.is_empty() {
return Some((pred, span)); return Some((clause, span));
} }
// First, check that all of the GAT args are substituted with a unique late-bound arg. // First, check that all of the GAT args are substituted with a unique late-bound arg.
@ -130,34 +162,15 @@ fn associated_type_bounds<'tcx>(
// Finally, map all of the args in the GAT to the params we expect, and compress // Finally, map all of the args in the GAT to the params we expect, and compress
// the remaining late-bound vars so that they count up from var 0. // the remaining late-bound vars so that they count up from var 0.
let mut folder = MapAndCompressBoundVars { let mut folder =
tcx, MapAndCompressBoundVars { tcx, binder: ty::INNERMOST, still_bound_vars: vec![], mapping };
binder: ty::INNERMOST, let pred = clause.kind().skip_binder().fold_with(&mut folder);
still_bound_vars: vec![],
mapping,
};
let pred = pred.kind().skip_binder().fold_with(&mut folder);
Some(( Some((
ty::Binder::bind_with_vars( ty::Binder::bind_with_vars(pred, tcx.mk_bound_variable_kinds(&folder.still_bound_vars))
pred,
tcx.mk_bound_variable_kinds(&folder.still_bound_vars),
)
.upcast(tcx), .upcast(tcx),
span, span,
)) ))
});
let all_bounds = tcx.arena.alloc_from_iter(bounds.clauses(tcx).chain(bounds_from_parent));
debug!(
"associated_type_bounds({}) = {:?}",
tcx.def_path_str(assoc_item_def_id.to_def_id()),
all_bounds
);
assert_only_contains_predicates_from(filter, all_bounds, item_ty);
all_bounds
} }
struct MapAndCompressBoundVars<'tcx> { struct MapAndCompressBoundVars<'tcx> {