Skip to content

Self::call constrains lifetime covariance and/or inference unexpectedly under NLL #62185

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
pnkfelix opened this issue Jun 27, 2019 · 1 comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Jun 27, 2019

Spawned off of #60914, namely the regression of the version-compare crate

Look at this (play):

use std::slice::Iter;

pub struct Version<'a> {
    parts: Vec<&'a str>,
}

impl<'a> Version<'a> {
    pub fn compare(&self, other: &Version) {
        Self::compare_iter(self.parts.iter(),  other.parts.iter())
    }
    
    fn compare_iter(_: Iter<&'a str>, _: Iter<&'a str>) { }
}

we get an NLL migration error for it.

  • (The use of Self::compare_iter is important on line 9; if you replace that with Version::compare_iter, then NLL will accept the input.)
  • ((Likewise, the input is accepted if you get rid of the 'a's in the formal parameters to fn compare_iter, but that is perhaps less surprising.))

Why? I'm not sure; at first I thought it made sense, since I assumed that the call to Self::compare_iter would force the two arguments to have the same lifetime 'a that is also attached to the self parameter.

But after reflecting on the matter further, I do not understand why we would not have a reborrow here that would allow the call to compare_iter to use a shorter lifetime that both inputs can satisfy.

  • (Or, wait: Is std::slice::Iter<T> not covariant with respect to T? I guess I'll need to check that.)

Anyway, this is either a soundness fix of AST borrow-check (which would be great news), or it is an NLL-complete bug for MIR borrow-check.

I just didn't want it to get lost in the shuffle of a blog post I am currently working on.

@pnkfelix pnkfelix added A-NLL Area: Non-lexical lifetimes (NLL) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 27, 2019
@pnkfelix
Copy link
Member Author

cc #60680

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant