Skip to content

Incorrect object-safety with where Option<Self>: Sized #25204

Closed
@theemathas

Description

@theemathas

Code:

// This fails to compile:
// trait Foo: Sized {
// This compiles successfully
trait Foo where Option<Self>: Sized {
    fn some_fn(&self);
}

trait Foo2 where Box<Self>: Sized {
    fn some_fn2(&self);
}

struct Bar;
impl Foo for Bar {
    fn some_fn(&self) {}
}

impl Foo2 for Bar {
    fn some_fn2(&self) {}
}

fn option_sized<T: ?Sized>() where Option<T>: Sized {}

fn box_sized<T: ?Sized>() where Box<T>: Sized {}

fn main() {
    let x: Box<Bar> = Box::new(Bar);
    let y: Box<Foo> = x;
    // This fails to compile
    // y.some_fn();

    let x2: Box<Bar> = Box::new(Bar);
    let y2: Box<Foo2> = x2;
    // But this compiles successfully
    y2.some_fn2();

    // This also fails to compile
    // option_sized::<Foo>();

    // But this compiles successfully
    box_sized::<Foo>();
}

playpen

I'm not sure which line is the compiler wrong, but this combination of compile successes/failures is very inconsistent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions