Skip to content

in loops, the error "borrow of possibly uninitialized variable" is overwritten by "borrow of moved value" in certain case(s) #57553

Description

In loops that are detected to happen more than once, the true error error[E0381]: borrow of possibly uninitialized variable: is overwritten by another error that doesn't seem to actually apply at all: error[E0382]: borrow of moved value:.

This happens possibly due to #52669 having been fixed.

Simple example (playground) follows, which is showing the fake error error[E0382]: borrow of moved value:; to see true error just uncomment the last break; line:

#![allow(unused_variables)]
#![allow(dead_code)]

struct NewType(i32);

fn main() {
    
    let mut v:Vec<NewType>=Vec::new();
    
    
    loop {
    //for i in 1..1 {
        //println!("iter{}",i);
        
        let a:NewType;
        //let mut a:NewType;
        //let a:NewType=NewType(3);
        //let mut a:NewType=NewType(3);//no errors with this line
        change(&mut a);//true but only happens without the "v.push(a)" below : "borrow of possibly uninitialized variable: `a`"
        v.push(a); //if this is uncommented then true error "borrow of possibly uninitialized variable: `a`" is overwritten by "borrow of moved value: `a`" and "value moved here, in previous iteration of loop"
       
        if 1==1 { //this breaks loop detection as happening only once(which a non-if break would do)
          break;
        }
        //break; //XXX uncomment this to see true error!
    }
}

fn change(i: &mut NewType) {
    *i=NewType(1);
}

Tested with rust nightly 2018, 11 Jan 2019 version, on playground.

Pedantic example on playground

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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