Rollup merge of #150130 - aerooneqq:delegation-one-line-trait-impl, r=petrochenkov

Support syntax for one-line trait reuse

This PR adds support for reusing the whole trait with a one-line reuse syntax and is part of the delegation feature rust-lang/rust#118212:
```rust
trait T {
  fn foo(&self);
}

struct S;
impl T for S { ... }

struct Wrapper(S);
reuse impl T for Wrapper { self.0 }
```

The core idea is that we already have support for glob reuse, so in this scenario we want to transform one-line reuse into a trait impl block with a glob reuse in the following way:
```rust
//Before
reuse impl T for Wrapper { self.0 }

//After
impl T for Wrapper {
  reuse T::* { self.0 }
}
```

It seems like this task can be solved during parsing stage, when we encountered a one-line trait reuse, we can expand into this impl block right away, and the code which was already written to expand glob delegations will take care about the rest. We will copy trait path into glob reuse path.

The implementation of the transformation reuses already existing methods for `impl` parsing, however, we do not parse inner `impl` items, instead we parse "inner items" as delegation body. Thus, we do not have to deal with generics, consts, unsafe and other `impl` related features.

Other syntax possibility is trying to shorten one-line reuse by replacing `impl` keyword with `reuse` keyword:
```rust
reuse T for Wrapper { self.0 }
```
In this case implementation may become more complicated, and the syntax more confusing, as keywords such as `const` or `unsafe` will precede `reuse`, and there are also generics:
```rust
unsafe reuse<T1, T2> T for Wrapper { self.0 }
```

In the first (currently implemented) version reuse is placed in the beginning of the item, and it is clear that we will reuse trait implementation, while in the second, shorter version, the `reuse` keyword may be lost in generics and keywords that may precede `impl`.

r? ``@petrochenkov``
This commit is contained in:
Jonathan Brouwer 2025-12-23 12:01:01 +01:00 committed by GitHub
commit e156286fa5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 879 additions and 33 deletions

View file

@ -0,0 +1,46 @@
#![feature(prelude_import)]
#![no_std]
//@ pretty-compare-only
//@ pretty-mode:expanded
//@ pp-exact:delegation-impl-reuse.pp
#![allow(incomplete_features)]
#![feature(fn_delegation)]
#[macro_use]
extern crate std;
#[prelude_import]
use ::std::prelude::rust_2015::*;
trait Trait<T> {
fn foo(&self) {}
fn bar(&self) {}
fn baz(&self) {}
}
struct S;
impl Trait<{
struct S;
0
}> for S {
reuse Trait<{
struct S;
0
}>::foo {
self.0
}
reuse Trait<{
struct S;
0
}>::bar {
self.0
}
reuse Trait<{
struct S;
0
}>::baz {
self.0
}
}
fn main() {}

View file

@ -0,0 +1,18 @@
//@ pretty-compare-only
//@ pretty-mode:expanded
//@ pp-exact:delegation-impl-reuse.pp
#![allow(incomplete_features)]
#![feature(fn_delegation)]
trait Trait<T> {
fn foo(&self) {}
fn bar(&self) {}
fn baz(&self) {}
}
struct S;
reuse impl Trait<{ struct S; 0 }> for S { self.0 }
fn main() {}