This came out of @zoecarver's observation on D101404 that std::to_address wasn't working correctly with a type such as vector<int>::iterator whose element_type was being deduced wrongly. std::to_address actually shouldn't care about element_type at all.
[libc++] std::to_address mustn't depend on P::element_type. Also, make sure it simulates an `auto` return type correctly. [libc++] Prefer __to_address(it) over addressof(*it), for UBSAN's benefit. UBSAN doesn't like `addressof(*it)` when `it` is positioned one-past-the-end of an array, because that expression binds a reference to a non-object. On the other hand, we have an overload of `__to_address` for raw pointer types that Just Works; and we can expect that any user-provided fancy pointer type will implement `operator->` so that `__to_address` will work just as well (or better) than `operator*`. This fixes a UBSAN failure in the previous commit's new test: iterator:1343:42: runtime error: reference binding to address 0x[...] with insufficient space for an object of type 'const char' [...] in std::__1::__wrap_iter<char const*>::operator->() const in std::__1::__to_address_helper<false> in std::__1::__to_address<std::__1::__wrap_iter<char const*> > in auto std::__1::to_address<std::__1::__wrap_iter<char const*> > in void test_container_iterators<std::__1::span<char const> >
The aforementioned failing test is here: https://buildkite.com/llvm-project/libcxx-ci/builds/2924
(Non-blocking) Could we get rid of __return_type and just make this auto everywhere? We'd have to change __choose_to_address to something like _IsValidExpansion<decltype(pointer_traits<_Pointer>::to_address(declval<const _Pointer&>()))>. I don't know if that would work but wdyt?