Skip to content

final methods should work the same as without it (if it works without it) #153649

Closed as not planned

Description

@mu001999

Tracking: #131179

Found when trying to use final to impl Error::type_id in #153598, this code (play) doesn't work as expected:

#![feature(final_associated_functions)]

trait Tr
where
    Self: 'static,
{
    final fn foo(&self) -> std::any::TypeId {
        std::any::TypeId::of::<Self>()
    }
}

struct Foo;
impl Tr for Foo {}

struct Bar;
impl Tr for Bar {}

fn foo(t: &dyn Tr) -> std::any::TypeId {
    t.foo()
}

fn main() {
    assert_ne!(foo(&Foo), foo(&Bar));
}

I expected to see this happen: run successfully

Instead, this happened: assertion failed. And without final, this will run as expected.

Meta

Build using the Nightly version: 1.96.0-nightly

(2026-03-09 2d76d9bc76f27b03b489)

Activity

  1. added
    C-bug
    on Mar 10, 2026
  2. added
    needs-triage
    on Mar 10, 2026
  3. mu001999 commented on Mar 10, 2026

    @mu001999
    Author
  4. eggyal commented on Mar 10, 2026

    @eggyal
  5. added
    A-dyn-trait
    F-final_associated_functions
    I-unsound
    T-types
    and removed
    needs-triage
    on Mar 10, 2026
  6. eggyal commented on Mar 10, 2026

    @eggyal
    Contributor

    I guess this issue shows that the following statement from the Future Possibilities section of RFC 3678 is incorrect:

    final methods do not need to appear in a trait’s vtable.

    I'm not sure whether that is the current implementation, and therefore responsible for this issue.

  7. eggyal commented on Mar 10, 2026

    @eggyal
    Contributor

    Ah, yes, we are:

    // Final methods should not be included in the vtable.
    if trait_method.defaultness(tcx).is_final() {
    return None;
    }

  8. mu001999 commented on Mar 10, 2026

    @mu001999
    MemberAuthor

    I'm not sure whether that is the current implementation, and therefore responsible for this issue.

    Current implementation will exclude final methods from the trait's vtable (not just do not need).

    This should be fixed if we keep dyn-compatible final methods in the vtable and only exclude final methods which are not dyn-compatible. But I think this way will also lead to unpredictable behavior when calling a final method.

  9. eggyal commented on Mar 10, 2026

    @eggyal
    Contributor

    I think, at least for the time being, all final methods should appear in the vtable. Whilst removing them has the runtime benefit of calls through a dyn Trait being direct vs indirect, I suspect the small benefit from such optimisation will be outweighed by the not insignificant effort involved in correctly determining whether a final method is common across implementors or not (with unsoundness if incorrect).

    If it's nevertheless decided to only retain in vtables those final methods that do actually depend upon/differ by the implementing type, perhaps in MIR there should be a distinction between the two cases? Wouldn't that avoid calling behaviour being unpredictable (at least according to MIR), and push the unsoundness risk onto correctly lowering from HIR?

    That said, I guess lowering to the correct case would require type checking the body - which could lead to cycles...?

  10. mu001999 commented on Mar 11, 2026

    @mu001999
    MemberAuthor

    @rustbot claim

  11. eggyal commented on Mar 11, 2026

    @eggyal
    Contributor

    Actually I'm not so sure about my initial reaction to this.

    Isn't what is happening that the t.foo() calls are dispatching to <dyn Foo>::foo, so Self in both cases is dyn Foo and therefore in both cases the same TypeId is correctly returned?

    It's unintuitive, but I'm not sure we can do any better?

  12. fee1-dead commented on Apr 23, 2026

    @fee1-dead
    Member

    I agree with eggyal('s last comment) above. I think this is expected behavior after re-reading the RFC.

    So I think this is more of a documentation problem since the RFC isn't super clear about this: declaring a trait method as final is completely equivalent to extracting its body into a generic function on Self, with all calls to that method replaced by calls to the generic function. Under this scheme this is expected since you are using Self = dyn Foo.

    The other way around, if we go with the change in the linked PR, would be even more confusing IMO. It would mean that changing a final method from dyn-compatible to dyn-incompatible would affect the behavior of TypeId::of since by then we can't include them in the vtable.

  13. fee1-dead commented on Apr 25, 2026

    @fee1-dead
    Member

    Removing I-unsound, since this isn't a soundness issue

  14. removed
    I-unsound
    on Apr 25, 2026
  15. mu001999 commented on May 7, 2026

    @mu001999
    MemberAuthor

    Closed due to #153696 (comment)

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

Metadata

Metadata

Assignees

Labels

A-dyn-traitC-bugF-final_associated_functionsT-types

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions