Skip to content

Mismatched types should suggest From/Into conversions #56181

@gnzlbg

Description

@gnzlbg

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-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