Skip to content

smarter blame assignment for lifetime inference errors #45979

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

Closed
nikomatsakis opened this issue Nov 14, 2017 · 2 comments
Closed

smarter blame assignment for lifetime inference errors #45979

nikomatsakis opened this issue Nov 14, 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@nikomatsakis
Copy link
Contributor

Consider this example (try it on play):

#![feature(match_default_bindings)]

use std::cell::RefCell;
use std::collections::HashMap;

struct Cx<'cx> {
  types: RefCell<HashMap<&'cx str, String>>,
}

enum Lvalue<'cx> {
  Var(&'cx str),
  Deref(Box<Lvalue<'cx>>),
}

impl<'cx> Lvalue<'cx> {
  fn ty(&self, cx: &Cx<'cx>) -> String {
    match self {
      Lvalue::Deref(base) => format!("&{}", base.ty(cx)),
      Lvalue::Var(v) => cx.types.borrow()[v].clone(),
    }
  }
}

fn describe<'cx>(lvalue: Lvalue, cx: &Cx<'cx>) -> String {
    match lvalue {
      Lvalue::Deref(base) => format!("deref of {} pointer", base.ty(cx)),
      Lvalue::Var(v) => format!("variable `{}`", v),
    }
}

fn main() { }

I get the error:

error[E0621]: explicit lifetime required in the type of `lvalue`
  --> src/main.rs:26:69
   |
24 | fn describe<'cx>(lvalue: Lvalue, cx: &Cx<'cx>) -> String {
   |                  ------ consider changing the type of `lvalue` to `Lvalue<'cx>`
25 |     match lvalue {
26 |       Lvalue::Deref(base) => format!("deref of {} pointer", base.ty(cx)),
   |                                                                     ^^ lifetime `'cx` required

This is pretty good, but I think it would be better if we moved the highlight off of cx, which has the correct type, and over perhaps to base, which does not, or perhaps to the function call ty:

error[E0621]: explicit lifetime required in the type of `lvalue`
  --> src/main.rs:26:69
   |
24 | fn describe<'cx>(lvalue: Lvalue, cx: &Cx<'cx>) -> String {
   |                  ------ consider changing the type of `lvalue` to `Lvalue<'cx>`
25 |     match lvalue {
26 |       Lvalue::Deref(base) => format!("deref of {} pointer", base.ty(cx)),
   |                                                                  ^^ lifetime `'cx` required

I think a better analysis of the region inference graph could tell us what to highlight. Right now I believe we blame the edge that introduced the unsatisfiable constraint. In this case, the edge that introduced 'cx corresponds to the parameter cx. But we really want to blame the edge that brought the unsatisfiable constraint into "tension", which I think is generally the point where contravariance is introduced (iow, parameters to function calls). But I suspect we can get decent results with just a simple weighting of the causes for introducing each edge.

@nikomatsakis nikomatsakis added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 14, 2017
@nikomatsakis nikomatsakis changed the title smarter blame assignment for smarter blame assignment for lifetime inference errors Nov 14, 2017
@cengiz-io
Copy link
Contributor

I'd like to work on this if possible

@TimNN TimNN added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Nov 14, 2017
@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Dec 7, 2017
@estebank
Copy link
Contributor

Fixed:

error[E0621]: explicit lifetime required in the type of `lvalue`
  --> src/main.rs:26:69
   |
24 | fn describe<'cx>(lvalue: Lvalue, cx: &Cx<'cx>) -> String {
   |                          ------ help: add explicit lifetime `'cx` to the type of `lvalue`: `Lvalue<'cx>`
25 |     match lvalue {
26 |       Lvalue::Deref(base) => format!("deref of {} pointer", base.ty(cx)),
   |                                                                     ^^ lifetime `'cx` required

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. 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

4 participants