Skip to content

High memory usage with cargo build in match statement #131410

Open
@Joshix-1

Description

@Joshix-1

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.I-compilememIssue: Problems and improvements with respect to memory usage during compilation.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.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