Skip to content

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

Closed
@nox

Description

@nox

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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