Skip to content

Confusing error msg for lifetime mismatch (possibly a bug in lifetime inference as well) #32008

Closed
@daniel-vainsencher

Description

@daniel-vainsencher

Originally opened in wrong repository (rfcs) as [0].

The problem is demonstrated by compiling [1]. I can work around the problem by using [2](first half of the diff is relevant to this problem, second part not so much), or by patching ndarrays to give parameters of binary operators distinct lifetime parameters.

A few problems here:

  1. The basic error message is not very clear:
src/lib.rs:30:28: 30:38 error: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements [E0495]
src/lib.rs:30         let delta = data - &self.mean;
<arrow pointing at &self.mean>

Lifetime of what expression cannot be inferred? What are the conflicting requirements? as someone with an only approximate mental model of lifetimes (i.e., a typical rust user) this is not helping me a lot to solve the problem nor to learn about lifetimes.

  1. The compiler suggests something that doesn't work. I don't expect suggestions to always work, but maybe its worth pointing this case out so that the suggestion can be improved or made conditional.
src/lib.rs:27:5: 38:6 help: consider using an explicit lifetime parameter as shown: fn next_value<'a>(&'a mut self, data: &'a V, weight: f64)
src/lib.rs:27     pub fn next_value(&mut self, data : &V, weight : f64) {
  1. @bluss suggested that rustc should be able to infer for the self and data borrows the intersection of their lifetimes, hence this should not be happening at all. I have no idea.
  2. The work around in [2] suggested by @bluss leaves me even more confused. I can change the lifetime of data by ... (what is that, actually, reborrowing it?), and then the borrow of self is ok? But if that's a good way to go about things, maybe the compiler should suggest this method sometimes.

Anyway, if any part of the documentation (error: no extended information for [E0495]) can enlighten me, pointers accepted gladly.

[0] rust-lang/rfcs#1523 (comment)
[1] daniel-vainsencher/online_weighted_stats@8f9cf03
[2] daniel-vainsencher/online_weighted_stats@0bfc04d

@bluss said: I think this is a representative minimization.
https://play.rust-lang.org/?gist=59e6b8c113e4e7eda1fe&version=stable

I get completely different error messages at that playground link, btw,
though still somewhat confusing: it is not clear why the block suffix does
not suffice, seems to cover the relevant part of the anonymous lifetime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions