Skip to content

Alternating error messages when compiling the same source #15034

@boschni

Description

@boschni

I'm getting alternating error messages when compiling the following source multiple times:

pub struct Lexer<'a> {
    input: &'a str,
}

impl<'a> Lexer<'a> {
    pub fn new<'a>(input: &'a str) -> Lexer<'a> {
        Lexer { input: input }
    }
}

struct Parser<'a> {
    lexer: &'a mut Lexer<'a>,
}

impl<'a> Parser<'a> {
    pub fn new<'a>(lexer: &'a mut Lexer) -> Parser<'a> {
        Parser { lexer: lexer }
    }
}

fn main() {}

First error message:

$ rustc test.rs
test.rs:20:2: 25:3 note: consider using an explicit lifetime parameter as shown: fn new<'a>(lexer: &'a mut Lexer<'a>) -> Parser<'a>
test.rs:20  pub fn new<'a>(lexer: &'a mut Lexer) -> Parser<'a> {
test.rs:21 
test.rs:22      Parser {
test.rs:23          lexer: lexer,
test.rs:24      }
test.rs:25  }
test.rs:22:3: 24:4 error: cannot infer an appropriate lifetime for lifetime parameter `'a due to conflicting requirements
test.rs:22      Parser {
test.rs:23          lexer: lexer,
test.rs:24      }
error: aborting due to previous error

Second error message:

$ rustc test.rs
test.rs:22:3: 24:4 error: cannot infer an appropriate lifetime for lifetime parameter `'a due to conflicting requirements
test.rs:22      Parser {
test.rs:23          lexer: lexer,
test.rs:24      }
test.rs:23:11: 23:16 note: first, the lifetime cannot outlive the expression at 23:10...
test.rs:23          lexer: lexer,
                           ^~~~~
test.rs:23:11: 23:16 note: ...so that automatically reference is valid at the time of borrow
test.rs:23          lexer: lexer,
                           ^~~~~
test.rs:20:53: 25:3 note: but, the lifetime must be valid for the lifetime 'a as defined on the block at 20:52...
test.rs:20  pub fn new<'a>(lexer: &'a mut Lexer) -> Parser<'a> {
test.rs:21 
test.rs:22      Parser {
test.rs:23          lexer: lexer,
test.rs:24      }
test.rs:25  }
test.rs:22:3: 24:4 note: ...so that types are compatible (expected `Parser<'a>` but found `Parser<'_>`)
test.rs:22      Parser {
test.rs:23          lexer: lexer,
test.rs:24      }
error: aborting due to previous error

It can take a couple of re-compilations to see the different message.

Rust version:
f8c9aec
rustc 0.11.0-pre
host: x86_64-apple-darwin

I was also able to reproduce this behavior on http://play.rust-lang.org/.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions