Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22867 from dfireBird/push-qyookpnnzqok
feat: merge `WherePredicate::ForLifetimes` into `WherePredicate::TypeBound`
Lukas Wirth 3 weeks ago
parent 2a512c2 · parent 131d0c9 · commit 6cc0d0f
-rw-r--r--crates/hir-def/src/expr_store/lower/generics.rs18
-rw-r--r--crates/hir-def/src/expr_store/pretty.rs25
-rw-r--r--crates/hir-def/src/hir/generics.rs3
-rw-r--r--crates/hir-ty/src/display.rs3
-rw-r--r--crates/hir-ty/src/lower.rs18
-rw-r--r--crates/hir/src/display.rs22
6 files changed, 37 insertions, 52 deletions
diff --git a/crates/hir-def/src/expr_store/lower/generics.rs b/crates/hir-def/src/expr_store/lower/generics.rs
index 2119f19c06..ce6e73670c 100644
--- a/crates/hir-def/src/expr_store/lower/generics.rs
+++ b/crates/hir-def/src/expr_store/lower/generics.rs
@@ -220,13 +220,10 @@ impl GenericParamsCollector {
);
let predicate = match (target, bound) {
(_, TypeBound::Error | TypeBound::Use(_)) => return,
- (Either::Left(type_ref), bound) => match hrtb_lifetimes {
- Some(hrtb_lifetimes) => WherePredicate::ForLifetime {
- lifetimes: ThinVec::from_iter(hrtb_lifetimes.iter().cloned()),
- target: type_ref,
- bound,
- },
- None => WherePredicate::TypeBound { target: type_ref, bound },
+ (Either::Left(type_ref), bound) => WherePredicate::TypeBound {
+ lifetimes: hrtb_lifetimes.map(|h| ThinVec::from_iter(h.iter().cloned())),
+ target: type_ref,
+ bound,
},
(Either::Right(lifetime), TypeBound::Lifetime(bound)) => {
WherePredicate::Lifetime { target: lifetime, bound }
@@ -257,8 +254,11 @@ impl GenericParamsCollector {
}));
let type_ref = ec.alloc_type_ref(param_id, ptr);
for bound in impl_trait_bounds {
- where_predicates
- .push(WherePredicate::TypeBound { target: type_ref, bound: bound.clone() });
+ where_predicates.push(WherePredicate::TypeBound {
+ lifetimes: None,
+ target: type_ref,
+ bound: bound.clone(),
+ });
}
type_ref
}
diff --git a/crates/hir-def/src/expr_store/pretty.rs b/crates/hir-def/src/expr_store/pretty.rs
index bc16a9e979..67eb0814c7 100644
--- a/crates/hir-def/src/expr_store/pretty.rs
+++ b/crates/hir-def/src/expr_store/pretty.rs
@@ -338,7 +338,17 @@ fn print_where_clauses(
w!(p, ",\n");
}
match pred {
- WherePredicate::TypeBound { target, bound } => {
+ WherePredicate::TypeBound { lifetimes, target, bound } => {
+ if let Some(lifetimes) = lifetimes {
+ w!(p, "for<");
+ for (i, lifetime) in lifetimes.iter().enumerate() {
+ if i != 0 {
+ w!(p, ", ");
+ }
+ w!(p, "{}", lifetime.display(db, p.edition));
+ }
+ w!(p, "> ");
+ }
p.print_type_ref(*target);
w!(p, ": ");
p.print_type_bounds(std::slice::from_ref(bound));
@@ -348,19 +358,6 @@ fn print_where_clauses(
w!(p, ": ");
p.print_lifetime_ref(*bound);
}
- WherePredicate::ForLifetime { lifetimes, target, bound } => {
- w!(p, "for<");
- for (i, lifetime) in lifetimes.iter().enumerate() {
- if i != 0 {
- w!(p, ", ");
- }
- w!(p, "{}", lifetime.display(db, p.edition));
- }
- w!(p, "> ");
- p.print_type_ref(*target);
- w!(p, ": ");
- p.print_type_bounds(std::slice::from_ref(bound));
- }
}
}
});
diff --git a/crates/hir-def/src/hir/generics.rs b/crates/hir-def/src/hir/generics.rs
index b6e9fc2820..039b229429 100644
--- a/crates/hir-def/src/hir/generics.rs
+++ b/crates/hir-def/src/hir/generics.rs
@@ -185,9 +185,8 @@ impl ops::Index<LocalLifetimeParamId> for GenericParams {
/// associated type bindings like `Iterator<Item = u32>`.
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum WherePredicate {
- TypeBound { target: TypeRefId, bound: TypeBound },
+ TypeBound { lifetimes: Option<ThinVec<Name>>, target: TypeRefId, bound: TypeBound },
Lifetime { target: LifetimeRefId, bound: LifetimeRefId },
- ForLifetime { lifetimes: ThinVec<Name>, target: TypeRefId, bound: TypeBound },
}
static EMPTY: LazyLock<GenericParams> = LazyLock::new(|| GenericParams {
diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs
index ff7f849d30..dc20eb930e 100644
--- a/crates/hir-ty/src/display.rs
+++ b/crates/hir-ty/src/display.rs
@@ -2461,8 +2461,7 @@ impl<'db> HirDisplayWithExpressionStore<'db> for TypeRefId {
.where_predicates()
.iter()
.filter_map(|it| match it {
- WherePredicate::TypeBound { target, bound }
- | WherePredicate::ForLifetime { lifetimes: _, target, bound }
+ WherePredicate::TypeBound { lifetimes: _, target, bound }
if matches!(
store[*target],
TypeRef::TypeParam(t) if t == *param
diff --git a/crates/hir-ty/src/lower.rs b/crates/hir-ty/src/lower.rs
index d3e79ae68e..f68358af94 100644
--- a/crates/hir-ty/src/lower.rs
+++ b/crates/hir-ty/src/lower.rs
@@ -896,10 +896,12 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
};
match where_predicate {
- WherePredicate::ForLifetime { target, bound, lifetimes } => {
- self.with_shifted_in(lifetimes, |ctx| lower_type_outlives(ctx, target, bound)).0
- }
- WherePredicate::TypeBound { target, bound } => lower_type_outlives(self, target, bound),
+ WherePredicate::TypeBound { lifetimes, target, bound } => match lifetimes {
+ Some(lifetimes) => {
+ self.with_shifted_in(lifetimes, |ctx| lower_type_outlives(ctx, target, bound)).0
+ }
+ None => lower_type_outlives(self, target, bound),
+ },
&WherePredicate::Lifetime { bound, target } => Either::Right(iter::once((
Clause(Predicate::new(
self.interner,
@@ -2022,9 +2024,7 @@ impl SupertraitsInfo {
let resolver = trait_.resolver(db);
let signature = TraitSignature::of(db, trait_);
for pred in signature.generic_params.where_predicates() {
- let (WherePredicate::TypeBound { target, bound }
- | WherePredicate::ForLifetime { lifetimes: _, target, bound }) = pred
- else {
+ let WherePredicate::TypeBound { lifetimes: _, target, bound } = pred else {
continue;
};
let (TypeBound::Path(bounded_trait, TraitBoundModifier::None)
@@ -2140,9 +2140,7 @@ fn resolve_type_param_assoc_type_shorthand(
for maybe_parent_generics in generics.iter_owners().rev() {
ctx.set_owner(maybe_parent_generics);
for pred in maybe_parent_generics.where_predicates() {
- let (WherePredicate::TypeBound { target, bound }
- | WherePredicate::ForLifetime { lifetimes: _, target, bound }) = pred
- else {
+ let WherePredicate::TypeBound { lifetimes: _, target, bound } = pred else {
continue;
};
let (TypeBound::Path(bounded_trait_path, TraitBoundModifier::None)
diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs
index 6f7615ed18..dc35a5c57b 100644
--- a/crates/hir/src/display.rs
+++ b/crates/hir/src/display.rs
@@ -806,10 +806,6 @@ fn write_where_predicates<'db>(
let check_same_target = |pred1: &WherePredicate, pred2: &WherePredicate| match (pred1, pred2) {
(TypeBound { target: t1, .. }, TypeBound { target: t2, .. }) => t1 == t2,
(Lifetime { target: t1, .. }, Lifetime { target: t2, .. }) => t1 == t2,
- (
- ForLifetime { lifetimes: l1, target: t1, .. },
- ForLifetime { lifetimes: l2, target: t2, .. },
- ) => l1 == l2 && t1 == t2,
_ => false,
};
@@ -821,7 +817,12 @@ fn write_where_predicates<'db>(
f.write_str("\n ")?;
match pred {
- TypeBound { target, bound } => {
+ TypeBound { lifetimes, target, bound } => {
+ if let Some(lifetimes) = lifetimes {
+ let lifetimes =
+ lifetimes.iter().map(|it| it.display(f.db, f.edition())).join(", ");
+ write!(f, "for<{lifetimes}> ")?;
+ }
target.hir_fmt(f, owner, store)?;
f.write_str(": ")?;
bound.hir_fmt(f, owner, store)?;
@@ -831,21 +832,12 @@ fn write_where_predicates<'db>(
write!(f, ": ")?;
bound.hir_fmt(f, owner, store)?;
}
- ForLifetime { lifetimes, target, bound } => {
- let lifetimes = lifetimes.iter().map(|it| it.display(f.db, f.edition())).join(", ");
- write!(f, "for<{lifetimes}> ")?;
- target.hir_fmt(f, owner, store)?;
- f.write_str(": ")?;
- bound.hir_fmt(f, owner, store)?;
- }
}
while let Some(nxt) = iter.next_if(|nxt| check_same_target(pred, nxt)) {
f.write_str(" + ")?;
match nxt {
- TypeBound { bound, .. } | ForLifetime { bound, .. } => {
- bound.hir_fmt(f, owner, store)?
- }
+ TypeBound { bound, .. } => bound.hir_fmt(f, owner, store)?,
Lifetime { bound, .. } => bound.hir_fmt(f, owner, store)?,
}
}