Rust“预期类型”错误打印出完全相同的不匹配类型

问题描述 投票:1回答:1

夜间生锈:

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)>`

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

rust lifetime typechecking
1个回答
1
投票

夜间生锈:

这似乎只是每晚构建中的“错误”错误消息。在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> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

为什么错误消息说一种类型比另一种更通用,同时还说它们是相同的类型?

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

考虑reporting a bug到Rust团队。

© www.soinside.com 2019 - 2024. All rights reserved.