Skip to content

Trait associated types where Self: Sized interact oddly with dyn safety #106522

Closed
@CAD97

Description

@CAD97

Given the trait

pub trait Trait {
    type Assoc
    where
        Self: Sized;

    fn method(&self);
}

we get that

  • &dyn Trait is not valid,
    error[E0191]: the value of the associated type `Assoc` (from trait `Trait`) must be specified
     --> src/lib.rs:9:21
      |
    2 |     type Assoc
      |     ---------- `Assoc` defined here
    ...
    9 | pub fn test(t: &dyn Trait) {
      |                     ^^^^^ help: specify the associated type: `Trait<Assoc = Type>`
  • &dyn Trait<Assoc=()> is valid,
  • but trying to call methods fails.
    error[E0277]: the size for values of type `dyn Trait<Assoc = ()>` cannot be known at compilation time
      --> src/lib.rs:10:5
       |
    10 |     t.method();
       |     ^ ------ required by a bound introduced by this call
       |     |
       |     doesn't have a size known at compile-time
       |
       = help: the trait `Sized` is not implemented for `dyn Trait<Assoc = ()>`

The error message says that the call to Trait::method introduces a Self: Sized bound. There is no such bound on method, however; the bound is on Trait::Assoc.

I have no clue what the "correct" behavior is for this, but it's probably not this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-dyn-traitArea: trait objects, vtable layoutC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions