Skip to content
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

Type mismatch with generic parameter returns less than helpful errors #43943

Open
mqudsi opened this issue Aug 17, 2017 · 1 comment
Open

Type mismatch with generic parameter returns less than helpful errors #43943

mqudsi opened this issue Aug 17, 2017 · 1 comment
Labels
A-diagnostics A-impl-trait A-suggestion-diagnostics A-typesystem C-enhancement T-compiler WG-diagnostics

Comments

@mqudsi
Copy link
Contributor

@mqudsi mqudsi commented Aug 17, 2017

When a type does not match a generic parameter (I'm not sure if it's treated differently if it's because a generic constraint failed or because a previous usage dictated a particular variant of that generic), the resulting error message is not helpful because it seems to indicate that everything is OK.

As an example:

error[E0308]: mismatched types
  --> src/main.rs:80:23
   |
80 |             return Ok(response);
   |                       ^^^^^^^^ expected type parameter, found struct `std::io::Cursor`
   |
   = note: expected type `tiny_http::Response<R>`
              found type `tiny_http::Response<std::io::Cursor<std::vec::Vec<u8>>>`
   = help: here are some functions which might fulfill your needs:
           - .with_data(...)
           - .with_header(...)
           - .with_status_code(...)

the method in question was declared as

fn send_email<R>(req: &mut Request) -> Result<Response<R>, String>
    where R: std::io::Read

I believe the compiler message should at least indicate the constraints on R, because as it currently reads, tiny_http::Response<std::io::Cursor<std::vec::Vec<u8>>> is a valid form of tiny_http::Response<R> for R=std::io::Cursor<std::vec::Vec<u8>>

@Mark-Simulacrum Mark-Simulacrum added A-diagnostics C-enhancement labels Aug 20, 2017
@estebank estebank added the WG-diagnostics label Dec 9, 2017
@estebank
Copy link
Contributor

@estebank estebank commented Jan 22, 2020

Current output:

error[E0308]: mismatched types
 --> file.rs:7:15
  |
4 | fn send_email<R>(req: &mut Request) -> Result<Response<R>, String>
  |               - this type parameter
...
7 |   Ok(Response(42))
  |               ^^ expected type parameter `R`, found integer
  |
  = note: expected type parameter `R`
                       found type `{integer}`
  = help: type parameters must be constrained to match other types
  = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

Only thing missing would be to suggest using impl Trait instead. To do so we should verify that the type parameter is only used in the return type and that all of its bounds can be represented as impl Trait (no where R::Assoc: Blah shenanigans). It should look something along the lines of


error[E0308]: mismatched types
 --> file.rs:7:15
  |
4 | fn send_email<R>(req: &mut Request) -> Result<Response<R>, String>
  |               - this type parameter
...
7 |   Ok(Response(42))
  |               ^^ expected type parameter `R`, found integer
  |
  = note: expected type parameter `R`
                       found type `{integer}`
  = help: type parameters must be constrained to match other types
  = note: for more information on traits as parameters, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
  = note: for more information on `impl Trait`, visit <URL>
help: because type parameter `R` is only used in the return, you can instead use `impl Trait`:
  |
4 | fn send_email(req: &mut Request) -> Result<Response<impl std::fmt::Display>, String>
  |             --                                      ^^^^^^^^^^^^^^^^^^^^^^

When there's an error with the returned value not implementing Trait, the output is:

error[E0277]: the trait bound `{integer}: std::io::Read` is not satisfied
 --> file.rs:4:53
  |
4 | fn send_email(req: &mut Request) -> Result<Response<impl std::io::Read>, String> {
  |                                                     ^^^^^^^^^^^^^^^^^^ the trait `std::io::Read` is not implemented for `{integer}`
5 |   Ok(Response(42))
  |   ---------------- this returned value is of type `std::result::Result<Response<{integer}>, std::string::String>`
  |
  = help: the following implementations were found:
            <&'a std::os::unix::net::UnixStream as std::io::Read>
            <&'a std::sys::unix::fd::FileDesc as std::io::Read>
            <&[u8] as std::io::Read>
            <&mut R as std::io::Read>
          and 19 others
  = note: the return type of a function must have a statically known size

@estebank estebank added A-impl-trait A-suggestion-diagnostics T-compiler A-typesystem labels Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics A-impl-trait A-suggestion-diagnostics A-typesystem C-enhancement T-compiler WG-diagnostics
Projects
None yet
Development

No branches or pull requests

3 participants