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

Format self correctly #42700

Open
gaurikholkar-zz opened this issue Jun 16, 2017 · 10 comments
Open

Format self correctly #42700

gaurikholkar-zz opened this issue Jun 16, 2017 · 10 comments
Labels
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. WG-diagnostics Working group: Diagnostics

Comments

@gaurikholkar-zz
Copy link

gaurikholkar-zz commented Jun 16, 2017

Fix the formatting of self in the error message.

struct Foo {
  field: i32
}

impl Foo {

fn foo<'a>(&self, x: &'a Foo) -> &'a Foo {

    if true { self } else { x }
    
    // now gives:
// error[E0611]: explicit lifetime required in the type of `self`
//    |
// 20 |     fn foo<'a>(&self, x: &'a Foo) -> &'a Foo {
//    |                  ^^^^^ consider changing the type of `self` to `&'a Foo`
// ...
// 25 |         if true { self } else { x }
//    |                   ---- lifetime `'a` required

    // should actually:
// error[E0611]: explicit lifetime required in the type of `self`
//    |
// 20 |     fn foo<'a>(&self, x: &'a Foo) -> &'a Foo {
//    |                  ^^^^^ consider changing to `&'a self`
// ...
// 25 |         if true { self } else { x }
//    |                   ---- lifetime `'a` required
  }
  
}
^^^^^ consider changing the type of `self` to `&'a Foo`

should actually be

^^^^^ consider changing to `&'a self`

cc @nikomatsakis

@sfackler sfackler added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 16, 2017
@nikomatsakis
Copy link
Contributor

Once @gaurikholkar's PR lands, I'll try to write up some mentoring instructions for this.

@nikomatsakis
Copy link
Contributor

So actually, this test:

struct Foo {
  field: i32
}

impl Foo {
  fn foo<'a>(&self, x: &'a Foo) -> &'a Foo {
    if true { self } else { x }
  }
}

fn main() { }

presently generates an "old-style" inference error:

error[E0312]: lifetime of reference outlives lifetime of borrowed content...

This is because we ruled out self arguments here. I think we would want to accept the case where the parameter whose type needs to be changed is self, first, but we have to be careful about cases where the anonymous region appears in both the self type and the return type -- we probably need to exclude that case, for the reasons discussed in #42703.

@nikomatsakis
Copy link
Contributor

Here is an example where there is no potential confusion around the return type, but we still currently use the old-school errors:

struct Foo {
  field: i32
}

impl Foo {
  fn foo<'a>(&self, mut x: Vec<&'a Foo>) {
    x.push(self);
  }
}

fn main() { }

I think we should present this error as:

// error[E0611]: explicit lifetime required in the type of `self`
//    |
// 20 |     fn foo<'a>(&self, mut x: Vec<&'a Foo>) {
//    |                ^^^^^ consider changing to `&'a self`
// ...
// 25 |         x.push(self);
//    |                 ---- lifetime `'a` required

@nikomatsakis
Copy link
Contributor

Main difference is the wording, we don't want to say "change the type of self to &'a Foo".

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@cengiz-io
Copy link
Contributor

Hello @nikomatsakis, hello @gaurikholkar

I've picked this up. Will ping you if needed 😛

@gaurikholkar-zz
Copy link
Author

@nikomatsakis a small chunk of these messages are also triggering E0623 now as per the return type and self PR right? I'm guessing the adding of EBR and LBR has also reduced this set of messages.

@estebank estebank added the WG-diagnostics Working group: Diagnostics label Oct 13, 2017
@estebank
Copy link
Contributor

estebank commented Jan 5, 2018

Current output:

error[E0623]: lifetime mismatch
 --> src/main.rs:9:15
  |
7 | fn foo<'a>(&self, x: &'a Foo) -> &'a Foo {
  |            -----                 -------
  |            |
  |            this parameter and the return type are declared with different lifetimes...
8 | 
9 |     if true { self } else { x }
  |               ^^^^ ...but data from `self` is returned here

@nikomatsakis
Copy link
Contributor

It may be that the current output is good enough. The bug was filed to give a suggestion like "try changing to &'a self.

@estebank
Copy link
Contributor

Current output with NLL:

error: unsatisfied lifetime constraints
  --> src/lib.rs:10:15
   |
8  | fn foo<'a>(&self, x: &'a Foo) -> &'a Foo {
   |        --  - let's call the lifetime of this reference `'1`
   |        |
   |        lifetime `'a` defined here
9  | 
10 |     if true { self } else { x }
   |               ^^^^ function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`

I believe we should add the suggestion back, proposing constraining the &self borrow to 'a, both with NLL enabled and disabled.

@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
@estebank
Copy link
Contributor

estebank commented Jan 8, 2023

Triage: no change.

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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

7 participants