Skip to content

NLL breaks usage of AddAssign #48129

Closed
Closed
@Aaron1011

Description

@Aaron1011

When trying to reproduce #48071, I came across another issue in libcompiler_builtins. The original errors were:

error[E0503]: cannot use `result` because it was mutably borrowed
   --> rustc/compiler_builtins_shim/../../libcompiler_builtins/src/float/add.rs:179:46
    |
179 |     if round_guard_sticky == 0x4 { result += result & one; }
    |                                    ------    ^^^^^^ use of borrowed `result`
    |                                    |
    |                                    borrow of `result` occurs here

error[E0503]: cannot use `a` because it was mutably borrowed
  --> rustc/compiler_builtins_shim/../../libcompiler_builtins/src/float/pow.rs:18:18
   |
18 |             a *= a;
   |             -    ^ use of borrowed `a`
   |             |
   |             borrow of `a` occurs here

error[E0503]: cannot use `product_high` because it was mutably borrowed
   --> rustc/compiler_builtins_shim/../../libcompiler_builtins/src/float/mul.rs:173:25
    |
173 |         product_high += product_high & one;
    |         ------------    ^^^^^^^^^^^^ use of borrowed `product_high`

I managed to reduce the second error to this example (playground):

#![feature(nll)]

use std::ops::*;

#[derive(Copy, Clone)]
struct Foo;

impl AddAssign for Foo {
    fn add_assign(&mut self, _rhs: Foo) {
        panic!()
    }
}

impl Foo {
    fn use_self(&mut self, _other: Foo) {
        panic!()
    }
}

fn bar() {
    let mut a = Foo;
    let mut b = Foo;
    
    b.use_self(b);
    a += a;
}

fn main() {}

which fails to compile with this error:

error[E0503]: cannot use `a` because it was mutably borrowed
  --> src/main.rs:25:10
   |
25 |     a += a;
   |     -    ^ use of borrowed `a`
   |     |
   |     borrow of `a` occurs here

As expected, removing #![feature(nll)] causes a += a to compile, but b.use_self(b); to fail with this error:

error[E0503]: cannot use `b` because it was mutably borrowed
  --> src/main.rs:24:16
   |
24 |     b.use_self(b);
   |     -          ^ use of borrowed `b`
   |     |
   |     borrow of `b` occurs here

I assume that this last error is due to the lack of two-phase borrows on ast-borrowck, and is not something that will be fixed. However, I expect NLL to compile both the operator and 'desuraged operator' version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions