Skip to content

Suggest setting associated types on type errors #63711

Closed
@estebank

Description

@estebank

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

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions