From 6b03a1b42352e38fafff67c6d8d685144b8f9a4c Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 29 May 2019 15:17:55 +0000 Subject: [PATCH] Add additional constraints on midpoint(pointer, pointer). Fixes PR#42037. llvm-svn: 361970 --- libcxx/include/numeric | 7 +++++-- .../numeric.ops/numeric.ops.midpoint/midpoint.fail.cpp | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libcxx/include/numeric b/libcxx/include/numeric index 4b08611..62cc29c 100644 --- a/libcxx/include/numeric +++ b/libcxx/include/numeric @@ -527,7 +527,7 @@ lcm(_Tp __m, _Up __n) #if _LIBCPP_STD_VER > 17 template _LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t && !is_same_v, _Tp> +enable_if_t && !is_same_v && !is_null_pointer_v<_Tp>, _Tp> midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK { @@ -548,7 +548,10 @@ _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK template _LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t, _TPtr> +enable_if_t + && is_object_v> + && ! is_void_v> + && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr> midpoint(_TPtr __a, _TPtr __b) noexcept { return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a); diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.fail.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.fail.cpp index 2352ce7..c757610 100644 --- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.fail.cpp +++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.fail.cpp @@ -18,13 +18,22 @@ #include "test_macros.h" +int func1 () { return 1; } +int func2 () { return 2; } + +struct Incomplete; +Incomplete *ip = nullptr; +void *vp = nullptr; + int main(int, char**) { - (void) std::midpoint(false, true); // expected-error {{no matching function for call to 'midpoint'}} + (void) std::midpoint(false, true); // expected-error {{no matching function for call to 'midpoint'}} // A couple of odd pointer types that should fail - (void) std::midpoint(nullptr, nullptr); // expected-error {{no matching function for call to 'midpoint'}} - (void) std::midpoint((void *)0, (void *)0); // expected-error@numeric:* {{arithmetic on pointers to void}} + (void) std::midpoint(nullptr, nullptr); // expected-error {{no matching function for call to 'midpoint'}} + (void) std::midpoint(func1, func2); // expected-error {{no matching function for call to 'midpoint'}} + (void) std::midpoint(ip, ip); // expected-error {{no matching function for call to 'midpoint'}} + (void) std::midpoint(vp, vp); // expected-error {{no matching function for call to 'midpoint'}} return 0; } -- 2.7.4