Skip to content

Non trait object safe return diagnostic needs improvement #33375

Closed
@hythloday

Description

@hythloday

Here's some illegal code that generates a very reasonable error message:

trait Foo {
  fn apply() -> i64;
}

struct Baz{}
static BAZ: Baz = Baz{};
impl Foo for Baz {
    fn apply(&self) -> i64 { 4 }
}

// <anon>:8:5: 8:33 error: method `apply` has a `&self` declaration in the impl, but not in the trait [E0185]

Adding a function that references this broken trait changes the error message and is imho quite misleading:

fn lookup() -> Box<&'static Foo> {
    Box::new(&BAZ)
}

<anon>:11:1: 13:2 error: the trait `Foo` cannot be made into an object [E0038]
<anon>:11 fn lookup() -> Box<&'static Foo> {
<anon>:12     Box::new(&BAZ)
<anon>:13 }
<anon>:11:1: 13:2 help: see the detailed explanation for E0038
<anon>:11:1: 13:2 note: method `apply` has no receiver

The error code has changed from something that pinpoints exactly where I went wrong to something unrelated with advice that won't help me see my problem, and highlights the wrong place (the function that's returning this trait rather than the trait itself). Ideally the error message wouldn't change at all.

rustc 1.10.0-nightly (8da2bcac5 2016-04-28)
binary: rustc
commit-hash: 8da2bcac5db1e091b90cceb19d0496f0f7501c88
commit-date: 2016-04-28
host: x86_64-apple-darwin
release: 1.10.0-nightly

(happens on stable and beta too)

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-papercutDiagnostics: An error or lint that needs small tweaks.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