Skip to content

smarter blame assignment for lifetime inference errors #45979

@nikomatsakis

Description

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-lifetimesArea: Lifetimes / regions
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Nov 14, 2017
changed the title [-]smarter blame assignment for [/-] [+]smarter blame assignment for lifetime inference errors[/+] on Nov 14, 2017
cengiz-io

cengiz-io commented on Nov 14, 2017

@cengiz-io
Contributor

I'd like to work on this if possible

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Nov 14, 2017
estebank

estebank commented on Sep 13, 2018

@estebank
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cengiz-io@nikomatsakis@TimNN@estebank

        Issue actions

          smarter blame assignment for lifetime inference errors · Issue #45979 · rust-lang/rust