Borrow the full stable name in closures for ergonomics

I’m in favor of this and I have been thinking for some time of making a similar proposal. This seems to fit a theme. That is, we chose to always borrow local variables because it’s a simple and consistent rule. But I think that we should make an effort to be smarter and less annoying, even if we can’t do a perfect job.

I imagine that roughly the way this would work is that the closure would borrow paths like a.b.c that involve either field access or deref of & or &mut references.

Some complications that come to mind:

  • if you have a field foo: Box<Foo>, and you borrow self.foo.bar, this involves running a custom deref, and hence the closure will still just capture self.foo.
  • we want to do some coallescing, so if you access self.foo.bar and self.foo in one closure, we naturally want to capture self.foo.

Other than this, I believe the proposal ought to be backwards compatible, modulo (as you say) the low-level details of what precisely gets captured (which I also don’t consider to be stable).

2 Likes