-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve compiler error on associated types #71035
Comments
Please provide a self-contained example that shows the bad diagnostic |
I tried to break it down to this code:
|
I just found out that defining
Why is that? Is the compiler not able to deduce
does exactly this but it's not directly clear how to do this. Maybe giving an example like
would be helpful here. |
I think that #71108 might fulfill the last comment's request. Edit: I just checked and the code doesn't account for Edit 2: This will not be handled in the linked PR, it is a second order projection that will need further work for me to be able to supply a structured suggestion. Edit 3: Managed to get it to work 🎉
It's a bit more verbose than I'd like, but we can tackle on a different PR. |
…onstraint, r=oli-obk On type mismatch involving associated type, suggest constraint When an associated type is found when a specific type was expected, if possible provide a structured suggestion constraining the associated type in a bound. ``` error[E0271]: type mismatch resolving `<T as Foo>::Y == i32` --> $DIR/associated-types-multiple-types-one-trait.rs:13:5 | LL | want_y(t); | ^^^^^^ expected `i32`, found associated type ... LL | fn want_y<T:Foo<Y=i32>>(t: &T) { } | ----- required by this bound in `want_y` | = note: expected type `i32` found associated type `<T as Foo>::Y` help: consider constraining the associated type `<T as Foo>::Y` to `i32` | LL | fn have_x_want_y<T:Foo<X=u32, Y = i32>>(t: &T) | ^^^^^^^^^ ``` ``` error[E0308]: mismatched types --> $DIR/trait-with-missing-associated-type-restriction.rs:12:9 | LL | qux(x.func()) | ^^^^^^^^ expected `usize`, found associated type | = note: expected type `usize` found associated type `<impl Trait as Trait>::A` help: consider constraining the associated type `<impl Trait as Trait>::A` to `usize` | LL | fn foo(x: impl Trait<A = usize>) { | ^^^^^^^^^^ ``` Fix rust-lang#71035. Related to rust-lang#70908.
If you have a variable with type
impl TryStream<Ok=T, Error=E>
and want to use it as aimpl Stream<Item=Result<T,E>>
, the compiler complains.The compiler does not take you by the hand and helps you with helpful hints.
I tried (roughly) this code: (EDIT: find a self-contained example here)
I expected to see this happen:
I would like the compiler to tell me that that that while there exists an implementation of
TryStream<Ok=T, Error=E>
asStream<Result<T,E>>
, there might exist other implementations and therefore this code is not accepted. UsingStream<Result<T,E>>
instead ofTryStream<Ok=T, Error=E>
would solve the error.Instead, this happened:
There are no hints by the compiler.
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: