Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Closed
theemathas opened this issue May 8, 2015 · 11 comments
Closed

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

theemathas opened this issue May 8, 2015 · 11 comments
Labels
A-type-system Area: Type system

Comments

@theemathas
Copy link
Contributor

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.

@theemathas
Copy link
Contributor Author

Another weird case:

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

fn main() {}

playpen

Error:

<anon>:25:1: 25:60 error: the trait `core::marker::Sized` is not implemented for the type `T` [E0277]
<anon>:25 fn weird<T: ?Sized>(_: Option<T>) where Option<T>: Sized {}
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:25:1: 25:60 note: `T` does not have a constant size known at compile-time
<anon>:25 fn weird<T: ?Sized>(_: Option<T>) where Option<T>: Sized {}
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@arielb1
Copy link
Contributor

arielb1 commented May 8, 2015

@theemathas

The compiler does not deduce T: Sized from Option<T>: Sized.

@theemathas
Copy link
Contributor Author

@arielb1 I pretty much figured out that the compiler only sometimes does that. I would prefer it to always do it, or never do it.

I expect the current code to have a compile error on this line:

let y: Box<Foo> = x;

@theemathas
Copy link
Contributor Author

I have realized that the weird function is not a bug, since Option requries its type parameter to be Sized.

However, this code somehow compiles:
http://is.gd/SzkZiN

@steveklabnik steveklabnik added the A-type-system Area: Type system label May 10, 2015
@Kimundi
Copy link
Member

Kimundi commented May 15, 2015

@theemathas Hm, I'd expect that code to compile, and only allow instance of Wrap<T> with Sized T.

@theemathas
Copy link
Contributor Author

Possibly related to #19182

@arielb1
Copy link
Contributor

arielb1 commented Jun 9, 2015

@theemathas

Its not the same issue, but the problem is that object safety does not consider non-supertrait predicates.

@arielb1
Copy link
Contributor

arielb1 commented Aug 14, 2015

related to #27675

@pnkfelix
Copy link
Member

I think the compiler is now properly flagged this code under the warnings issued by RFC 1214:

trait OptSelfSized where Option<Self>: Sized { }

yields:

wf-15.rs:5:1: 5:49 warning: the trait `core::marker::Sized` is not implemented for the type `Self` [E0277]
wf-15.rs:5 trait OptSelfSized where Option<Self>: Sized { }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wf-15.rs:5:1: 5:49 help: run `rustc --explain E0277` to see a detailed explanation
wf-15.rs:5:1: 5:49 note: `Self` does not have a constant size known at compile-time
wf-15.rs:5:1: 5:49 note: this warning results from recent bug fixes and clarifications; it will become a HARD ERROR in the next release. See RFC 1214 for details.
wf-15.rs:5 trait OptSelfSized where Option<Self>: Sized { }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wf-15.rs:5:1: 5:49 note: required by `core::option::Option`

@Mark-Simulacrum
Copy link
Member

This appears to be fixed, but I'm not certain.

@Mark-Simulacrum
Copy link
Member

Closing, please reopen if this is still an issue.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

6 participants