Skip to content

Lang/libs coordination question: okay for libs to stabilize traits with final fn in them? #160394

Open

Description

@joshtriplett

Filing this issue to get a definitive answer on whether @rust-lang/lang has any concerns about @rust-lang/libs-api stabilizing APIs in the standard library that use final fn.

If we stabilize such an API, that commits us to keeping this mechanism. That seems fine to me, as we have an accepted RFC and we seem to have a consistent expectation of shipping/stabilizing final fn.

Tracking issue for final fn: #131179

Activity

  1. added
    needs-triage
    on Aug 2, 2026
  2. added
    T-lang
    T-libs-api
    I-lang-nominated
    S-waiting-on-t-lang
    and removed
    needs-triage
    on Aug 2, 2026
  3. lcnr commented on Aug 3, 2026

    @lcnr
    Contributor

    what's the relevant RFC here and does this also matter for Types?

  4. joboet commented on Aug 3, 2026

    @joboet
    Member

    For context: this came up in #160365, but I remember from the All Hands that libs-api would like to use this for Allocator::by_ref as well.

    The RFC is RFC#3678.

  5. joshtriplett commented on Aug 4, 2026

    @joshtriplett
    MemberAuthor

    @lcnr If Types has recommendations here, I think they'd be relevant.

  6. added
    I-lang-radar
    P-lang-drag-1
    T-types
    on Aug 5, 2026
  7. traviscross commented on Aug 5, 2026

    @traviscross
    Contributor

    Let's ask via FCP (and see what questions come up).

    Proposal: We're OK with libs-api stabilizing traits with final fn (RFC 3678) in them subject to1 not stabilizing any trait or trait method that would prevent us from consistently putting these final fn methods in the vtable by making the trait dyn-incompatible if we did or by making it observable how Self is instantiated.

    @rfcbot fcp merge lang,types

    Footnotes

    1. Edit 2026-08-10: See the resolution of the concern. ↩

  8. rust-rfcbot commented on Aug 5, 2026

    @rust-rfcbot
    Collaborator

    @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.
    cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns.
    See this document for info about what commands tagged team members can give me.

  9. added
    proposed-final-comment-period
    disposition-merge
    on Aug 5, 2026
  10. scottmcm commented on Aug 5, 2026

    @scottmcm
    Member

    @rfcbot concern discuss-what-this-promises-for-dyn

    Totally fine with it for static dispatch, where the rules are clear.

    I wonder if we could maybe make using the final fn methods via dyn still be unstable even if the method is stabilized to gain time to solidify exactly what the rules are going to be about that?

    I was originally pondering allowing it only for #[rustc_dyn_incompatible_trait] traits, but Rng::fill_bytes looks like something that would be entirely reasonable to use behind dyn Rng.)

    Reminder that I still think that final fn methods should be in the vtable, since that allows them to be optimized based on what the other methods they call do. (If <dyn RangeBounds>::contains were to be made final, I wouldn't want it making two dyn calls to get the bounds, for example, when it could make one dyn call to contains which then might not have to do any comparisons at all if the source of the bounds is RangeFull, for example.)


    Aside: if Rng::fill_bytes is the motivating example, what's the soundness concern that makes it be final fn instead of an ordinary provided method? Or is it just trying to not put it into the vtable?

  11. joshtriplett commented on Aug 5, 2026

    @joshtriplett
    MemberAuthor

    We discussed this in today's @rust-lang/lang meeting. We need to more definitively settle some of the questions around dyn and the vtable, so for now we'd like to ask libs-api to only stabilize traits using final fn if 1) they would otherwise be dyn-compatible, and 2) they don't do anything like calling type_id that would allow distinguishing whether it's in the vtable.

    We should document this in std-dev-guide.

  12. joshtriplett commented on Aug 5, 2026

    @joshtriplett
    MemberAuthor

    what's the soundness concern that makes it be final fn instead of an ordinary provided method?

    Not a soundness concern, just wanting to distinguish "what can trait implementers implement", and avoid having to do a pair of circular definitions with must_implement_one_of.

  13. scottmcm commented on Aug 5, 2026

    @scottmcm
    Member

    Resolving my concern with the allowed stabilization limited as josh said above

    if 1) they would otherwise be dyn-compatible, and
    2) they don't do anything like calling type_id that would allow distinguishing whether it's in the vtable.

    @rfcbot resolve discuss-what-this-promises-for-dyn

  14. scottmcm commented on Aug 5, 2026

    @scottmcm
  15. tmandry commented on Aug 5, 2026

    @tmandry
    Member

    @rfcbot reviewed

    Agreed with the limitations listed in #160394 (comment).

    To document the reasoning: There's a question of whether final fn methods should appear in the vtable. If they do appear in the vtable, final fn would 1) not allow a trait to be dyn compatible when used on a dyn-incompatible method and 2) allow type_id and friends to observe the actual type from the final fn implementation. So by restricting its use in this way, we can change the answer later without changing any observable behavior.

  16. lcnr commented on Aug 10, 2026

    @lcnr
    Contributor

    effectively this locks us into having some way of supporting "extension methods" with the following properties?

    trait Trait {
        final fn foo() {}
    }
    • can name it as Trait::foo
    • the method is accessible if Trait is in scope of if the self type is dyn Trait.

    seems fine enough 🤷 cc rust-lang/library-trait-evolution-initiative#1 which has similar requirements

    @rustbot reviewed

  17. added
    T-libs
    and removed
    T-libs-api
    on Aug 12, 2026
  18. added
    P-lang-drag-2
    and removed
    P-lang-drag-1
    on Sep 2, 2026
  19. jackh726 commented on Sep 22, 2026

    @jackh726
    Member

    @rustbot concern types-semantics-and-documentation

    So, I checked my box - but on consideration, I'm not really comfortable here with the information I (don't) have. This isn't a feature stabilization, but it is making something available on stable that requires (some, but minimal) types support.

    I'd really like to know what it is exactly that we're committing to with t-types with these functions being used on stable. Perhaps the question around dyn/vtable things has been settled, and that's mostly a lang matter anyways. But, is there any official documentation on what final fn does? And how its implemented?

    Looking at #151783, there are really only two things I see that are relevant to t-types:

    • final fn are ignored for dyn-compatibilty
    • final fn are not included in the vtable (and are called directly)

    That's not much (and neither are really maintenance hazards), but I still want to ensure that this is documented clearly somewhere.

  20. rustbot commented on Sep 22, 2026

    @rustbot
    Collaborator

    Error: Cannot set @rustbot concern on an active rfcbot FCP, use @rfcbot concern instead.

    Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

  21. jackh726 commented on Sep 22, 2026

    @jackh726
    Member

    @rfcbot concern types-semantics-and-documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-lang-nominatedI-lang-radarP-lang-drag-2S-waiting-on-t-langT-langT-libsT-typesdisposition-mergeproposed-final-comment-period

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions