rust-lang / rust-lang/rust

Making a mistake in a match can lead to infinite recursion without any hint to what is going on

Open
#130,376 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
pub struct SubMenu;

impl SubMenu {
    pub fn test(&self) {
    }
}

pub enum Item {
    Back,
    SubMenu(SubMenu)
}

impl Item {
    pub fn test(&self) {
        match self {
            Item::Back => (),
            // This recurses because I messed up the match
            // I really want Item::SubMenu(x) but there is 
            // no warning at all that this can cause an issue
            sub => sub.test()
        }
    }
}

fn main() {
    let itm = Item::SubMenu(SubMenu{});
    itm.test();
}
Current output
(Running in rust playground)
Compiling playground v0.0.1 (/playground)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.46s

(Clippy output in rust playground)
Checking playground v0.0.1 (/playground)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.88s
Desired output
Anything that tells the user that there is an infinite loop waiting to happen

Something like:
sub => sub.test()
       ^^^^^^^^^^ this will recurse infinitely
Rationale and extra context

I made a mistake while writing the match seen above. I really wanted Item::SubMenu(sub) => sub.test() but mistakenly thought sub => sub.test() will be enough. While the error was of course made by me, after I found it I was surprised that neither rustc, clippy nor rust-analyzer will warn me that this will cause infinite recursion.
I ran the code on an embedded target which made the error search very hard, since any debug connection died when the stack overflowed without any error message, leading to a two day search for the cause

Other cases

No response

Rust Version
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-pc-windows-msvc
release: 1.81.0
LLVM version: 18.1.7

(The exact same issue/missing diagnostic is also true on the beta and nightly channels)
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

Start with the supplied Rust match example and compare the behavior of rustc, Clippy, and rust-analyzer described in the issue. Determine a diagnostic approach for recognizing the unintended recursive match arm; done means the example produces a clear warning without incorrectly flagging valid matches.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.