Rust-GCC / Rust-GCC/gccrs

Mutating an immutable variable

Open
#4,533 4 comments 0 reactions 1 assignee View on GitHub

@Islam-Imad is already working on this.

Since Jul 8, 2026.

missing-lint
Dominant language
C++
Stars
2.9k
Forks
231
Avg merge
19h 55m
Merged PRs (30d)
67

Description

// { dg-output "prime\r*\nnot_prime\r*\nprime\r*\nprime\r*\nnot_prime\r*\n" }
#![feature(no_core)]
#![no_core]
extern "C" {
    fn puts(s: *const i8);
}
extern "C" {
    fn printf(s: *const i8, ...);
}
fn dump(message: &str) {
    unsafe {
        let b = message as *const str;
        let c = b as *const i8;
        puts(c);
    }
}
fn is_prime(number: i32) -> bool {
    if number <= 1 {
        return false;
    }
    let i = 1;
    loop {
        i += 1;
        if i * i >= number {
            break;
        }
        if number % i != 0 {
            continue;
        }
        return false;
    }
    return true;
}

fn debug_prime(number: i32) {
    let state = is_prime(number);
    if state {
        dump("prime");
    } else {
        dump("not_prime");
    }
}

fn main() -> i32 {
    debug_prime(11);
    debug_prime(12);
    debug_prime(13);
    debug_prime(17);
    debug_prime(100);
    0
}

inside is_prime function variable i is immutable and still we can compile with gccrs.

rustc output :

~/projects/LLVM/hack> rustc rust.rs ; ./rust
error[E0384]: cannot assign twice to immutable variable `i`
  --> rust.rs:10:9
   |
 8 |     let i = 1;
   |         - first assignment to `i`
 9 |     loop {
10 |         i += 1;
   |         ^^^^^^ cannot assign twice to immutable variable
   |
help: consider making this binding mutable
   |
 8 |     let mut i = 1;
   |         +++

error: aborting due to 1 previous error

~/projects/LLVM/hack>       

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.