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

Suggest using a struct's name instead of Self #102840

Closed
nox opened this issue Oct 9, 2022 · 2 comments
Closed

Suggest using a struct's name instead of Self #102840

nox opened this issue Oct 9, 2022 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nox
Copy link
Contributor

nox commented Oct 9, 2022

Given the following code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3c81ead494d0897d6cd49f20d51302df

pub struct StrWriter<'w, W> {
    inner: &'w mut W,
}

impl<'w, W> StrWriter<'w, W> {
    pub fn reborrow(&mut self) -> StrWriter<'_, W> {
        Self {
            inner: self.inner,
        }
    }

    pub fn reborrow_explicit_lts<'a>(&'a mut self) -> StrWriter<'a, W> {
        Self {
            inner: self.inner,
        }
    }
}

The current output is:

error: lifetime may not live long enough
 --> src/lib.rs:8:20
  |
5 | impl<'w, W> StrWriter<'w, W> {
  |      -- lifetime `'w` defined here
6 |     pub fn reborrow(&mut self) -> StrWriter<'_, W> {
  |                     - let's call the lifetime of this reference `'1`
7 |         Self {
8 |             inner: self.inner,
  |                    ^^^^^^^^^^ this usage requires that `'1` must outlive `'w`

error: lifetime may not live long enough
  --> src/lib.rs:14:20
   |
5  | impl<'w, W> StrWriter<'w, W> {
   |      -- lifetime `'w` defined here
...
12 |     pub fn reborrow_explicit_lts<'a>(&'a mut self) -> StrWriter<'a, W> {
   |                                  -- lifetime `'a` defined here
13 |         Self {
14 |             inner: self.inner,
   |                    ^^^^^^^^^^ this usage requires that `'a` must outlive `'w`
   |
   = help: consider adding the following bound: `'a: 'w`

Ideally the output should look like:

error: lifetime of `Self` does not live long enough
 --> src/lib.rs:8:20
  |
5 | impl<'w, W> StrWriter<'w, W> {
  |      -- lifetime `'w` defined here
6 |     pub fn reborrow(&mut self) -> StrWriter<'_, W> {
  |                     - let's call the lifetime of this reference `'1`
7 |         Self {
7 |         ^^^^ help: build the struct value with `StrWriter` instead of `Self`
8 |             inner: self.inner,
  |                    ^^^^^^^^^^ this usage requires that `'1` must outlive `'w`

error: lifetime of `Self` does not live long enough
  --> src/lib.rs:14:20
   |
5  | impl<'w, W> StrWriter<'w, W> {
   |      -- lifetime `'w` defined here
...
12 |     pub fn reborrow_explicit_lts<'a>(&'a mut self) -> StrWriter<'a, W> {
   |                                  -- lifetime `'a` defined here
13 |         Self {
   |         ^^^^ help: build the struct value with `StrWriter` instead of `Self`
14 |             inner: self.inner,
   |                    ^^^^^^^^^^ this usage requires that `'a` must outlive `'w`

Using Self instead of the struct name when self and the return type of the method only differs in lifetimes will never work, and the user must write the returned struct value with the struct name, without referring to Self. The help note for the reborrow_explicit_lts is especially egregious because adding the bound 'a: 'w actually makes the method completely defeat its own purpose of returning the same value with a shorter lifetime.

@nox nox added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 9, 2022
@aliemjay
Copy link
Member

aliemjay commented Oct 9, 2022

Thanks for reporting. This issue is tracked in #101393 and should be fixed by #100976. You may want to close it as a duplicate.

@nagisa
Copy link
Member

nagisa commented Oct 9, 2022

Duplicate of #101393

@nagisa nagisa marked this as a duplicate of #101393 Oct 9, 2022
@nagisa nagisa closed this as completed Oct 9, 2022
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants