Closed
Description
The following code is missing a constraint for TryInto<Error=TryIntoError>
:
use std::convert::TryInto;
struct TryIntoError;
struct X;
impl TryInto<usize> for X {
type Error = TryIntoError;
fn try_into(self) -> Result<usize, TryIntoError> {
Ok(0)
}
}
trait T: TryInto<usize> {
fn foo(self) -> Result<usize, TryIntoError> {
TryInto::try_into(self)
}
}
which causes the following error:
error[E0308]: mismatched types
--> src/main.rs:16:9
|
15 | fn foo(self) -> Result<usize, TryIntoError> {
| --------------------------- expected `std::result::Result<usize, TryIntoError>` because of return type
16 | TryInto::try_into(self)
| ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `TryIntoError`, found associated type
|
= note: expected type `std::result::Result<_, TryIntoError>`
found type `std::result::Result<_, <Self as std::convert::TryInto<usize>>::Error>`
This should be closer to
error[E0308]: mismatched types
--> src/main.rs:16:9
|
14 | trait T: TryInto<usize> {
| -------------- help: explicitely set associated type `Error`: `TryInto<usize, TryIntoError>`
15 | fn foo(self) -> Result<usize, TryIntoError> {
| --------------------------- expected `std::result::Result<usize, TryIntoError>` because of return type
16 | TryInto::try_into(self)
| ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `TryIntoError`, found associated type
|
= note: expected type `std::result::Result<_, TryIntoError>`
found type `std::result::Result<_, <Self as std::convert::TryInto<usize>>::Error>`
or
error[E0308]: mismatched types
--> src/main.rs:16:9
|
15 | fn foo(self) -> Result<usize, TryIntoError> {
| --------------------------- expected `std::result::Result<usize, TryIntoError>` because of return type
16 | TryInto::try_into(self)
| ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `TryIntoError`, found associated type
|
= note: expected type `std::result::Result<_, TryIntoError>`
found type `std::result::Result<_, <Self as std::convert::TryInto<usize>>::Error>`
help: explicitely set associated type `Error`
|
14 | trait T: TryInto<usize, TryIntoError> {
| ^^^^^^^^^^^^^^
help: alternatively, use `TryInto::Error`
|
15 | fn foo(self) -> Result<usize, Self::Error> {
| ^^^^^^^^^^^
CC reporter @minggfeng
Metadata
Metadata
Assignees
Labels
Area: Associated items (types, constants & functions)Area: Messages for errors, warnings, and lintsArea: Suggestions generated by the compiler applied by `cargo fix`Category: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint; hard to understand for new users.Medium priorityRelevant to the compiler team, which will review and decide on the PR/issue.