Rust-GCC / Rust-GCC/gccrs

Track mutability

Open
#807 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
2.9k
Forks
230
Avg merge
19h 55m
Merged PRs (30d)
67

Description

I tried this code:

use std::ops::{Deref, DerefMut};

struct DerefMutExample<T> {
    value: T
}

impl<T> Deref for DerefMutExample<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        println!("123");
        &self.value
    }
}

impl<T> DerefMut for DerefMutExample<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        println!("456");
        &mut self.value
    }
}

pub fn main() {
    let mut x = DerefMutExample { value: 'a' };
    *x = 'b';
    assert_eq!('b', *x);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5d497ace74bdccc069e01f8b9e8b3449

This shows that the deref assignment must take a mutable reference and currently even this simple example:

pub fn main() {
    let a = 123;
    let b = &mut a;
}

see https://godbolt.org/z/4GYjoTxcT

I expected to see this happen: error with:

error[E0596]: cannot borrow `a` as mutable, as it is not declared as mutable
 --> <source>:3:13
  |
2 |     let a = 123;
  |         - help: consider changing this to be mutable: `mut a`
3 |     let b = &mut a;
  |             ^^^^^^ cannot borrow as mutable

Instead, this happened: it compiled without error

Meta
  • What version of Rust GCC were you using, git sha if possible. This was in my operator overloading branch but we don't have any code to track mutability

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 by reproducing the two Rust examples from the issue: the DerefMut assignment and the mutable borrow of an immutable binding. Trace the compiler's mutability checking for let a = 123 and &mut a; done when the second example reports the expected E0596 diagnostic without regressing the DerefMut example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.