rust-lang / rust-lang/rust

High memory usage with cargo build in match statement

Open
#131,410 7 comments 0 reactions 1 assignee View on GitHub

@dianne is already working on this.

Since Jul 12, 2025.

A-patterns C-bug I-compilemem I-crash T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The memory usage gets high really quickly which causes my pc to crash.

Code

Full git repo: https://github.com/Joshix-1/cargo-memory-leak
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=03d0fd2c2ceeb588e5297389a01f9fa9

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum FieldType {
    Air,
    Wood,
    SandSource,
    BlackHole,
    SandC0, SandC1, SandC2, SandC3, SandC4, SandC5, SandC6, SandC7,
    WaterC0, WaterC1, WaterC2, WaterC3, WaterC4, WaterC5, WaterC6, WaterC7,
}

macro_rules! sand { () => {
    FieldType::SandC0 | FieldType::SandC1 | FieldType::SandC2
    | FieldType::SandC3 | FieldType::SandC4 | FieldType::SandC5
    | FieldType::SandC6 | FieldType::SandC7
}; }

macro_rules! water { () => {
    FieldType::WaterC0 | FieldType::WaterC1 | FieldType::WaterC2
    | FieldType::WaterC3 | FieldType::WaterC4 | FieldType::WaterC5
    | FieldType::WaterC6 | FieldType::WaterC7
}; }

macro_rules! falls {() => { sand!() | water!() };}

macro_rules! not_solid {
    () => { FieldType::Air | water!() };
}

impl FieldType {
    pub const fn is_sand(self) -> bool { matches!(self, sand!()) }

    pub const fn is_water(self) -> bool { matches!(self, water!()) }
}

fn main() {
    let arr = &mut [FieldType::Air; 4];
    let [ref mut a, ref mut b, ref mut c, ref mut d] = arr; 
    let cell: (
        (&mut FieldType, &mut FieldType),
        (&mut FieldType, &mut FieldType),
    ) = ((a, b), (c, d));
    match cell {
        (
            (sand0 @ falls!(), sand1 @ falls!()),
            (unsolid0 @ not_solid!(), unsolid1 @ not_solid!()),
            // compiles without the if in the next line
        ) if unsolid0.is_water() != sand0.is_water() || unsolid1.is_water() != sand1.is_water() => {
            if unsolid0.is_water() != sand0.is_water() {
                (*sand0, *unsolid0) = (*unsolid0, *sand0);
            }
            if unsolid1.is_water() != sand1.is_water() {
                (*sand1, *unsolid1) = (*unsolid1, *sand1);
            }
        }
        _ => {}
    }
}
Meta

rustc --version --verbose:

 rustc --version --verbose
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

also on playground, nightly and stable

Error output
$ env RUST_BACKTRACE=1 cargo build 2> build.err > build.out
# crash of xserver
$ du -bs build.*
49	build.err
0	build.out
$ cat build.err
   Compiling sandrs v0.1.1 (/tmp/tmp.aULMeIa66O)

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.