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

Unhelpful Error Messages for trait objects due to trait bounds #38124

Closed
theduke opened this issue Dec 2, 2016 · 3 comments
Closed

Unhelpful Error Messages for trait objects due to trait bounds #38124

theduke opened this issue Dec 2, 2016 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@theduke
Copy link
Contributor

theduke commented Dec 2, 2016

When a struct cannot be turned into a trait object due to a trait bound,
the compiler errors are very cryptic and unhelpful.

trait T: Clone {}

#[derive(Clone)]
struct S {}

fn main() {
  let x: &T = & S{};
}

This results in the following errors (both on stable and nightly):

rustc 1.13.0 (2c6933acc 2016-11-07)
error[E0038]: the trait `T` cannot be made into an object
  --> <anon>:12:15
   |
12 |   let x: &T = & S{};
   |               ^^^^^ the trait `T` cannot be made into an object
   |
   = note: the trait cannot require that `Self : Sized`
   = note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&T>` for `&S`

error[E0038]: the trait `T` cannot be made into an object
  --> <anon>:12:10
   |
12 |   let x: &T = & S{};
   |          ^^ the trait `T` cannot be made into an object
   |
   = note: the trait cannot require that `Self : Sized`

error: aborting due to 2 previous errors

The problem lies with the : Clone trait bound, since Clone is not object safe.

But the errors do not mention Clone at all, which leaves someone relatively new to Rust (like me) completely stranded and puzzled as to what might be the problem.

Better errors here would be a big win.

My ideal error would be something like:

the trait 'T' cannot be made into an object. 
...
note:  T requires the trait bound 'Clone', which cannot be made into an object`.
@theduke theduke changed the title Unhelpful Error Messages for object safety issues Unhelpful Error Messages for trait objects due to trait bounds Dec 2, 2016
@sanxiyn sanxiyn added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 2, 2016
@llogiq
Copy link
Contributor

llogiq commented Feb 12, 2017

Another data point (playground)

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

estebank commented Mar 6, 2019

CC #38376

Current status: no change.

I believe this lead the dev towards the following:

trait Selectable : Sized {
    fn get_fd(&self) -> RawFd;
    fn on_read(self, _: &mut Server<Self>) -> Option<Self>;
    fn on_write(self, _: &mut Server<Self>) -> Option<Self>;
    fn on_except(self, _: &mut Server<Self>) -> Option<Self>;
}

struct Server<T: Selectable>{
    readfds:VecDeque<Box<T>>,
    writefds:VecDeque<Box<T>>,
    exceptfds:VecDeque<Box<T>>,
}

@estebank estebank added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Jan 10, 2020
@estebank
Copy link
Contributor

The current output now points at Clone:

error[E0038]: the trait `T` cannot be made into an object
 --> src/main.rs:7:15
  |
1 | trait T: Clone {}
  |       -  ----- ...because it requires `Self: Sized`
  |       |
  |       this trait cannot be made into an object...
...
7 |   let x: &T = & S{};
  |               ^^^ the trait `T` cannot be made into an object
  |
  = note: required because of the requirements on the impl of `std::ops::CoerceUnsized<&dyn T>` for `&S`
  = note: required by cast to type `&dyn T`

error[E0038]: the trait `T` cannot be made into an object
 --> src/main.rs:7:10
  |
1 | trait T: Clone {}
  |       -  ----- ...because it requires `Self: Sized`
  |       |
  |       this trait cannot be made into an object...
...
7 |   let x: &T = & S{};
  |          ^^ the trait `T` cannot be made into an object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants