Skip to content

[nll] cast to raw pointer creates an "enduring" borrow #53123

Closed
@nikomatsakis

Description

@nikomatsakis

The following program (extracted from try_transform_mut v0.1.0) fails to compile with MIR-based borrow check:

#![feature(nll)]
#![allow(unused_variables)]

pub trait TryTransform {
    fn try_transform<F>(self, f: F)
    where
        Self: Sized,
        F: FnOnce(Self);
}

impl<'a, T> TryTransform for &'a mut T {
    fn try_transform<F>(self, f: F)
    where
        Self: Sized,
        F: FnOnce(Self),
    {
        let this: *mut T = self as *mut T;
        f(self);
    }
}

fn main() {
}

The error is:

error[E0499]: cannot borrow `*self` as mutable more than once at a time
  --> src/main.rs:18:11
   |
17 |         let this: *mut T = self as _;
   |                            ---- first mutable borrow occurs here
18 |         f(self);
   |           ^^^^ second mutable borrow occurs here
   |
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 11:6...
  --> src/main.rs:11:6
   |
11 | impl<'a, T> TryTransform for &'a mut T {
   |      ^^

I'm not entirely sure what is going on here, but it seems strange. I would not consider the self as _ to be a "borrow" of self really.

Metadata

Metadata

Assignees

Labels

E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.NLL-completeWorking towards the "valid code works" goalT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions