Generic's type mismatched

How can I solve this?

error:

error[E0308]: mismatched types
  --> src/error/parse_code/string_syntaxerror.rs:44:9
   |
17 |   impl CompilerError for StringSyntaxError {
   |                          ----------------- this is the type of the `Self` literal
...
43 |       fn err_clone<E: CompilerError>(&self) -> E {
   |                    -                           - expected `E` because of return type
   |                    |
   |                    expected this type parameter
44 | /         Self {
45 | |             file: self.file.clone(),
46 | |             line: self.line.clone(),
47 | |             col : self.col.clone()
48 | |         }
   | |_________^ expected type parameter `E`, found `StringSyntaxError`
   |
   = note: expected type parameter `E`
                      found struct `error::parse_code::string_syntaxerror::StringSyntaxError`
   = note: the caller chooses a type for `E` which can be different from `error::parse_code::string_syntaxerror::StringSyntaxError`

This post would be more appropriate for https://users.rust-lang.org/, the users forum. internals.rust-lang.org is for discussing improvements to the language.

But the problem you are having is that fn error_clone promises to be able to return a value of any type E, not the specific type StringSyntaxError, which promise it can't keep. To fix this, the function needs to not have a separate type parameter E:

fn err_clone(&self) -> Self {

You will need to make this change to the definition of trait CompilerError as well as the impl.

3 Likes