Skip to content

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

@theduke

Description

@theduke

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`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions