This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Fix std::to_address(arr)
ClosedPublic

Authored by Quuxplusone on Sep 6 2021, 11:37 AM.

Details

Reviewers
ldionne
zoecarver
Mordante
Group Reviewers
Restricted Project
Commits
rGdadbe88a1387: [libc++] Fix std::to_address(array).
Summary

There were basically two bugs here:

  • When C++20 to_address is called on int arr[10], then const _Ptr& becomes a reference to a const array, and then we dispatch to __to_address<const int(&)[10]>, which, oops, gives us a const int* result instead of an int* result. Solution: We need to provide the two standard-specified overloads of std::to_address in exactly the same way that we provide two overloads of __to_address.
  • When __to_address is called on a pointer type, __to_address(const _Ptr&) is disabled so we successfully avoid trying to instantiate pointer_traits of that pointer type. But when it's called on an array type, __to_address(const _Ptr&) is not disabled for array types, so we go ahead and instantiate pointer_traits<int[10]>, which goes boom. Solution: We need to disable __to_address(const _Ptr&) for both pointer and array types.

Maybe we should disable it for function types too? Those are supposed not to work, but it'd affect the wording of the error message we gave. [This has now been done.]

Diff Detail

Event Timeline

Quuxplusone requested review of this revision.Sep 6 2021, 11:37 AM
Quuxplusone created this revision.
Herald added 1 blocking reviewer(s): Restricted Project. · View Herald TranscriptSep 6 2021, 11:37 AM

Maybe we should disable it for function types too? Those are supposed not to work, but it'd affect the wording of the error message we gave.

I think that would be an Quality of Implementation improvement, so I would like to see that change. But not a blocker to land the current fix. LGTM, provided the build passes.

libcxx/include/__memory/pointer_traits.h
171

Looking at this static assert I feel it's intended this assertion's error message should be shown when a function pointer is used.

Mordante accepted this revision as: Mordante.Sep 6 2021, 12:14 PM
Quuxplusone edited the summary of this revision. (Show Details)

Disable the const _Ptr& overload for function types, as well as array types. Turns out we already got the nice error message for std::to_address(f), but we would give the ugly __pointer_traits_element_type spew if you did std::__to_address(f). This is now fixed, and put a test on it.

Thanks for the update. LGTM!

ldionne accepted this revision.Sep 7 2021, 9:44 AM
This revision is now accepted and ready to land.Sep 7 2021, 9:44 AM
This revision was automatically updated to reflect the committed changes.