1
Fork 0
rust/compiler/rustc_hir_analysis/src
bors 1ddedbaa59 Auto merge of #125929 - Bryanskiy:delegation-generics-3, r=petrochenkov
Delegation: support generics for delegation from free functions

(The PR was split from https://github.com/rust-lang/rust/pull/123958, explainer - https://github.com/Bryanskiy/posts/blob/master/delegation%20in%20generic%20contexts.md)

This PR implements generics inheritance from free functions to free functions and trait methods.

#### free functions to free functions:

```rust
fn to_reuse<T: Clone>(_: T) {}

reuse to_reuse as bar;
// desugaring:
fn bar<T: Clone>(x: T) {
  to_reuse(x)
}
```

Generics, predicates and signature are simply copied. Generic arguments in paths are ignored during generics inheritance:

```rust
fn to_reuse<T: Clone>(_: T) {}

reuse to_reuse::<u8> as bar;
// desugaring:
fn bar<T: Clone>(x: T) {
  to_reuse::<u8>(x) // ERROR: mismatched types
}
```

Due to implementation limitations callee path is lowered without modifications. Therefore, it is a compilation error at the moment.

#### free functions to trait methods:

```rust
trait Trait<'a, A> {
    fn foo<'b, B>(&self, x: A, y: B) {...}
}

reuse Trait::foo;
// desugaring:
fn foo<'a, 'b, This: Trait<'a, A>, A, B>(this: &This, x: A, y: B) {
  Trait::foo(this, x, y)
}
```

The inheritance is similar to the previous case but with some corrections:

- `Self` parameter converted into `T: Trait`
- generic parameters need to be reordered so that lifetimes go first

Arguments are similarly ignored.

---

In the future, we plan to  support generic inheritance for delegating from all contexts to all contexts (from free/trait/impl to free/trait /impl). These cases were considered first as the simplest from the implementation perspective.
2024-07-30 10:39:33 +00:00
..
check Auto merge of #128250 - Amanieu:select_unpredictable, r=nikic 2024-07-30 03:22:27 +00:00
coherence Reformat use declarations. 2024-07-29 08:26:52 +10:00
collect Delegation: support generics for delegation from free functions 2024-07-29 20:04:55 +03:00
errors Reformat use declarations. 2024-07-29 08:26:52 +10:00
hir_ty_lowering Delegation: support generics for delegation from free functions 2024-07-29 20:04:55 +03:00
impl_wf_check Reformat use declarations. 2024-07-29 08:26:52 +10:00
outlives Reformat use declarations. 2024-07-29 08:26:52 +10:00
variance Reformat use declarations. 2024-07-29 08:26:52 +10:00
autoderef.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
bounds.rs Add constness to TraitDef 2024-07-03 15:37:34 +00:00
check_unused.rs Remove LintDiagnostic::msg 2024-05-23 04:08:35 +02:00
collect.rs Rollup merge of #128174 - compiler-errors:trait-alias-marker, r=oli-obk 2024-07-29 17:46:42 +02:00
constrained_generic_params.rs Auto merge of #125076 - compiler-errors:alias-term, r=lcnr 2024-05-13 22:20:43 +00:00
delegation.rs Delegation: support generics for delegation from free functions 2024-07-29 20:04:55 +03:00
errors.rs Delegation: support generics for delegation from free functions 2024-07-29 20:04:55 +03:00
hir_wf_check.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
impl_wf_check.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
lib.rs Delegation: support generics for delegation from free functions 2024-07-29 20:04:55 +03:00