Skip to content

Incorrect suggestion for incompatible match arm types #142359

Open
@jjl9807

Description

@jjl9807

Code

use std::ops::ControlFlow;

fn main() {
    let mut copy: Vec<U> = three_arg_diff(1, X {}, X {});

    match x {
        Err(r) => ControlFlow::Break(r),
        2_000_000..=3_999_999 => { println!("A")}
    }
}

Current output

error[E0308]: `match` arms have incompatible types
 --> .\2\test.rs:8:36
  |
6 | /     match x {
7 | |         Err(r) => ControlFlow::Break(r),
  | |                   --------------------- this is found to be of type `ControlFlow<_, _>`
8 | |         2_000_000..=3_999_999 => { println!("A")}
  | |                                    ^^^^^^^^^^^^^ expected `ControlFlow<_, _>`, found `()`
9 | |     }
  | |_____- `match` arms have incompatible types
  |
  = note:   expected enum `ControlFlow<_, _>`
          found unit type `()`
  = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try wrapping the expression in a variant of `ControlFlow`
 --> C:\Users\jjl98\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\std\src\macros.rs:142
:23
  |
14~     ($($arg:tt)*) => {std::ops::ControlFlow::Continue({
14|         $crate::io::_print($crate::format_args_nl!($($arg)*));
14~     })};
 --> C:\Users\jjl98\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\std\src\macros.rs:142
:23
  |
14~     ($($arg:tt)*) => {std::ops::ControlFlow::Break({
14|         $crate::io::_print($crate::format_args_nl!($($arg)*));
14~     })};
  |

Desired output

error[E0308]: `match` arms have incompatible types
  --> .\2\test.rs:9:36
   |
6  | /     match x {
7  | |     //~^ ERROR cannot move out of `x` as enum variant `Some` which is behind a shared reference
8  | |         Err(r) => ControlFlow::Break(r),
   | |                   --------------------- this is found to be of type `ControlFlow<_, _>`
9  | |         2_000_000..=3_999_999 => { println!("A")}
   | |                                    ^^^^^^^^^^^^^ expected `ControlFlow<_, _>`, found `()`
10 | |     }
   | |_____- `match` arms have incompatible types
   |
   = note:   expected enum `ControlFlow<_, _>`
           found unit type `()`
   = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

Rationale and extra context

The suggestion here is incorrect — users should not be prompted to modify code in the standard library. In fact, it would be better not to show any suggestion at all, as was the case in version 1.86. The "Desired Output" shown above is exactly what rustc 1.86.0 (05f9846 2025-03-31) produces.

Other cases

Rust Version

rustc 1.89.0-nightly (1677d46cb 2025-06-10)
binary: rustc
commit-hash: 1677d46cb128cc8f285dbd32b0dc4d7a46437050
commit-date: 2025-06-10
host: x86_64-pc-windows-msvc
release: 1.89.0-nightly
LLVM version: 20.1.5

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)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