[feat] Functions should have a metadata describing what fields they modify or read

Hello developers,

new borrow checker feature

Every function should have a metadata describing what fields or sub fields down the tree it modifies or reads from it's &mut parameters. The borrow checker could use this information to be more permissive in those cases, where inlining the called function would make it compile.

metadata for fn World::do_scoring = 
     ReadWrite [ self.score ],
     ReadOnly [ self.people ]
     

why it matters?

Instead of passing on multiple data separately as ref(mut) parameters, you could just pass a &mut to the root data, usually &mut self.

Also the mutability of parameters and variables could be infered by the compiler, making it easier to code.

Example:

struct World {
    bullets: Vec<i32>,
    people: Vec<i32>,
    score: i32,
}

impl World {
    pub fn do_scoring(&mut self) {
        self.score += self.people[2];
    }
}

fn main() {
    let mut world = World {
        bullets: vec![1; 8],
        people: vec![1; 8],
        score: 0,
    };

    let refbullet = &mut world.bullets[2];

    world.do_scoring(); // borrow error
    //world.score += world.people[2]; //  would compile when inlining the same logic.

    *refbullet = 3;

    println!("score: {}", world.score);
}

The name you're looking for is "view types": View types for Rust · baby steps

9 Likes