rust-lang / rust-lang/rust

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

Open
#152,607 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-dyn-trait C-bug I-unsound P-high T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The following code compiles and causes a use-after-free.

trait Super {
    type Assoc;
}

trait Sub: Super<Assoc: Copy> {}

fn unchecked_copy<T: Sub<Assoc = String> + ?Sized>(x: &String) -> String {
    *x
}

fn main() {
    let x: String = String::from("abc");
    let y: String = unchecked_copy::<dyn Sub<Assoc = String>>(&x);
    drop(x);
    println!("{y}");
}

It seems that the compiler is somehow concluding that T::Assoc = String and T::Assoc: Copy are true at the same time.

cc @lcnr

Meta

Reproducible on the playground with version 1.95.0-nightly (2026-02-12 47611e16044c68ef27ba)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied reproducer on the stated nightly version and inspect the compiler’s handling of associated type bounds inherited through trait objects. Trace the relevant trait-object and bound-checking entry points, then add coverage for this example. Done means the unsound program is rejected or otherwise handled soundly without allowing the use-after-free.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.