Skip to content

Improve error message for "return type cannot contain a projection or Self..." #78600

Closed
@gliderkite

Description

@gliderkite

Following the improvement for the diagnostic of the error reported in this issue #69276, it would be nice (depending on how #61949 is prioritized) to have the same error message when instead of returning Self the function returns Result<Self, Error>.

When Self is returned I get a nice error message (on rustc 1.47):

struct S<'a>(&'a i32);

impl<'a> S<'a> {
    async fn new(i: &'a i32) -> Self {
        S(&22)
    }
}
error[E0760]: `async fn` return type cannot contain a projection or `Self` that references lifetimes from a parent scope
 --> src/lib.rs:4:33
  |
4 |     async fn new(i: &'a i32) -> Self {
  |                                 ^^^^ help: consider spelling out the type instead: `S<'a>`

But if Result<Self, Error> is used I get:

struct S<'a>(&'a i32);

impl<'a> S<'a> {
    async fn new(i: &'a i32) -> Result<Self, ()> {
        Ok(S(&22))
    }
}
error[E0760]: `async fn` return type cannot contain a projection or `Self` that references lifetimes from a parent scope
 --> src/lib.rs:4:33
  |
4 |     async fn new(i: &'a i32) -> Result<Self, ()> {
  |                                 ^^^^^^^^^^^^^^^^

with no suggestion to spell the type.

Link to Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions