Trait method impl restrictions (final methods)#3678
Trait method impl restrictions (final methods)#3678joshtriplett merged 35 commits intorust-lang:masterfrom
final methods)#3678Conversation
|
We've past proposals for inherent trait methods I think, mostly proposing a second inherent looking There is however a question of default speed vs size optimizations in vtables, aka do these methods appear in the vtable, or do they have a generic implementation for every type? Also, how does one flag that these method should appear in a vtable, or should use a generic implementation for every type? |
|
It might be better to have this as an attribute rather than yet another potential keyword position for parsing to deal with. Otherwise, great. |
|
@Lokathor wrote:
I get that, but on the flip side, that'd be inconsistent with RFC 3323, and it seems awkward to use a keyword in one place and an attribute in another. |
|
@burdges wrote:
That might be equivalent to a subset of this, depending on the details. I haven't seen those proposals.
In theory, if you have a method that can't be overridden outside of the crate/module, and nothing overrides it, you could optimize by omitting it from the vtable. I don't think that optimization should be mandatory, or required for initial implementation, though. |
|
I'm more asking what's the best default. If the best default were to be in the vtable, then you can just do I'd expect If otoh the best default were not to be in the vtable, then rustc should do something more complex. Anyways yeah maybe not relevant here. |
|
Another future possibility could be to add |
|
@bluebear94 Interesting! So, rather than requiring |
|
this would be quite useful for stabilizing |
|
@programmerjake That's a great use case, thank you! |
|
This seems like a reasonable and desirable extension to RFC 3323. So I propose... @rfcbot fcp merge Thanks @joshtriplett for writing this up. I note that syntax was left as an open item on RFC 3323, and so we're implicitly carrying that open item here also. |
|
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
|
Big +1 to having something like this. @rfcbot concern visiblity-style-vs-final-style I think these are far more than a syntax difference, so I want to talk about it more. As a couple of examples:
So is the visibility phrasing here actually important? Do we actually need it? The cases I know of don't need it
The provided implementation of a Thus my inclination is that we should do the |
|
I think |
|
If it is to be an attribute then I would prefer something like I assume the syntax was chosen for parity with RFC3323, but I think it is okay to deviate from this if it comes with a simplification. |
Note that the RFC allows for
This is something I specifically checked to ensure was there before proposing FCP merge, in part for the reasons you mention. |
This comment was marked as duplicate.
This comment was marked as duplicate.
I think that that just pushes me even more to skip the If we need a special syntax, let's make it (And, aesthetically, |
Avoid implying that this should always happen if possible. The compiler may not always want to do this.
|
Updated based on @Amanieu's comments. |
|
@rfcbot resolve amanieu-dyn-compatibility |
|
@rfcbot resolve amanieu-dyn-compatibility |
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
|
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. |
|
Huzzah! The @rust-lang/lang team has decided to accept this RFC. To track further discussion, subscribe to the tracking issue here: |
|
The next step is an implementation. There was a previous draft at rust-lang/rust#130802 , which may or may not be useful as a starting point for someone looking to work on an implementation. (It does not straightforwardly rebase; the compiler has changed since that PR.) I'd be happy to work with someone looking to implement this to provide any assistance needed. |
…, r=fee1-dead Implement RFC 3678: Final trait methods Tracking: rust-lang#131179 This PR is based on rust-lang#130802, with some minor changes and conflict resolution. Futhermore, this PR excludes final methods from the vtable of a dyn Trait. And some excerpt from the original PR description: > Implements the surface part of rust-lang/rfcs#3678. > > I'm using the word "method" in the title, but in the diagnostics and the feature gate I used "associated function", since that's more accurate. cc @joshtriplett
Rollup merge of #151783 - mu001999-contrib:impl/final-method, r=fee1-dead Implement RFC 3678: Final trait methods Tracking: #131179 This PR is based on #130802, with some minor changes and conflict resolution. Futhermore, this PR excludes final methods from the vtable of a dyn Trait. And some excerpt from the original PR description: > Implements the surface part of rust-lang/rfcs#3678. > > I'm using the word "method" in the title, but in the diagnostics and the feature gate I used "associated function", since that's more accurate. cc @joshtriplett
…dead Implement RFC 3678: Final trait methods Tracking: rust-lang/rust#131179 This PR is based on rust-lang/rust#130802, with some minor changes and conflict resolution. Futhermore, this PR excludes final methods from the vtable of a dyn Trait. And some excerpt from the original PR description: > Implements the surface part of rust-lang/rfcs#3678. > > I'm using the word "method" in the title, but in the diagnostics and the feature gate I used "associated function", since that's more accurate. cc @joshtriplett
…dead Implement RFC 3678: Final trait methods Tracking: rust-lang/rust#131179 This PR is based on rust-lang/rust#130802, with some minor changes and conflict resolution. Futhermore, this PR excludes final methods from the vtable of a dyn Trait. And some excerpt from the original PR description: > Implements the surface part of rust-lang/rfcs#3678. > > I'm using the word "method" in the title, but in the diagnostics and the feature gate I used "associated function", since that's more accurate. cc @joshtriplett
…dead Implement RFC 3678: Final trait methods Tracking: rust-lang/rust#131179 This PR is based on rust-lang/rust#130802, with some minor changes and conflict resolution. Futhermore, this PR excludes final methods from the vtable of a dyn Trait. And some excerpt from the original PR description: > Implements the surface part of rust-lang/rfcs#3678. > > I'm using the word "method" in the title, but in the diagnostics and the feature gate I used "associated function", since that's more accurate. cc @joshtriplett
Support restricting implementation of individual methods within traits, using the already reserved
finalkeyword.This makes it possible to define a trait that any crate can implement, but disallow overriding one of the trait's methods or associated functions.
This was inspired in the course of writing another RFC defining a trait, which wanted precisely this feature of restricting overrides of the trait's method. I separated out this feature as its own RFC, since it's independently useful for various other purposes, and since it should be available to any crate and not just the standard library.
Rendered
Tracking:
finalmethods) rust#131179