Today I encountered an interesting bug in my program. Given this piece of code:
let table: Rc<RefCell<HashMap<Object,Object>>> = some_table();
{
table.borrow_mut().insert(key,value);
}
Now the return value of insert may be an object that calls drop and drop calls some arbitrary code which could then again take a borrow of table. And so it happened. At that time, however, table was still borrowed. The problem is averted easily by introducing a temporary variable to delay drop:
{
let _delay = table.borrow_mut().insert(key,value);
}
My question now is, whether the behavior of the first version is intended, i.e. the order – dropping table before the RefMut value – is exactly specified.