This desugaring doesn't feel right:
- What are
get_arg()
andArg
here? - How did the
&self
parameter become&mut self
? This is unsound in presence of code that callsvisit
while expecting it to take an&self
.
// `Foo`'s definition is valid today, so it has to compile
trait Foo {
fn foo(&self);
fn bar(&self, f: impl Fn());
fn baz(&self) {
// This is possible only because both `bar` and `foo` take `&self`.
self.bar(|| self.foo())
}
}
impl Foo for MyNode
with(ob: &mut Option<Box<i32>>)
{
fn foo(&self) {
*ob = None; // Invalidates any reference to the inside of `ob`
}
fn bar(&self, f: impl Fn()) {
// Let's assume that the box is valid now
let box_ref: &Box<i32> = ob.as_ref().unwrap();
f(); // This calls `self.foo()` which invalidates `box_ref`
println!("{}", box_ref); // Oops, now we're printing invalid data
}
}
fn main() {
with(cx: &mut Some(Box::new(0)) {
MyNode.baz();
}
}