Skip to content

rust compiler bug on borrow/ownership? #67824

Open
@fifc

Description

@fifc
struct Node {
    child: [Option<Box<Node>>; 3]
}

fn main() {
    let mut root: [Option<Box<Node>>; 3] = Default::default();
    let mut tree = &mut root;
    for _ in 0..3 {
        let opt = &mut tree[0]; // line 9
        match opt {
            None => {
                //break;  // ok if uncomment this line of code
                // if true { break; } // even error with code like this 
            }
            Some(b) => {
                tree = &mut b.as_mut().child;
            }
        }
    }
}
error[E0499]: cannot borrow `tree[_]` as mutable more than once at a time
 --> t.rs:9:19
  |
9 |         let opt = &mut tree[0];
  |                   ^^^^^^^^^^^^ mutable borrow starts here in previous iteration of loop

however, compile ok if add a break statement in the None block

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)C-bugCategory: This is a bug.NLL-completeWorking towards the "valid code works" goalNLL-poloniusIssues related for using Polonius in the borrow checkerT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions