I think there is a mistake in the explanation related to temporary lifetime extension in the documentation for std::ptr::from_ref():
Note that this has subtle interactions with the rules for lifetime extension of temporaries in tail expressions. This code is valid, albeit in a non-obvious way:
// The temporary holding the return value of `foo` has its lifetime extended, // because the surrounding expression involves no function call. let p = &foo() as *const T; unsafe { p.read() };
Naively replacing the cast with
from_ref
is not valid:// The temporary holding the return value of `foo` does *not* have its lifetime extended, // because the surrounding expression involves no function call. let p = ptr::from_ref(&foo()); unsafe { p.read() }; // UB! Reading from a dangling pointer ⚠️
In the first snippet, the comment says the lifetime is extended because the expression "involves no function call", which seems correct.
But the second snippet says the return value "does not have its lifetime extended" ... "expression involves no function call". I think this should say "does involve a function call".
Am I correct? Didn't want to submit a PR without getting someone else to confirm my understanding first.