Skip to content

[NLL] A rejects-valid (a NLL regression) #48180

@leonardo-m

Description

@leonardo-m

This code compiles and runs correctly if I don't use the NLL feature:

#![feature(nll)]

use std::ops::{ShrAssign, BitOr, Shl, SubAssign};
use std::mem::swap;

trait HasTrailingZeros {
    #[inline(always)]
    fn trailing_zeros(self) -> u32;
}
impl HasTrailingZeros for u32 {
    #[inline(always)]
    fn trailing_zeros(self) -> u32 { self.trailing_zeros() }
}

fn gcd<T>(mut u: T, mut v: T) -> T
where T: PartialEq +
         Copy +
         From<u8> +
         PartialOrd +
         BitOr<Output=T> +
         Shl<u32, Output=T> +
         ShrAssign<u32> +
         SubAssign +
         HasTrailingZeros {
    if u == 0.into() { return v; }
    if v == 0.into() { return u; }
    let shift = (u | v).trailing_zeros();
    u >>= u.trailing_zeros();
    loop {
        v >>= v.trailing_zeros();
        if u > v {
            swap(&mut u, &mut v);
        }
        v -= u;
        if v == 0.into() { break; }
    }
    u << shift
}

fn main() {
    println!("{}", gcd(20u32, 10u32));
}

With the NLL feature it gives (it used to compile with NLL):

error[E0503]: cannot use `u` because it was mutably borrowed
  --> ...\test.rs:28:11
   |
28 |     u >>= u.trailing_zeros();
   |     -     ^ use of borrowed `u`
   |     |
   |     borrow of `u` occurs here

error[E0503]: cannot use `v` because it was mutably borrowed
  --> ...\test.rs:30:15
   |
30 |         v >>= v.trailing_zeros();
   |         -     ^ use of borrowed `v`
   |         |
   |         borrow of `v` occurs here

error: aborting due to 2 previous errors
...>rustc -vV
rustc 1.25.0-nightly (16362c737 2018-02-12)
binary: rustc
commit-hash: 16362c737fe740f630ada06349fa9004e2a51bb7
commit-date: 2018-02-12
host: x86_64-pc-windows-gnu
release: 1.25.0-nightly
LLVM version: 6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions