Skip to content

suggestion for handling E0404 doesn't deal with multiple generics properly #112472

Closed
@pflakus

Description

@pflakus

Code

// also available as a playground here:
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4e1b3126262820b760f4ee5037d629e4

use std::fmt::Debug;
use std::marker::PhantomData;

#[allow(unused)]
struct Codec<EncodeLine, DecodeLine> {
    phantom_decode: PhantomData<DecodeLine>,
    phantom_encode: PhantomData<EncodeLine>,
}

pub enum ParseError {}

impl<EncodeLine, DecodeLine> Codec<EncodeLine, DecodeLine> where
    DecodeLine: Debug + TryFrom<String>,
    <DecodeLine as TryFrom<String>>::Error: ParseError,

    // // suggested fix: replace the above two lines with the following one
    // DecodeLine: Debug + TryFrom<String><Error = ParseError>,

    // // actual fix: replace the above two lines with the following one
    // DecodeLine: Debug + TryFrom<String, Error = ParseError>
{
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0404]: expected trait, found enum `ParseError`
  --> src/lib.rs:14:45
   |
14 |     <DecodeLine as TryFrom<String>>::Error: ParseError,
   |                                             ^^^^^^^^^^ not a trait
   |
help: constrain the associated type to `ParseError`
   |
14 |     DecodeLine: TryFrom<String><Error = ParseError>,
   |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: consider importing one of these items instead
   |
1  + use nom::error::ParseError;
   |
1  + use winnow::error::ParseError;
   |

For more information about this error, try `rustc --explain E0404`.

Desired output

Compiling playground v0.0.1 (/playground)
error[E0404]: expected trait, found enum `ParseError`
  --> src/lib.rs:14:45
   |
14 |     <DecodeLine as TryFrom<String>>::Error: ParseError,
   |                                             ^^^^^^^^^^ not a trait
   |
help: constrain the associated type to `ParseError`
   |
14 |     DecodeLine: TryFrom<String, Error = ParseError>,
   |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
help: consider importing one of these items instead
   |
1  + use nom::error::ParseError;
   |
1  + use winnow::error::ParseError;
   |

For more information about this error, try `rustc --explain E0404`.

Rationale and extra context

The generated constraint recommendation appears not to be aware that TryFrom already has defined generics and just plugs its own generic parameter at the end naively. (TryFrom<String><Error = ParseError>)

Instead it ought to recognize and expand the generic parameters (like so: TryFrom<String, Error = ParseError>)

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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