Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Borrowck does not correctly do mutability checks on Trait objects #7464

Closed
MarkJr94 opened this issue Jun 28, 2013 · 1 comment
Closed

Borrowck does not correctly do mutability checks on Trait objects #7464

MarkJr94 opened this issue Jun 28, 2013 · 1 comment
Labels
A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system

Comments

@MarkJr94
Copy link
Contributor

For a simple sample check this file: https://gist.github.com/MarkJr94/5888147

This is the Character trait:

pub trait Character {
    fn x(&self)-> int;
    fn y(&self)-> int;
    fn set_x(&mut self, int);
    fn set_y(&mut self, int);
    fn translate(&mut self, dx: int, dy: int);

    fn name(&self) -> ~str;
    fn new(x: int, y:int) -> Self;
    fn rest(&mut self);
    fn as_str(&self) -> ~str;

    fn hp(&self) -> uint;
    fn hp_mut<'r> (&'r mut self) -> &'r mut uint;
    fn stamina(&self) -> uint;
    fn attack(&mut self, player: &mut Character) -> uint;
}

The issue is specific to Trait objects. Changing player: &mut Character to a player: &mut Mummy works.

Moreover, there is a different issue, in that this problem is also solved by changing player: &mut Character to mut player: &mut Character which should not be necessary, as I do not reassign the player reference/pointer.

Minimal Example:

// traitfail.rs

trait Slide {
    fn new(x: int) -> Self;
    fn x_mut<'r> (&'r mut self) -> &'r mut int;
    fn x(&self) -> int;
}

struct Bead {
    x: int
}

impl Slide for Bead {
    pub fn new(x: int) -> Bead {
        Bead { x: x}
    }

    pub fn x(&self) -> int {
        self.x
    }

    pub fn x_mut<'r>(&'r mut self) -> &'r mut int {
        &'r mut self.x
    }
}

fn main() {
    let slide = &mut Slide::new::<Bead>(5) as &mut Slide;

    let x = slide.x();

    let x_mut = slide.x_mut();
    *x_mut += 2;
}
// Compiler output

traitfail.rs:30:13: 30:18 error: cannot borrow immutable local variable as mutable
traitfail.rs:30     let x_mut = slide.x_mut();
                                ^~~~~
error: aborting due to previous error
@alexcrichton
Copy link
Member

This test now works, and I believe that there are a number of test cases already checked in which exercise this functionality.

Closing.

flip1995 pushed a commit to flip1995/rust that referenced this issue Oct 21, 2021
…=flip1995

Add undocumented_unsafe_blocks lint

changelog: Added a new lint [`undocumented_unsafe_blocks`]

Fixes rust-lang#7464, rust-lang#7238 (?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system
Projects
None yet
Development

No branches or pull requests

2 participants