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

Mismatched types should suggest From/Into conversions #56181

Closed
gnzlbg opened this issue Nov 23, 2018 · 2 comments
Closed

Mismatched types should suggest From/Into conversions #56181

gnzlbg opened this issue Nov 23, 2018 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Nov 23, 2018

This program (Playground):

use std::{process::Stdio, fs::File};
pub fn foo(_x: Stdio) {}
pub fn bar(x: File) { foo(x) }

errors with

error[E0308]: mismatched types
 --> src/lib.rs:3:27
  |
3 | pub fn bar(x: File) { foo(x) }
  |                           ^ expected struct `std::process::Stdio`, found struct `std::fs::File`
  |
  = note: expected type `std::process::Stdio`
             found type `std::fs::File`

error: aborting due to previous error

but there is an impl of From<File> for Stdio, such that this code (Playground):

use std::{process::Stdio, fs::File};
pub fn foo(_x: Stdio) {}
pub fn bar(x: File) { foo(x.into()) }

compiles and works as expected.

The error message should have a note stating that there is a conversion available, and a fix suggestion. That would be a huge boon for API discoverability. If there are multiple APIs that take File and return Stdio, the error message should suggest some of them with some sort of prioritization (From/TryFrom traits, ::from/::new methods, .as_... methods, etc.)

@gnzlbg gnzlbg changed the title Mismatched types should suggest conversion Mismatched types should suggest From/Into conversions Nov 23, 2018
@oli-obk oli-obk added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 23, 2018
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 26, 2019
@estebank
Copy link
Contributor

CC #37384, #42499

@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@kadiwa4
Copy link
Contributor

kadiwa4 commented Jan 28, 2023

Fixed (nightly 2023-01-27):

error[E0308]: mismatched types
 --> src/lib.rs:3:27
  |
3 | pub fn bar(x: File) { foo(x) }
  |                       --- ^ expected struct `Stdio`, found struct `File`
  |                       |
  |                       arguments to this function are incorrect
  |
note: function defined here
 --> src/lib.rs:2:8
  |
2 | pub fn foo(_x: Stdio) {}
  |        ^^^ ---------
help: call `Into::into` on this expression to convert `File` into `Stdio`
  |
3 | pub fn bar(x: File) { foo(x.into()) }
  |                            +++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants