Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad interaction of smart pointers, auto-deref and closures #27106

Closed
RalfJung opened this issue Jul 18, 2015 · 3 comments
Closed

Bad interaction of smart pointers, auto-deref and closures #27106

RalfJung opened this issue Jul 18, 2015 · 3 comments
Labels
A-type-system Area: Type system

Comments

@RalfJung
Copy link
Member

The following code does not compile:

use std::rc::Rc;
use std::cell::RefCell;

pub struct Callbacks {
    callbacks: Vec<Rc<RefCell<FnMut(i32)>>>,
}

impl Callbacks {
    pub fn call(&mut self, val: i32) {
        for callback in self.callbacks.iter() {
            let mut closure = callback.borrow_mut();
            closure(val);
        }
    }
}

(Playpen)

It says: "cannot borrow immutable borrowed content as mutable".

Instead, I have to explicitly dereference and borrow the closure in the loop, with (&mut *closure)(val). Typically, Rust performs this automatically, so I expected that to also happen in this case. At the very least, the error message should be improved, as currently, it doesn't help with the actual issue at all.

eefriedman added a commit to eefriedman/rust that referenced this issue Jul 18, 2015
See issue rust-lang#27106.

The non-hacky version requires a substantial amount of refactoring.
Conceptually, though, this is straightforward: we just need to make
calls use the existing code to handle `&mut self` in
confirm::fixup_derefs_on_method_receiver_if_necessary.
@eefriedman
Copy link
Contributor

@Diggsey
Copy link
Contributor

Diggsey commented Sep 10, 2016

This looks like a duplicate of #26186

@Mark-Simulacrum
Copy link
Member

Closing in favor of #26186.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

5 participants