Skip to content

Confusing error involving lifetime elision #79879

Open
@ZainlessBrombie

Description

@ZainlessBrombie

The following code:

struct A<'a> {
    backref: Option<&'a mut A<'a>>,
    value: String,
}

impl<'a> A<'a> {
    fn child_with_value(&'a mut self, value: String) -> A<'a> {
        A {
            backref: Some(self),
            value,
        }
    }
}

fn works(a: &mut A) {
    println!("{}", a.value);
}

fn errors(a: &mut A) {
    let mut b = a.child_with_value("foo".into());
    errors(&mut b);
}

Gives this error:
image

I expected to see this happen:
Given that the invocation should be legal, I expect this to compile without issues :)

Instead, this happened: Compiler error above. Note that the variable flows into itself, which seems strange

Meta

Tested both on stable and nightly:
rustc --version --verbose:

rustc 1.50.0-nightly (f0f68778f 2020-12-09)
binary: rustc
commit-hash: f0f68778f798d6d34649745b41770829b17ba5b8
commit-date: 2020-12-09
host: x86_64-unknown-linux-gnu
release: 1.50.0-nightly

Backtrace not applicable I think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions