Skip to content

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

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

Closed
pflakus opened this issue Jun 9, 2023 · 0 comments · Fixed by #112486
Closed

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

pflakus opened this issue Jun 9, 2023 · 0 comments · Fixed by #112486
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pflakus
Copy link

pflakus commented Jun 9, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
@pflakus and others