close
Skip to content

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

@theemathas

Description

@theemathas

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-dyn-traitArea: trait objects, vtable layoutC-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    open/unblocked

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions