-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
dyn Trait comparison should not include the vtable pointer #106447
Copy link
Copy link
Closed
Labels
T-langRelevant to the language teamRelevant to the language teamdisposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.This PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Metadata
Metadata
Assignees
Labels
T-langRelevant to the language teamRelevant to the language teamdisposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.This PR / issue is in PFCP or FCP with a disposition to close it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
This was previously raised in #103763 (comment). Currently, comparing a pair of
*const dyn Traitraw pointers for equality will check that both the address and vtable are equal.This is problematic because the vtable of a type is not guaranteed to be unique for each type: different types can share the same vtable and the same type can have its vtable instantiated multiple times.
To illustrate this issue, consider the following program which prints
truein release mode andfalsein debug mode because LLVM is merging the two identical vtables (playground).It is also possible to achieve the reverse effect where two
dyn Traitpointers to the same value (with the same base type) compare unequal because a separate vtable was instantiated with each pointer (e.g. due to codegen units or separate crates).In conclusion, vtables are completely useless as a source of truth for pointer equality, so we should only consider the pointer address when comparing
dyn Traitraw pointers. We currently document this as a footgun (#103567), but it would be better to eliminate this footgun entirely from the language.