fix: Check associated type bounds from supertraits for trait object well-formedness#153518
Open
akhilesharora wants to merge 1 commit intorust-lang:mainfrom
Open
Conversation
Collaborator
|
r? @nnethercote rustbot has assigned @nnethercote. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
…ormedness When forming a trait object like `dyn Sub<Assoc = String>` where `trait Sub: Super<Assoc: Copy>`, the compiler was not verifying that `String: Copy`. This allowed unsound code to compile, potentially leading to use-after-free vulnerabilities. The root cause was that both the old and new trait solvers used `explicit_super_predicates_of` which only yields `Self: SuperTrait` predicates, but not the associated type bounds from those supertraits. For example, `trait Sub: Super<Assoc: Copy>` expands to both `Self: Super` and `<Self as Super>::Assoc: Copy`, but only the first was being checked. This change switches to `explicit_implied_predicates_of` in both: - The old solver's `confirm_object_candidate` - The new solver's `predicates_for_object_candidate` This ensures that associated type bounds from supertraits are properly verified when constructing trait objects.
b50bb80 to
3dd9775
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Summary
Fixes #152607
When forming a trait object like
dyn Sub<Assoc = String>wheretrait Sub: Super<Assoc: Copy>, the compiler was not verifying thatString: Copy. This allowed unsound code to compile, potentially leading to use-after-free vulnerabilities.Root Cause
Both the old and new trait solvers used
explicit_super_predicates_ofwhich only yieldsSelf: SuperTraitpredicates (e.g.,Self: Super), but not the associated type bounds from those supertraits (e.g.,<Self as Super>::Assoc: Copy).The Fix
Changed both solvers to use
explicit_implied_predicates_ofinstead, which yields both supertrait predicates AND their associated type bounds:compiler/rustc_trait_selection/src/traits/select/confirmation.rs)compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs)Test Plan
tests/ui/traits/object/supertrait-assoc-type-bounds.rsthat now correctly fails with "the trait boundString: Copyis not satisfied"tests/ui/traits/object/supertrait-assoc-type-bounds-pass.rsconfirming valid cases still compile