rust-lang / rust-lang/rust

Improper str AddAssign produces confusing error on otherwise-correct match statement.

Open
#161,717 2 comments 0 reactions 1 assignee View on GitHub

@Aditya-PS-05 is already working on this.

Since Aug 25, 2026.

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

Description

Code
// Code written by a beginner
pub fn foo(byte: u8) {
    let mut s = "";
    s += match byte {
        0 => '1',
        1 => '2',
        _ => panic!(),
    };
}
Current output
error[E0308]: `match` arms have incompatible types
 --> src/lib.rs:6:14
  |
3 |       s += match byte {
  |  __________-
4 | |         0 => '1',
  | |              --- this is found to be of type `char`
5 | |         1 => '2',
  | |              --- this is found to be of type `char`
6 | |         _ => panic!(),
  | |              ^^^^^^^^ expected `char`, found `!`
7 | |     };
  | |_____- `match` arms have incompatible types
  |
  = note: expected type `char`
             found type `!`
Desired output
error[E0368]: binary assignment operation `+=` cannot be applied to type `&str`
...
...
  |     -^^^^^
  |     |
  |     cannot use `+=` on type `&str`
Rationale and extra context

This error was very confusing to the programmer, because they reasoned (correctly) that the never type can be coerced into a char.

Neither of us understand why this error was emitted. It was the only error emitted: the compiler was silent about the actual problem (can't AddAssign to str).

Other cases
// Similar code that produces the expected error
pub fn foo(byte: u8) {
    let mut s = "";
    let c = match byte {
        0 => '1',
        1 => '2',
        _ => panic!(),
    };
    s += c;
}
Rust Version
rustc 1.98.0 (88d9e12ae 2026-08-18)
binary: rustc
commit-hash: 88d9e12ae178fab0fb5cc050a94da85685d449ea
commit-date: 2026-08-18
host: x86_64-unknown-linux-gnu
release: 1.98.0
LLVM version: 22.1.8
Anything else?

Also happens on rustc 1.100.0-nightly (e7769602a 2026-08-24).

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.