8

The current edition of The Rustonomicon has this example code:

use std::mem;

pub struct IterMut<'a, T: 'a>(&'a mut [T]);

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;

    fn next(&mut self) -> Option<Self::Item> {
        let slice = mem::replace(&mut self.0, &mut []);
        if slice.is_empty() {
            return None;
        }

        let (l, r) = slice.split_at_mut(1);
        self.0 = r;
        l.get_mut(0)
    }
}

I'm confused about this line in particular:

let slice = mem::replace(&mut self.0, &mut []);
//                                    ^^^^^^^ 

How does this borrow check? If this were an immutable borrow, RFC 1414 indicates that the [] rvalue should have 'static lifetime, so that an immutable borrow would borrow-check, but the example shows a mutable borrow! It seems that one of two things must be going on:

  • Either [] is a temporary (so that it can be used mutably), in which case it would not have 'static lifetime, and should not borrow-check;
  • Or that [] has 'static lifetime, and therefore it should not be possible to take a mutable borrow (since we don't guarantee exclusive access as we take the borrow), and should not borrow-check.

What am I missing?

Related:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Aidan Cully
  • 5,457
  • 24
  • 27
  • 1
    OK, so `[]` is a literal, and therefore statically allocated, I guessed that might be part of it. But then why are we able to take a mutable reference to the common structure? Mutable references are supposed to be exclusive... – Aidan Cully May 09 '19 at 19:52
  • 1
    The other part of the answer appears to be that zero-sized types allow mutable references. Quoting [rfc1414](https://github.com/rust-lang/rfcs/blob/master/text/1414-rvalue_static_promotion.md): "...aliasing mutable references are only safe for zero sized types (since you never dereference the pointer for them)." – Aidan Cully May 09 '19 at 20:08
  • @Shepmaster I think this is a different question than the original, and I've re-written to make that more clear. I'm not sure that my comments above accurately reflect the current state (the text I quoted from rfc1414 suggests that the handling of zero-sized types could be improved in an extension to that RFC), so this may deserve a more authoritative response. – Aidan Cully May 12 '19 at 07:05

1 Answers1

9

TL;DR: empty arrays are special cased in the compiler and it's safe because you can't ever dereference the pointer of a zero-length array, so there's no possible mutable aliasing.


RFC 1414, rvalue static promotion, discusses the mechanism by which values are promoted to static values. It has a section about possible extensions for mutable references (bolding mine):

It would be possible to extend support to &'static mut references, as long as there is the additional constraint that the referenced type is zero sized.

This again has precedence in the array reference constructor:

// valid code today
let y: &'static mut [u8] = &mut [];

The rules would be similar:

  • If a mutable reference to a constexpr rvalue is taken. (&mut <constexpr>)
  • And the constexpr does not contain a UnsafeCell { ... } constructor.
  • And the constexpr does not contain a const fn call returning a type containing a UnsafeCell.
  • And the type of the rvalue is zero-sized.
  • Then instead of translating the value into a stack slot, translate it into a static memory location and give the resulting reference a 'static lifetime.

The zero-sized restriction is there because aliasing mutable references are only safe for zero sized types (since you never dereference the pointer for them).

From this, we can tell that mutable references to empty arrays are currently special-cased in the compiler. In Rust 1.39, the discussed extension has not been implemented:

struct Zero;

fn example() -> &'static mut Zero {
    &mut Zero
}
error[E0515]: cannot return reference to temporary value
 --> src/lib.rs:4:5
  |
4 |     &mut Zero
  |     ^^^^^----
  |     |    |
  |     |    temporary value created here
  |     returns a reference to data owned by the current function

While the array version does work:

fn example() -> &'static mut [i32] {
    &mut []
}

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366