Unclear error note due to use of underscores when unifying paremeters to a single generic type
f023e04
Opened by Jakub Gedeon at
When I compile the following code:
fn test<T>(x: (T, Vec<T>)) {}
fn main() {
let param = ("hi", vec!(1, 2));
test(param);
}
I get the following error message:
rustc 1.18.0 (03fc9d622 2017-06-06)
error[E0308]: mismatched types
--> <anon>:5:10
|
5 | test(param);
| ^^^^^ expected &str, found integral variable
|
= note: expected type `(_, std::vec::Vec<_>)`
found type `(&str, std::vec::Vec<{integer}>)`
It is unclear that the types in the note aren't matching. (_, std::vec::Vec<_>) seems like it should correspond to (T, std::vec::Vec<S>), which would unify with (&str, std::vec::Vec<{integer}>) just fine. The overall error message does not provide the desired context to fix the problem, and requires that you look up what the specific types represented by the underscores are to understand why it is failing to typecheck.
Specifically, I think that the error should probably be something like this:
error[E0308]: mismatched types --> <anon>:5:10 | 5 | test(param); | ^^^^^ expected &str, found integral variable | = note: expected type `(T, std::vec::Vec<T>)` found type `(&str, std::vec::Vec<{integer}>)` = note: expected type comes from definition of `test`: fn test<T>(x: (T, Vec<T>))Mark Rousskov at 2017-07-19 17:00:58
Triage, we still use
_without tying them together, but we do show the fn signature now:error[E0308]: mismatched types --> src/main.rs:5:10 | 5 | test(param); | ---- ^^^^^ expected `(_, Vec<_>)`, found `(&str, Vec<{integer}>)` | | | arguments to this function are incorrect | = note: expected tuple `(_, Vec<_>)` found tuple `(&str, Vec<{integer}>)` note: function defined here --> src/main.rs:1:4 | 1 | fn test<T>(x: (T, Vec<T>)) {} | ^^^^ --------------Esteban Kuber at 2023-10-19 23:56:42