Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
CAD97 opened this issue Jan 6, 2023 · 1 comment
Closed

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

CAD97 opened this issue Jan 6, 2023 · 1 comment
Labels
A-trait-objects Area: trait objects, vtable layout C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Jan 6, 2023

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.

@CAD97 CAD97 added the C-bug Category: This is a bug. label Jan 6, 2023
@fmease fmease added T-types Relevant to the types team, which will review and decide on the PR/issue. A-trait-objects Area: trait objects, vtable layout and removed needs-triage-legacy labels Jan 26, 2024
@fmease
Copy link
Member

fmease commented Jan 26, 2024

The code you posted:

pub trait Trait {
    type Assoc
    where
        Self: Sized;

    fn method(&self);
}

nowadays compiles successfully, namely since oli's PR #112319. Closing as completed then.

@fmease fmease closed this as completed Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-objects Area: trait objects, vtable layout C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants