Skip to content

Misleading message while type mismatch with type alias #154404

@ywxt

Description

@ywxt

Code

extern "C" fn char_type(ch: core::ffi::c_char) -> core::ffi::c_char {
    ch
}

fn main() {
    char_type(b"char"[0]);
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:6:15
  |
6 |     char_type(b"char"[0]);
  |     --------- ^^^^^^^^^^ expected `i8`, found `u8`
  |     |
  |     arguments to this function are incorrect
  |
note: function defined here
 --> src/main.rs:1:15
  |
1 | extern "C" fn char_type(ch: core::ffi::c_char) -> core::ffi::c_char {
  |               ^^^^^^^^^ ---------------------
help: you can convert a `u8` to an `i8` and panic if the converted value doesn't fit
  |
6 |     char_type(b"char"[0].try_into().unwrap());
  |                         ++++++++++++++++++++

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

Desired output

error[E0308]: mismatched types
 --> src/main.rs:6:15
  |
6 |     char_type(b"char"[0]);
  |     --------- ^^^^^^^^^^ expected `core::ffi::c_char`, found `u8`
  |     |
  |     arguments to this function are incorrect
  |
note: function defined here
 --> src/main.rs:1:15
  |
1 | extern "C" fn char_type(ch: core::ffi::c_char) -> core::ffi::c_char {
  |               ^^^^^^^^^ ---------------------
help: you can convert a `u8` to an `core::ffi::c_char` and panic if the converted value doesn't fit
  |
6 |     char_type(b"char"[0].try_into().unwrap());
  |                         ++++++++++++++++++++

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

Rationale and extra context

c_char has different types across platforms. This is the output on x86.
If users do not examine the function signature and instead modify the code to b"char"[0] as i8, although it will compile and pass, portability is lost—because it will fail again on ARM.

The compiler should hint to the user that it is a type alias, rather than printing only the original type name.

Other cases

Rust Version

rustc 1.96.0-nightly (80d0e4be6 2026-03-25)
binary: rustc
commit-hash: 80d0e4be6f15899649ba31669077c59a986f96cc
commit-date: 2026-03-25
host: x86_64-unknown-linux-gnu
release: 1.96.0-nightly
LLVM version: 22.1.2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-cfgArea: `cfg` conditional compilationA-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions