Rust "expected type" 错误打印完全相同的不匹配类型

Rust "expected type" error prints mismatched types that are exactly the same

夜间生锈:

Playground

struct Foo<T, F: Fn(&T, &T) -> T> {
    value: T,
    func: F
}

fn main() {
    let lambda = |&x, &y| x + y;
    let foo = Foo {
        value: 5 as i32,
        func: lambda
    };
}

错误信息:

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:8:15
  |
8 |     let foo = Foo {
  |               ^^^ one type is more general than the other
  |
  = note: expected type `std::ops::FnOnce<(&i32, &i32)>`
             found type `std::ops::FnOnce<(&i32, &i32)>`

请注意,预期类型和找到的类型是完全相同的字符。为什么错误消息说一种类型比另一种更通用,同时还说它们是同一类型?

With nightly rust:

这似乎只是夜间构建中的 "bad" 错误消息。在 Rust 1.32(稳定版)中,错误告诉您这是生命周期不匹配:

error[E0631]: type mismatch in closure arguments
 --> src/main.rs:8:15
  |
7 |     let lambda = |&x, &y| x + y;
  |                  -------------- found signature of `fn(&_, &_) -> _`
8 |     let foo = Foo {
  |               ^^^ expected signature of `for<'r, 's> fn(&'r i32, &'s i32) -> _`
  |
note: required by `Foo`
 --> src/main.rs:1:1
  |
1 | struct Foo<T, F: Fn(&T, &T) -> T> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0271]: type mismatch resolving `for<'r, 's> <[closure@src/main.rs:7:18: 7:32] as std::ops::FnOnce<(&'r i32, &'s i32)>>::Output == i32`
 --> src/main.rs:8:15
  |
8 |     let foo = Foo {
  |               ^^^ expected bound lifetime parameter, found concrete lifetime
  |
note: required by `Foo`
 --> src/main.rs:1:1
  |
1 | struct Foo<T, F: Fn(&T, &T) -> T> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Why is the error message saying that one type is more general than the other, while also saying that they are the same type?

这些类型仅在生命周期上有所不同。夜间消息不包括生命周期——也许是为了在生命周期不相关的情况下减少噪音。显然,当生命周期是类型之间的唯一区别时,这根本没有帮助。

考虑 reporting a bug 到 Rust 团队。