Skip to content

Macro using expr non-terminal can cause incorrect borrowck error #8754

Closed
@lilyball

Description

@lilyball

A macro that uses an expr non-terminal that's used in both arms of an if statement, where the expression given to the macro expression involves borrowing *self as mutable, triggers an incorrect borrowck warning where it claims *self is being borrowed as mutable twice.

Code:

struct Foo {
    i: int
}

macro_rules! foo(
    ($code:expr) => (
        if true {
            $code
        } else {
            $code
        }
    )
)

impl Foo {
    pub fn foo(&mut self) {
        foo!({ self.bar() })
    }

    pub fn bar(&mut self) {
        self.i += 1;
    }
}

fn main() {
    let mut foo = Foo{i: 0};

    foo.foo();
}

Errors:

Untitled.rs:17:15: 17:20 error: cannot borrow `*self` as mutable more than once at a time
Untitled.rs:17         foo!({ self.bar() })
                              ^~~~~
Untitled.rs:5:0: 13:1 note: in expansion of foo!
Untitled.rs:17:8: 18:5 note: expansion site
Untitled.rs:17:15: 17:20 note: second borrow of `*self` as mutable occurs here
Untitled.rs:17         foo!({ self.bar() })
                              ^~~~~
Untitled.rs:5:0: 13:1 note: in expansion of foo!
Untitled.rs:17:8: 18:5 note: expansion site
Untitled.rs:17:15: 17:20 error: cannot borrow `*self` as mutable more than once at a time
Untitled.rs:17         foo!({ self.bar() })
                              ^~~~~
Untitled.rs:5:0: 13:1 note: in expansion of foo!
Untitled.rs:17:8: 18:5 note: expansion site
Untitled.rs:17:15: 17:20 note: second borrow of `*self` as mutable occurs here
Untitled.rs:17         foo!({ self.bar() })
                              ^~~~~
Untitled.rs:5:0: 13:1 note: in expansion of foo!
Untitled.rs:17:8: 18:5 note: expansion site
error: aborting due to 2 previous errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-syntaxextArea: Syntax extensions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions