Skip to content

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

Closed
@daniel-vainsencher

Description

@daniel-vainsencher

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.
  2. 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) {
  3. @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.
  4. 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.

[1] daniel-vainsencher/online_weighted_stats@8f9cf03
[2] daniel-vainsencher/online_weighted_stats@0bfc04d

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions