Skip to content

Non-Deterministic Lifetime Error Message #17872

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
maxsnew opened this issue Oct 8, 2014 · 2 comments
Closed

Non-Deterministic Lifetime Error Message #17872

maxsnew opened this issue Oct 8, 2014 · 2 comments
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@maxsnew
Copy link

maxsnew commented Oct 8, 2014

I'm getting this on Fedora 20 with rustc 0.12.0-nightly (b5ba2f5 2014-10-06 20:27:14 +0000).

The following program:

extern crate graphviz;

use graphviz as dot;
use std::str;

type Nd = uint;
type Ed = (uint, uint);
struct Graph<'a> {
  nodes: Vec<&'a str>,
  edges: Vec<Ed>,
}

impl<'a> dot::Labeller<'a, Nd, Ed> for Graph<'a> {
  fn graph_id(&self) -> dot::Id<'a> {
    dot::Id::new("example3")
  }
  fn node_id(&self, n: &Nd) -> dot::Id {
    dot::Id::new(format!("N{:u}", *n))
  }
  fn node_label<'a>(&self, i: &Nd) -> dot::LabelText<'a> {
    dot::LabelStr(str::Slice(self.nodes[*i].as_slice()))
  }
}

When compiled with rustc --crate-type=lib non-deterministically gives one of 2 error messages.
A useful one:

lifetime-error.rs:21:5: 23:6 note: consider using an explicit lifetime parameter as shown: fn node_label<'a>(&'a self, i: &Nd) -> dot::LabelText<'a>

and a buggy one, thats suggested fix is what's already written in the program:

lifetime-error.rs:21:5: 23:6 note: consider using an explicit lifetime parameter as shown: fn node_label<'a>(&self, i: &Nd) -> dot::LabelText<'a>

On my Mac, I always get the worse error message.

I think this is the same issue described here: #13057

@ghost ghost added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Nov 11, 2014
@tamird
Copy link
Contributor

tamird commented Mar 15, 2015

This suggestion has actually gotten worse. I now consistently get a nonsense message:

#![feature(rustc_private)]

extern crate graphviz;

use std::borrow::IntoCow;
use graphviz as dot;

type Nd = usize;
type Ed = (usize, usize);
struct Graph<'a> {
    nodes: Vec<&'a str>,
    edges: Vec<Ed>,
}

impl<'a> dot::Labeller<'a, Nd, Ed> for Graph<'a> {
    fn graph_id(&self) -> dot::Id {
        dot::Id::new("example2").unwrap()
    }

    fn node_id(&self, n: &Nd) -> dot::Id {
        dot::Id::new(format!("N{}", n)).unwrap()
    }

    fn node_label<'b>(&self, n: &Nd) -> dot::LabelText<'b> {
        dot::LabelText::LabelStr(self.nodes[*n].into_cow())
        // error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
        // help: consider using an explicit lifetime parameter as shown: fn node_label(&self, n: &Nd) -> dot::LabelText<'a>
    }
}

fn main() { }

...the compiler is suggesting to use a different lifetime parameter, not just an explicit one.

@steveklabnik
Copy link
Member

These examples do not compile for other reasons, but in the 18 months since it was filed, we did some updates on shadowing of lifetimes. You now get:

src/lib.rs:20:19: 20:21 error: lifetime name `'a` shadows a lifetime name that is already in scope [E0496]
src/lib.rs:20     fn node_label<'a>(&self, i: &Nd) -> dot::LabelText<'a> {
                                ^~
src/lib.rs:20:19: 20:21 help: run `rustc --explain E0496` to see a detailed explanation
src/lib.rs:13:6: 13:8 note: shadowed lifetime `'a` declared here
src/lib.rs:13 impl<'a> dot::Labeller<'a, Nd, Ed> for Graph<'a> {
                   ^~

Much better. As such I'm going to give it a close!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

3 participants