1
Fork 0

Refactor where predicates, and reserve for attributes support

This commit is contained in:
Frank King 2024-11-25 16:38:35 +08:00
parent 67a8c64259
commit 161221da9e
44 changed files with 394 additions and 408 deletions

View file

@ -5312,9 +5312,10 @@ fn point_at_assoc_type_restriction<G: EmissionGuarantee>(
};
let name = tcx.item_name(proj.projection_term.def_id);
let mut predicates = generics.predicates.iter().peekable();
let mut prev: Option<&hir::WhereBoundPredicate<'_>> = None;
let mut prev: Option<(&hir::WhereBoundPredicate<'_>, Span)> = None;
while let Some(pred) = predicates.next() {
let hir::WherePredicate::BoundPredicate(pred) = pred else {
let curr_span = pred.span;
let hir::WherePredicateKind::BoundPredicate(pred) = pred.kind else {
continue;
};
let mut bounds = pred.bounds.iter();
@ -5340,8 +5341,8 @@ fn point_at_assoc_type_restriction<G: EmissionGuarantee>(
.iter()
.filter(|p| {
matches!(
p,
hir::WherePredicate::BoundPredicate(p)
p.kind,
hir::WherePredicateKind::BoundPredicate(p)
if hir::PredicateOrigin::WhereClause == p.origin
)
})
@ -5351,20 +5352,21 @@ fn point_at_assoc_type_restriction<G: EmissionGuarantee>(
// There's only one `where` bound, that needs to be removed. Remove the whole
// `where` clause.
generics.where_clause_span
} else if let Some(hir::WherePredicate::BoundPredicate(next)) = predicates.peek()
} else if let Some(next_pred) = predicates.peek()
&& let hir::WherePredicateKind::BoundPredicate(next) = next_pred.kind
&& pred.origin == next.origin
{
// There's another bound, include the comma for the current one.
pred.span.until(next.span)
} else if let Some(prev) = prev
curr_span.until(next_pred.span)
} else if let Some((prev, prev_span)) = prev
&& pred.origin == prev.origin
{
// Last bound, try to remove the previous comma.
prev.span.shrink_to_hi().to(pred.span)
prev_span.shrink_to_hi().to(curr_span)
} else if pred.origin == hir::PredicateOrigin::WhereClause {
pred.span.with_hi(generics.where_clause_span.hi())
curr_span.with_hi(generics.where_clause_span.hi())
} else {
pred.span
curr_span
};
err.span_suggestion_verbose(
@ -5417,7 +5419,7 @@ fn point_at_assoc_type_restriction<G: EmissionGuarantee>(
);
}
}
prev = Some(pred);
prev = Some((pred, curr_span));
}
}

View file

@ -150,8 +150,8 @@ fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]>
.predicates
.iter()
.filter_map(|pred| {
match pred {
hir::WherePredicate::BoundPredicate(pred)
match pred.kind {
hir::WherePredicateKind::BoundPredicate(pred)
if pred.bounded_ty.hir_id.owner.to_def_id() == trait_def_id =>
{
// Fetch spans for trait bounds that are Sized: