Repository: rust
Issue: Misleading error message with Trait and unimplemented std::marker::Sized
Labels: C-enhancement,A-diagnostics,A-trait-system,T-compiler

When doing something like this

``` rust
use std::path::{Path, PathBuf};

pub trait Control {
    type Output;
    fn open<P: AsRef<Path>>(path: P) -> Result<Self::Output, String>;
}

#[derive(Debug)]
pub struct Foo {
    path: Path,
}

impl Control for Foo {
    type Output = Self;

    fn open<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        Ok(Foo {
            path: path.as_ref().into(),
        })
    }
}

fn main() {
    let path = Path::new("/test");
    let c = Foo::open(path);
    print!("{:?}", c);
}
```
> [Playground Link](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=4f343d8cacd8c0d40e889a389c17d42c)

you get an error like this
```
Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/main.rs:14:6
   |
14 | impl Control for Foo {
   |      ^^^^^^^ doesn't have a size known at compile-time
   |
   = help: within `Foo`, the trait `std::marker::Sized` is not implemented for `[u8]`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required because it appears within the type `Foo`

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/main.rs:17:5
   |
17 | /     fn open<P: AsRef<Path>>(path: P) -> Result<Self, String> {
18 | |         Ok(Foo {
19 | |             path: path.as_ref(),
20 | |         })
21 | |     }
   | |_____^ doesn't have a size known at compile-time
   |
   = help: within `Foo`, the trait `std::marker::Sized` is not implemented for `[u8]`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required because it appears within the type `Foo`
   = note: required by `std::result::Result`

```

---
I would expect an error like the one from this code
``` rust
use std::path::{Path, PathBuf};

pub trait Control<'a> {
    type Output;
    fn open<P: 'a + AsRef<Path>>(path: P) -> Result<Self::Output, String>;
}

pub struct Foo<'a> {
    path: &'a Path,
}

impl<'a> Control<'a> for Foo<'a> {
    type Output = Self;

    fn open<P: 'a + AsRef<Path>>(path: P) -> Result<Self, String> {
        Ok(Foo {
            path: path.as_ref(),
        })
    }
}

fn main() {
    let path  = PathBuf::from("/test");
    let c = Foo::open(path  );
}
```
> [Playground Link](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=49205816abdac866ddf01a7bd33499d5)

```
error[E0515]: cannot return value referencing function parameter `path`
  --> src/main.rs:16:9
   |
16 | /         Ok(Foo {
17 | |             path: path.as_ref(),
   | |                   ---- `path` is borrowed here
18 | |         })
   | |__________^ returns a value referencing data owned by the current function
```

*Edit:* Sorry, had the wrong error output and playground link on the first example