Skip to content

Incorrect suggestion for incompatible match arm types #142359

@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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Jun 11, 2025
xizheyin

xizheyin commented on Jun 12, 2025

@xizheyin
Contributor

There are some similar issues, and it would be better to change the title to “Suggest in non-user-defined external library”. We could start a Track Issue to follow up on these issues.

hkBst

hkBst commented on Jun 12, 2025

@hkBst
Member

The suggestion here is incorrect — users should not be prompted to modify code in the standard library.

Which part prompts the user to modify the standard library?

jjl9807

jjl9807 commented on Jun 12, 2025

@jjl9807
Author

The suggestion here is incorrect — users should not be prompted to modify code in the standard library.

Which part prompts the user to modify the standard library?

The help sub-diagnostic in current output suggests modifying rustlib/src/rust/library/std/src/macros.rs by wrapping the expression in a ControlFlow variant.

hkBst

hkBst commented on Jun 12, 2025

@hkBst
Member

Ah, I see. It's making the suggestion to wrap something in the expansion of println! in ControlFlow, instead of suggesting to wrap println! itself.

hkBst

hkBst commented on Jun 12, 2025

@hkBst
Member

@rustbot labels A-macros

added
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
on Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @hkBst@rustbot@xizheyin@jjl9807

      Issue actions

        Incorrect suggestion for incompatible match arm types · Issue #142359 · rust-lang/rust