Replace #[rustc_do_not_implement_via_object] with #[rustc_dyn_incompatible_trait]#148637
Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom Jan 21, 2026
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background:
#[rustc_do_not_implement_via_object]on a trait currently still allowsdyn Traitto exist (if the trait is otherwise dyn-compatible), it just means thatdyn Traitdoes not automatically implementTraitvia the normal object candidate. For some traits, this means thatdyn Traitdoes not implementTraitat all (e.g.UnsizeandTuple). For some traits, this means thatdyn TraitimplementsTrait, but with different associated types (e.g.Pointee,DiscriminantKind). Both of these cases can can cause issues with codegen , as seen in #148089 (and #148089 (comment) ), because codegen assumes that ifdyn Traitdoes not implementTrait(including ifdyn Trait<Assoc = T>does not implementTraitwithAssoc == T), thendyn Traitcannot be constructed, so vtable accesses ondyn Traitare unreachable, but this is not the case ifdyn Traithas multiple supertraits: one which is#[rustc_do_not_implement_via_object], and one which we are doing the vtable access to call a method from.This PR replaces
#[rustc_do_not_implement_via_object]with#[rustc_dyn_incompatible_trait], which makes the marked trait dyn-incompatible, makingdyn Traitnot well-formed, instead of it being well-formed but not implementingTrait. This resolves #148089 by making it not compile.May fix #148615
The traits that are currently marked
#[rustc_do_not_implement_via_object]are:Sized,MetaSized,PointeeSized,TransmuteFrom,Unsize,BikeshedGuaranteedNoDrop,DiscriminantKind,Destruct,Tuple,FnPtr,Pointee. Of these:SizedandFnPtrare already not dyn-compatible (FnPtr: Copy, which impliesSized)MetaSized#[rustc_do_not_implement_via_object]. Still dyn-compatible after this change. (Has a special-case in the trait solvers to ignore the object candidate fordyn MetaSized, since itdyn MetaSized: MetSizedcomes from the sized candidate that alldyn Traitget.)PointeeSized#[rustc_do_not_implement_via_object]. It doesn't seem to have been doing anything anyway (playground), sincePointeeSizedis removed before trait solving(?).Pointee,DiscriminantKind,Unsize, andTuplebeing dyn-compatible without havingdyn Trait: Trait(with same assoc tys) can be observed to cause codegen issues (Segfault on nightly with feature Pointer Metadata #148089) so should be made dyn-incompatibleDestruct,TransmuteFrom, andBikeshedGuaranteedNoDropI'm not sure if would be useful as object types, but they can be relaxed to being dyn-compatible later if it is determined they should be.resolved
Questions before merge:
dyn MetaSized: MetaSizedhaving bothSizedCandidateandObjectCandidateMetaSized(which is the only trait that was previously#[rustc_do_not_implement_via_object]that is still dyn-compatible after this change). Is it fine to just remove them? Removing them (as I did in the second commit) doesn't change any UI test results.dyn MetaSizedcould get itsMetaSizedimplementation in two ways: the object candidate (the normaldyn Trait: Trait) that was supressed by#[rustc_do_not_implement_via_object], and theSizedCandidate(that alldyn Traitget fordyn Trait: MetaSized). Given thatMetaSizedhas no associated types or methods, is it fine that these both exist now? Or is it better to only have theSizedCandidateand leave these checks in (i.e. drop the second commit, and remove the FIXMEs)?dyn MetaSizedto ignore the object candidate in preference to the sizedness candidate (technically the check is for anyis_sizedness_trait, but onlyMetaSizedgets this far (Sizedis inherently dyn-incompatible, anddyn PointeeSizedis ill-formed for other reasons)trait Foo: Pointee {}, you now get a note that reads like Foo "opted out of dyn-compatbility", when reallyPointeedid that.diagnostic example
Still investigating "3.
compiler/rustc_hir/src/attrs/encode_cross_crate.rs: ShouldDynIncompatibleTraitattribute be encoded cross crate?"