-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Support syntax for one-line trait reuse #150130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+879
−33
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43b3a47 to
829d1bf
Compare
29 tasks
Contributor
Author
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
Contributor
|
r=me with the test added #150130 (comment) and commits squashed. |
Collaborator
|
Reminder, once the PR becomes ready for a review, use |
fmease
reviewed
Dec 22, 2025
1116ce4 to
1de1885
Compare
Contributor
Author
|
@rustbot ready |
Contributor
|
@bors r+ |
Collaborator
JonathanBrouwer
added a commit
to JonathanBrouwer/rust
that referenced
this pull request
Dec 23, 2025
…t-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#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`
bors
added a commit
that referenced
this pull request
Dec 23, 2025
…uwer Rollup of 6 pull requests Successful merges: - #150130 (Support syntax for one-line trait reuse) - #150205 (compiletest: Add `LineNumber` newtype to avoid `+1` magic here and there) - #150282 (move a ui test to coretests unit test) - #150295 (Fix compilation error in hermit-abi time.rs) - #150301 (std: remove unsupported pipe module from VEXos pal) - #150303 (`target_features::Stability`: tweak docs of `requires_nightly()`) r? `@ghost` `@rustbot` modify labels: rollup
Collaborator
rust-timer
added a commit
that referenced
this pull request
Dec 23, 2025
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 #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``
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
F-fn_delegation
`#![feature(fn_delegation)]`
S-waiting-on-bors
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for reusing the whole trait with a one-line reuse syntax and is part of the delegation feature #118212:
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:
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
implparsing, however, we do not parse innerimplitems, instead we parse "inner items" as delegation body. Thus, we do not have to deal with generics, consts, unsafe and otherimplrelated features.Other syntax possibility is trying to shorten one-line reuse by replacing
implkeyword withreusekeyword:In this case implementation may become more complicated, and the syntax more confusing, as keywords such as
constorunsafewill precedereuse, and there are also generics: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
reusekeyword may be lost in generics and keywords that may precedeimpl.r? @petrochenkov