Auto merge of #13763 - rami3l:fix/gen-partial-eq-generic, r=Veykril

fix: add generic `TypeBoundList` in generated derivable impl

Potentially fixes #13727.

Continuing with the work in #13732, this fix tries to add correct type bounds in the generated `impl` block:

```diff
  enum Either<T, U> {
      Left(T),
      Right(U),
  }

- impl<T, U> PartialEq for Either<T, U> {
+ impl<T: PartialEq, U: PartialEq> PartialEq for Either<T, U> {
      fn eq(&self, other: &Self) -> bool {
          match (self, other) {
              (Self::Left(l0), Self::Left(r0)) => l0 == r0,
              (Self::Right(l0), Self::Right(r0)) => l0 == r0,
              _ => false,
          }
      }
  }
```
This commit is contained in:
bors 2023-01-09 13:02:09 +00:00
commit ae659125a5
5 changed files with 130 additions and 23 deletions

View file

@ -719,12 +719,23 @@ pub fn param_list(
ast_from_text(&list)
}
pub fn type_param(name: ast::Name, ty: Option<ast::TypeBoundList>) -> ast::TypeParam {
let bound = match ty {
Some(it) => format!(": {it}"),
None => String::new(),
};
ast_from_text(&format!("fn f<{name}{bound}>() {{ }}"))
pub fn type_bound(bound: &str) -> ast::TypeBound {
ast_from_text(&format!("fn f<T: {bound}>() {{ }}"))
}
pub fn type_bound_list(
bounds: impl IntoIterator<Item = ast::TypeBound>,
) -> Option<ast::TypeBoundList> {
let bounds = bounds.into_iter().map(|it| it.to_string()).unique().join(" + ");
if bounds.is_empty() {
return None;
}
Some(ast_from_text(&format!("fn f<T: {bounds}>() {{ }}")))
}
pub fn type_param(name: ast::Name, bounds: Option<ast::TypeBoundList>) -> ast::TypeParam {
let bounds = bounds.map_or_else(String::new, |it| format!(": {it}"));
ast_from_text(&format!("fn f<{name}{bounds}>() {{ }}"))
}
pub fn lifetime_param(lifetime: ast::Lifetime) -> ast::LifetimeParam {