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

Poor labels for anonymous lifetimes in diagnostics #43288

Closed
jsgf opened this issue Jul 17, 2017 · 2 comments
Closed

Poor labels for anonymous lifetimes in diagnostics #43288

jsgf opened this issue Jul 17, 2017 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@jsgf
Copy link
Contributor

jsgf commented Jul 17, 2017

Given this (incorrect) code:

use std::collections::HashMap;

type MyMap<'a, 'b> = HashMap<&'a str, &'b str>;

fn foo(iter: &mut Iterator<Item=&str>, mut map: MyMap) {
    map.insert(iter.next().unwrap(), "value");
}

fn main() {
}

rustc reports:

   Compiling playground v0.0.1 (file:///playground)
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
 --> src/main.rs:6:28
  |
6 |     map.insert(iter.next().unwrap(), "value");
  |                            ^^^^^^
  |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the function body at 5:1...
 --> src/main.rs:5:1
  |
5 | / fn foo(iter: &mut Iterator<Item=&str>, mut map: MyMap) {
6 | |     map.insert(iter.next().unwrap(), "value");
7 | | }
  | |_^
note: ...so that types are compatible (expected std::option::Option<&str>, found std::option::Option<&str>)
 --> src/main.rs:6:28
  |
6 |     map.insert(iter.next().unwrap(), "value");
  |                            ^^^^^^
note: but, the lifetime must be valid for the anonymous lifetime #3 defined on the function body at 5:1...
 --> src/main.rs:5:1
  |
5 | / fn foo(iter: &mut Iterator<Item=&str>, mut map: MyMap) {
6 | |     map.insert(iter.next().unwrap(), "value");
7 | | }
  | |_^
note: ...so that reference does not outlive borrowed content
 --> src/main.rs:6:16
  |
6 |     map.insert(iter.next().unwrap(), "value");
  |                ^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

error: Could not compile `playground`.

The lifetime #1 notation is not very clear or helpful. Assuming it's referring to the lifetimes of the reference parameters, it would be better if it gave them names and used those, along the lines of:

Given:
fn foo<'a, 'b, 'c, 'd>(iter: &'a mut Iterator<Item=&'b str>, mut map: MyMap<'c, 'd>) {

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
 --> src/main.rs:6:28
  |
6 |     map.insert(iter.next().unwrap(), "value");
  |                            ^^^^^^
  |
note: first, the lifetime cannot outlive the anonymous lifetime 'b defined on the function body at 5:1...

(etc)
@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions labels Jul 19, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

estebank commented Jan 15, 2018

Current output:

error[E0623]: lifetime mismatch
 --> src/main.rs:6:16
  |
5 | fn foo(iter: &mut Iterator<Item=&str>, mut map: MyMap) {
  |                                 ----            -----
  |                                 |
  |                                 these two types are declared with different lifetimes...
6 |     map.insert(iter.next().unwrap(), "value");
  |                ^^^^^^^^^^^^^^^^^^^^ ...but data from `iter` flows into `map` here

The correct code would be

5 | fn foo<'a, 'b>(iter: &mut Iterator<Item=&'a str>, mut map: MyMap<'a, 'b>) {

@Mark-Simulacrum
Copy link
Member

I personally think that the current diagnostic is good enough here so I'm going to close this; the only improvement I could think of would be to indicate that MyMap has lifetimes attached to it via MyMap<'1, '2> but I believe we're already tracking that elsewhere and it'll presumably become increasingly common to specify them implicitly via '_ which'll make the diagnostic even clearer.

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 A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants