rust/compiler/rustc_middle/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
..
dep_graph Reformat use declarations. 2024-07-29 08:26:52 +10:00
hir Delegation: support generics for delegation from free functions 2024-07-29 20:04:55 +03:00
hooks Reformat use declarations. 2024-07-29 08:26:52 +10:00
infer Reformat use declarations. 2024-07-29 08:26:52 +10:00
middle Reformat use declarations. 2024-07-29 08:26:52 +10:00
mir Rollup merge of #128277 - RalfJung:offset_from_wildcard, r=oli-obk 2024-07-29 11:42:34 +02:00
query Delegation: support generics for delegation from free functions 2024-07-29 20:04:55 +03:00
thir Track mutability of deref patterns 2024-04-20 15:59:54 +02:00
traits Reformat use declarations. 2024-07-29 08:26:52 +10:00
ty Reformat use declarations. 2024-07-29 08:26:52 +10:00
util Reformat use declarations. 2024-07-29 08:26:52 +10:00
arena.rs Uplift PredefinedOpaquesData 2024-06-18 10:40:30 -04:00
error.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
lib.rs Rollup merge of #125505 - aDotInTheVoid:middle-idl, r=pnkfelix 2024-06-05 18:21:09 +02:00
lint.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
macros.rs lift_to_tcx -> lift_to_interner 2024-07-17 10:46:10 -04:00
metadata.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
tests.rs Give an item related to issue 27438 a more meaningful name 2024-04-30 22:27:19 +02:00
thir.rs Rollup merge of #128304 - Zalathar:thir-pat-display, r=Nadrieril 2024-07-29 11:42:34 +02:00
values.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00