Method auto-(de)ref (and lack of it) in RFC 132 ufcs
Rust Internals [Unofficial]
February 26, 2026
CAD97:
Thus the only version of autoref on argument position I could support would autoref not to
&[mut] $exprsemantics 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)wherex: Box<i32>, f: fn(&i32), you need to introduce the reference yourself asf(&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)ifT: Deref<Target = U>(or&****tas required etc) - Else acts like
f(& { t })ifT = 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?
- 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