rust-lang / rust-lang/rust

Misleading message while type mismatch with type alias

Open
#154,404 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-cfg A-diagnostics D-invalid-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic using the Rust code shown in src/main.rs and compare the current and desired E0308 output. Trace how the compiler renders the aliased c_char type in the mismatch and conversion help, then add coverage for the reported x86 case and verify the diagnostic preserves the alias rather than only i8.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.