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

2023-12-29

夜间生锈:

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 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> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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

考虑报告错误 https://github.com/rust-lang/rust/issues给 Rust 团队。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐