You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
final methods should work the same as without it (if it works without it) #153649
A-dyn-traitArea: trait objects, vtable layoutC-bugCategory: This is a bug.F-final_associated_functions`#![feature(final_associated_functions)]`T-typesRelevant to the types team, which will review and decide on the PR/issue.
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.
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...?
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?
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.
A-dyn-traitArea: trait objects, vtable layoutC-bugCategory: This is a bug.F-final_associated_functions`#![feature(final_associated_functions)]`T-typesRelevant to the types team, which will review and decide on the PR/issue.
Activity
mu001999 commentedon Mar 10, 2026
eggyal commentedon Mar 10, 2026
eggyal commentedon Mar 10, 2026
I guess this issue shows that the following statement from the Future Possibilities section of RFC 3678 is incorrect:
I'm not sure whether that is the current implementation, and therefore responsible for this issue.
eggyal commentedon Mar 10, 2026
Ah, yes, we are:
rust/compiler/rustc_trait_selection/src/traits/vtable.rs
Lines 213 to 216 in 3bc6ea5
mu001999 commentedon Mar 10, 2026
Current implementation will exclude
finalmethods from the trait's vtable (not just do not need).This should be fixed if we keep dyn-compatible
finalmethods in the vtable and only excludefinalmethods which are not dyn-compatible. But I think this way will also lead to unpredictable behavior when calling a final method.eggyal commentedon Mar 10, 2026
I think, at least for the time being, all
finalmethods should appear in the vtable. Whilst removing them has the runtime benefit of calls through adyn Traitbeing direct vs indirect, I suspect the small benefit from such optimisation will be outweighed by the not insignificant effort involved in correctly determining whether afinalmethod is common across implementors or not (with unsoundness if incorrect).If it's nevertheless decided to only retain in vtables those
finalmethods 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...?
mu001999 commentedon Mar 11, 2026
@rustbot claim
eggyal commentedon Mar 11, 2026
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, soSelfin both cases isdyn Fooand therefore in both cases the sameTypeIdis correctly returned?It's unintuitive, but I'm not sure we can do any better?
fee1-dead commentedon Apr 23, 2026
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
finalis completely equivalent to extracting its body into a generic function onSelf, with all calls to that method replaced by calls to the generic function. Under this scheme this is expected since you are usingSelf = 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::ofsince by then we can't include them in the vtable.fee1-dead commentedon Apr 25, 2026
Removing I-unsound, since this isn't a soundness issue
mu001999 commentedon May 7, 2026
Closed due to #153696 (comment)