Skip to content

fix: Check associated type bounds from supertraits for trait object well-formedness#153518

Open
akhilesharora wants to merge 1 commit intorust-lang:mainfrom
akhilesharora:fix/issue-152607-supertrait-assoc-type-bound
Open

fix: Check associated type bounds from supertraits for trait object well-formedness#153518
akhilesharora wants to merge 1 commit intorust-lang:mainfrom
akhilesharora:fix/issue-152607-supertrait-assoc-type-bound

Conversation

@akhilesharora
Copy link

@akhilesharora akhilesharora commented Mar 7, 2026

Summary

Fixes #152607

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.

Root Cause

Both the old and new trait solvers used explicit_super_predicates_of which only yields Self: SuperTrait predicates (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_of instead, which yields both supertrait predicates AND their associated type bounds:

  • Old solver (compiler/rustc_trait_selection/src/traits/select/confirmation.rs)
  • New solver (compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs)

Test Plan

  • Added regression test tests/ui/traits/object/supertrait-assoc-type-bounds.rs that now correctly fails with "the trait bound String: Copy is not satisfied"
  • Added positive test tests/ui/traits/object/supertrait-assoc-type-bounds-pass.rs confirming valid cases still compile
  • All trait object tests pass
  • All associated type bounds tests pass

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 7, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 16 candidates

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Mar 7, 2026
@rustbot

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.
@akhilesharora akhilesharora force-pushed the fix/issue-152607-supertrait-assoc-type-bound branch from b50bb80 to 3dd9775 Compare March 7, 2026 00:03
@fmease fmease assigned lcnr and unassigned nnethercote Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Associated type bound from supertrait isn't checked for trait object

4 participants