Skip to content
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

TryFrom trait bound on trait definition complains about missing From impl #80484

Closed
adamlesinski opened this issue Dec 29, 2020 · 4 comments
Closed
Labels
C-bug Category: This is a bug.

Comments

@adamlesinski
Copy link

I tried this code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a9fcf72c4bbeb63baf3dffb724d37852

I expected it to compile successfully.

Instead, it fails to compile with the error:

error[E0277]: the trait bound `&'a <C as Container>::Item: From<&'a Wrapper>` is not satisfied

I do not expect that a From implementation is needed when implementing TryFrom.

If I remove the trait bound from the trait Container, and place it on the function foo instead, it works as expected: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a4bdca2f01cd2a33f85d18e5ebdb5181

Also, if the trait bound is changed to not include lifetimes or references, this works as well: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a2e8dbad7838e71b6c0b8a42fca57509

The issue happens on both Beta and Nightly.

@adamlesinski adamlesinski added the C-bug Category: This is a bug. label Dec 29, 2020
@SkiFire13
Copy link
Contributor

This is a duplicate of #20671

It complains about From because when looking for candidate impls for for<'a> &'a <C as Container>::Item: TryFrom<&'a Wrapper> it found impl<T, U> TryFrom<U> for T where U: Into<T>, then impl<T, U> Into<U> for T where U: From<T>, which leads to for<'a> &'a <C as Container>::Item: From<&'a Wrapper>.

@adamlesinski
Copy link
Author

I see, but impl<T, U> TryFrom<U> for T where U: Into<T> shouldn't be a candidate, given that none of the bounds are satisfied. It should see my local implementation of TryFrom right?

@SkiFire13
Copy link
Contributor

It sees your local TryFrom implementation and correctly accepts it, however the problem isn't on the call foo::<WrappedContainer>() (you can even remove that call and the error will still be there, playground example), the problem is on the definition of foo itself (fn foo<C: Container>() {}), in particular that C: Container. There the trait solver assumes that C implements the trait Container, however it doesn't assume it satisfies all the where clauses, in particular those that aren't directly on Self or on associated items (due to the bug I mentioned in the previous message), but it still tries to check that they're satisfied, and this obviously fails because they aren't valid for all C. This is why the error message mentions for<'a> &'a <C as Container>::Item: TryFrom<&'a Wrapper> with the generic C and not with your type substituted.

ps: I think the way rustc works is that impl<T, U> TryFrom<U> for T where U: Into<T> becomes a candidate because it may apply without considering the bounds. Then the bounds are considered and they may lead to searching for other impls or to a dead end that removes it from the candidates. However i don't know why it's still there in the error message, I would expect it to show the original predicate it was trying to satisfy (for<'a> &'a <C as Container>::Item: TryFrom<&'a Wrapper>).

@Dylan-DPC
Copy link
Member

Current complete error:

error[[E0277]](https://doc.rust-lang.org/stable/error_codes/E0277.html): the trait bound `for<'a> &'a <C as Container>::Item: From<&'a Wrapper>` is not satisfied
  --> src/main.rs:30:11
   |
30 | fn foo<C: Container>() {}
   |           ^^^^^^^^^ the trait `for<'a> From<&'a Wrapper>` is not implemented for `&'a <C as Container>::Item`
   |
   = note: required for `&'a Wrapper` to implement `for<'a> Into<&'a <C as Container>::Item>`
   = note: required for `&'a <C as Container>::Item` to implement `for<'a> TryFrom<&'a Wrapper>`
note: required by a bound in `Container`

@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@jieyouxu jieyouxu closed this as not planned Won't fix, can't repro, duplicate, stale Feb 18, 2024
@jieyouxu jieyouxu removed the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

5 participants