Unsatisfied trait bound for inner type in result leads to imprecise error message
Given a function that returns an instance of a generic expecting an inner type to implement a trait but that bound is not satisfied, the compiler unhelpfully points to the entire function and does not specify which inner type is the root of the problem.
Sample code (full error case on git):
fn test() -> MapErr<(), ()> {
future::ok(())
.map_err(|String| ())
}
MapErr is a type that expects its first type parameter to implement the Future trait, () does not do so.
The compiler generates the following:
Compiling futuretest v0.1.0 (file:///mnt/c/Users/Mahmoud/git/futuretest)
error[E0277]: the trait bound `(): futures::Future` is not satisfied
--> src/main.rs:17:1
|
17 | / fn test() -> MapErr<(), ()> {
18 | | future::ok(())
19 | | .map_err(|String| ())
20 | | }
| |_^ the trait `futures::Future` is not implemented for `()`
|
= note: required by `futures::MapErr`
error: aborting due to previous error
error: Could not compile `futuretest`.
To learn more, run the command again with --verbose.
It is extremely unclear from the resulting error message what the problem is. The text specifies that MapErr requires () to implement futures::Future, but highlights the entire function instead of (ideally) just the first type parameter in the function return type.
In this specifically chosen case, the error complains about () and MapErr - but trickily, () is just fine for the second type parameter to MapErr<(), ()>, it's the first () that needs to implement futures::Future.
So to recap:
- Error should point to the return type declaration instead of the entire function
- Error needs to clarify which inner type must satisfy the trait in question
Here's what the latest rustc (1.50 nightly) gives for this error:
error[E0277]: the trait bound `(): futures::Future` is not satisfied --> src/main.rs:17:14 | 17 | fn test() -> MapErr<(), ()> { | ^^^^^^^^^^^^^^ the trait `futures::Future` is not implemented for `()` | ::: /home/achin/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.17/src/future/map_err.rs:8:34 | 8 | pub struct MapErr<A, F> where A: Future { | ------ required by this bound in `futures::MapErr`This error message does both things that the original issue asked for:
- It points to the return type (not to the entire function)
- It points out which of
MapErr's inner types requires the bound
So I think this issue can be closed
Andrew Chin at 2020-11-27 19:46:19
Awesome! Can the underline squiggles be made to point to the parameter in question rather than the entire generic?
Mahmoud Al-Qudsi at 2020-11-28 05:18:17