External Publication
Visit Post

Method auto-(de)ref (and lack of it) in RFC 132 ufcs

Rust Internals [Unofficial] February 26, 2026
Source

CAD97:

Thus the only version of autoref on argument position I could support would autoref not to &[mut] $expr semantics but instead to &[mut] { $expr } semantics, which puts the expression still in a value expression position, meaning that the source place gets moved from.

[1]

CAD97:

you can't do f(x) where x: Box<i32>, f: fn(&i32), you need to introduce the reference yourself as f(&x) in order for autoderef to project through. [...] I think nowadays we could justify removing this restriction, thus allowing automatic reborrows of non-reference types, at least non-mutably.

Taking these together, do you mean:

Given t: T and f: fn(&U) and T not &U, f(t):

  • Acts like f(&*t) if T: Deref<Target = U> (or &****t as required etc)
  • Else acts like f(& { t }) if T = U
  • (Else doesn't compile or whatever, not going to attempt being exhaustive)

So for example,

fn g<T>(_: &T) {}

fn ex(bx: Box<i32>) {
    g::<i32>(bx);      // Doesn't drop `bx`: `g(&*bx)`
    g::<Box<i32>>(bx); // Drops `bx`:        `g(& { bx })`
}

I.e. where things drop depends on probing types for Deref impls?


  1. I've seen people want that, and I think it has the same "moved it? oops guess I'll clone" hazards as P: AsRef<Path>. I'm willing to be convinced it's fine, but that's also not the actual point of this reply, hence hiding this in a footnote. ↩︎

Discussion in the ATmosphere

Loading comments...