diff options
| author | bors <bors@rust-lang.org> | 2024-07-30 10:39:33 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-07-30 10:39:33 +0000 |
| commit | 1ddedbaa5919b7b3e70d984660e21e844c615c97 (patch) | |
| tree | 873de3810aa4b299b5d9d57abacc99b29076a0d9 /compiler/rustc_data_structures/src | |
| parent | e69c19ea0b8cf29ab8188a0eb5e899655464a1ff (diff) | |
| parent | f2f9aab3806916105518ce8e4ccb143028ab81ff (diff) | |
| download | rust-1ddedbaa5919b7b3e70d984660e21e844c615c97.tar.gz rust-1ddedbaa5919b7b3e70d984660e21e844c615c97.zip | |
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.
Diffstat (limited to 'compiler/rustc_data_structures/src')
0 files changed, 0 insertions, 0 deletions
