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

error: the trait core::marker::Sized is not implemented for the type std::error::Error + Sized + 'static #33201

Closed
julienw opened this issue Apr 25, 2016 · 6 comments · Fixed by #33138

Comments

@julienw
Copy link

julienw commented Apr 25, 2016

The error is not easy to understand for this code:

use std::error::Error;
use std::fmt;

#[derive(Debug, Clone)]
struct SomeError {
  cause: String
}

impl fmt::Display for SomeError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "SomeError")
  }
}

impl Error for SomeError {
  fn description(&self) -> &str {
    "Some Error"
  }

  fn cause(&self) -> Option<&Error> {
    None
  }
}

#[derive(Debug, Clone)]
enum NewError {
  SomeCause(Error + Sized)
}

fn main() {
    let error = NewError::SomeCause(SomeError { cause: "some cause".to_string() });
    println!("{:?}", error);
}

(it's also in https://play.rust-lang.org/?gist=ec4209f420275037c3f652a7aeae01ff&version=stable&mode=debug)

If I understand properly, the issue is that I should have something like this instead:

enum NewError<T: Error> {
  SomeCause(T)
}

But the error is very misleading.

@julienw
Copy link
Author

julienw commented Apr 25, 2016

Full error message:

<anon>:25:17: 25:22 error: the trait `core::marker::Sized` is not implemented for the type `std::error::Error + Sized + 'static` [E0277]
<anon>:25 #[derive(Debug, Clone)]
                          ^~~~~
<anon>:25:17: 25:22 note: in this expansion of #[derive_Clone] (defined in <anon>)
<anon>:25:17: 25:22 help: see the detailed explanation for E0277
<anon>:25:17: 25:22 note: `std::error::Error + Sized + 'static` does not have a constant size known at compile-time
<anon>:25:17: 25:22 note: required because it appears within the type `NewError`
<anon>:25:17: 25:22 note: required by `core::clone::Clone`
error: aborting due to previous error

@julienw
Copy link
Author

julienw commented Apr 25, 2016

If I understand properly, the issue is that I should have something like this instead:

So actually the error is that I have a Trait object that's not behind a pointer (Box or ref). As a result it's not sized even though I use + Sized. Maybe the error message could explain better why it doesn't have a constant size known at compile-time. (damned, every word is important :) )

@arielb1
Copy link
Contributor

arielb1 commented Apr 26, 2016

The + Sized being allowed is an implementation bug,

@arielb1
Copy link
Contributor

arielb1 commented Apr 27, 2016

duplicate of #33243.

@arielb1 arielb1 closed this as completed Apr 27, 2016
bors added a commit that referenced this issue May 6, 2016
Short-cut `T: Sized` trait selection for ADTs

Basically avoids all nested obligations when checking whether an ADT is sized - this speeds up typeck by ~15%

The refactoring fixed #32963, but I also want to make `Copy` not object-safe (will commit that soon).

Fixes #33201

r? @nikomatsakis
@dataf3l
Copy link

dataf3l commented May 24, 2018

@julienw can you share the example with the solution?

@julienw
Copy link
Author

julienw commented May 25, 2018

I described it at the end of my initial description, but here is the full code for the solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants