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

Confusing errors when using Self variants with lifetimes #69224

Open
clarfonthey opened this issue Feb 17, 2020 · 1 comment
Open

Confusing errors when using Self variants with lifetimes #69224

clarfonthey opened this issue Feb 17, 2020 · 1 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-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@clarfonthey
Copy link
Contributor

clarfonthey commented Feb 17, 2020

Minimal example, imagine that we have the following type:

enum MyCow<'a> {
    Owned(String),
    Borrowed(&'a str),
}

If the user returns Self variants, this fails:

impl<'a> MyCow<'a> {
    fn borrow(&self) -> MyCow<'_> {
        match self {
            Self::Owned(s) => Self::Borrowed(s),
            Self::Borrowed(s) => Self::Borrowed(s),
        }
    }
    fn own(&self) -> MyCow<'static> {
        match self {
            Self::Owned(s) => Self::Owned(s.clone()),
            Self::Borrowed(s) => Self::Owned(s.to_string()),
        }
    }
}

But the same code works when you change Self to MyCow!

impl<'a> MyCow<'a> {
    fn borrow(&self) -> MyCow<'_> {
        match self {
            Self::Owned(s) => MyCow::Borrowed(s),
            Self::Borrowed(s) => MyCow::Borrowed(s),
        }
    }
    fn own(&self) -> MyCow<'static> {
        match self {
            Self::Owned(s) => MyCow::Owned(s.clone()),
            Self::Borrowed(s) => MyCow::Owned(s.to_string()),
        }
    }
}

The real error here is that Self really represents MyCow<'a> and not MyCow<'_> or MyCow<'static>-, but the errors don't really relay this. Potentially linked issue: #30904 (has been fixed).

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 17, 2020
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Jul 1, 2020
@clarfonthey
Copy link
Contributor Author

FWIW this is still an issue on the latest stable: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c6da051f190e76b03a995df938a86f2c

Also potentially related to #86555, even though the two are not technically the same. We could potentially just allow the version that uses Self here.

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-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants