Skip to content

Error compiling problem case #4 from nll-rfc #46361

Closed
@fpoli

Description

@fpoli

UPDATE: This example works now, but needs someone to add a test. See mentoring instructions below.


I'm trying to compile some examples using nonlexical lifetimes. To do so, I'm compiling rustc using the master branch, and the -Z nll compiler option.

rustc 1.24.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.24.0-dev
LLVM version: 4.0

By compiling "Problem case 4: mutating &mut references" from nll-rfc, I get some errors.

Program
struct List<T> {
    value: T,
    next: Option<Box<List<T>>>,
}

fn to_refs<T>(mut list: &mut List<T>) -> Vec<&mut T> {
    let mut result = vec![];
    loop {
        result.push(&mut list.value);
        if let Some(n) = list.next.as_mut() {
            list = &mut n;
        } else {
            return result;
        }
    }
}

fn main() {
    println!("Hello World!");
}
Output of `rustc -g -Zverbose -Znll nll-problem-case-4.rs`:
error[E0597]: `n` does not live long enough
  --> nll-problem-case-4.rs:11:25
   |
11 |             list = &mut n;
   |                         ^ does not live long enough
...
14 |         }
   |         - borrowed value only lives until here
   |
note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 6:1...
  --> nll-problem-case-4.rs:6:1
   |
6  | / fn to_refs<T>(mut list: &mut List<T>) -> Vec<&mut T> {
7  | |     let mut result = vec![];
8  | |     loop {
9  | |         result.push(&mut list.value);
...  |
15 | |     }
16 | | }
   | |_^

error[E0499]: cannot borrow `list.value` as mutable more than once at a time
  --> nll-problem-case-4.rs:9:26
   |
9  |         result.push(&mut list.value);
   |                          ^^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
16 | }
   | - mutable borrow ends here

error[E0499]: cannot borrow `list.next` as mutable more than once at a time
  --> nll-problem-case-4.rs:10:26
   |
10 |         if let Some(n) = list.next.as_mut() {
   |                          ^^^^^^^^^ mutable borrow starts here in previous iteration of loop
...
16 | }
   | - mutable borrow ends here

error[E0506]: cannot assign to `list` because it is borrowed
  --> nll-problem-case-4.rs:11:13
   |
9  |         result.push(&mut list.value);
   |                          ---------- borrow of `list` occurs here
10 |         if let Some(n) = list.next.as_mut() {
11 |             list = &mut n;
   |             ^^^^^^^^^^^^^ assignment to borrowed `list` occurs here

error: aborting due to 4 previous errors

My questions are:

  • Am I enabling correctly the experimental nll? What confuses me is that I compiled rustc with ./configure --enable-debug --enable-debuginfo, but I still don't see the debug!("mir_borrowck done"); message from the compiler.
  • Is "problem case 4" supposed to compile without errors in the future, when nll will be stable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)C-bugCategory: This is a bug.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions