Skip to content

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

Closed

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    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.
    on Oct 31, 2020
  2. self-assigned this
    on Nov 2, 2020
  3. added
    AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
    on Dec 11, 2020
  4. tmandry commented on Jan 28, 2021

    @tmandry
    Member

    @rustbot remove-assignment

    @csmoe feel free to re-claim if you are working on this.

  5. added a commit that references this issue on Feb 17, 2021
    f7501b6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions