[libcxx-commits] [libcxx] 727fef7 - [libc++] Add floating point type check for uniform real distribution (#70564)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Dec 7 06:06:59 PST 2023
Author: Nhat Nguyen
Date: 2023-12-07T09:06:54-05:00
New Revision: 727fef79c0421133744700717603ff1b8a7d6628
URL: https://github.com/llvm/llvm-project/commit/727fef79c0421133744700717603ff1b8a7d6628
DIFF: https://github.com/llvm/llvm-project/commit/727fef79c0421133744700717603ff1b8a7d6628.diff
LOG: [libc++] Add floating point type check for uniform real distribution (#70564)
Fixes #62433
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>
Added:
libcxx/test/libcxx/numerics/rand/rand.req.urng/valid_real_type.verify.cpp
Modified:
libcxx/include/__random/cauchy_distribution.h
libcxx/include/__random/chi_squared_distribution.h
libcxx/include/__random/exponential_distribution.h
libcxx/include/__random/extreme_value_distribution.h
libcxx/include/__random/fisher_f_distribution.h
libcxx/include/__random/gamma_distribution.h
libcxx/include/__random/is_valid.h
libcxx/include/__random/lognormal_distribution.h
libcxx/include/__random/normal_distribution.h
libcxx/include/__random/piecewise_constant_distribution.h
libcxx/include/__random/piecewise_linear_distribution.h
libcxx/include/__random/student_t_distribution.h
libcxx/include/__random/uniform_real_distribution.h
libcxx/include/__random/weibull_distribution.h
Removed:
################################################################################
diff --git a/libcxx/include/__random/cauchy_distribution.h b/libcxx/include/__random/cauchy_distribution.h
index b057074ab8d8f..44d7e0e27c14d 100644
--- a/libcxx/include/__random/cauchy_distribution.h
+++ b/libcxx/include/__random/cauchy_distribution.h
@@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS cauchy_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/chi_squared_distribution.h b/libcxx/include/__random/chi_squared_distribution.h
index 5cae56b264178..ce17a283d4955 100644
--- a/libcxx/include/__random/chi_squared_distribution.h
+++ b/libcxx/include/__random/chi_squared_distribution.h
@@ -11,6 +11,7 @@
#include <__config>
#include <__random/gamma_distribution.h>
+#include <__random/is_valid.h>
#include <iosfwd>
#include <limits>
@@ -26,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS chi_squared_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/exponential_distribution.h b/libcxx/include/__random/exponential_distribution.h
index e5f0dad8cb882..e9df4de90e747 100644
--- a/libcxx/include/__random/exponential_distribution.h
+++ b/libcxx/include/__random/exponential_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS exponential_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/extreme_value_distribution.h b/libcxx/include/__random/extreme_value_distribution.h
index 4afc275981575..f8f18ebf7896c 100644
--- a/libcxx/include/__random/extreme_value_distribution.h
+++ b/libcxx/include/__random/extreme_value_distribution.h
@@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS extreme_value_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/fisher_f_distribution.h b/libcxx/include/__random/fisher_f_distribution.h
index 1915cdb06a843..8da689a3b0f4f 100644
--- a/libcxx/include/__random/fisher_f_distribution.h
+++ b/libcxx/include/__random/fisher_f_distribution.h
@@ -27,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS fisher_f_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/gamma_distribution.h b/libcxx/include/__random/gamma_distribution.h
index e2f04ad51727e..bdf8e141ece73 100644
--- a/libcxx/include/__random/gamma_distribution.h
+++ b/libcxx/include/__random/gamma_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS gamma_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/is_valid.h b/libcxx/include/__random/is_valid.h
index 113452bc5d35a..bde4733099d68 100644
--- a/libcxx/include/__random/is_valid.h
+++ b/libcxx/include/__random/is_valid.h
@@ -23,6 +23,20 @@
_LIBCPP_BEGIN_NAMESPACE_STD
+// [rand.req.genl]/1.4:
+// The effect of instantiating a template that has a template type parameter
+// named RealType is undefined unless the corresponding template argument is
+// cv-unqualified and is one of float, double, or long double.
+
+template <class>
+struct __libcpp_random_is_valid_realtype : false_type {};
+template <>
+struct __libcpp_random_is_valid_realtype<float> : true_type {};
+template <>
+struct __libcpp_random_is_valid_realtype<double> : true_type {};
+template <>
+struct __libcpp_random_is_valid_realtype<long double> : true_type {};
+
// [rand.req.genl]/1.5:
// The effect of instantiating a template that has a template type parameter
// named IntType is undefined unless the corresponding template argument is
diff --git a/libcxx/include/__random/lognormal_distribution.h b/libcxx/include/__random/lognormal_distribution.h
index c30f9adf103a1..47db56ee45298 100644
--- a/libcxx/include/__random/lognormal_distribution.h
+++ b/libcxx/include/__random/lognormal_distribution.h
@@ -10,6 +10,7 @@
#define _LIBCPP___RANDOM_LOGNORMAL_DISTRIBUTION_H
#include <__config>
+#include <__random/is_valid.h>
#include <__random/normal_distribution.h>
#include <cmath>
#include <iosfwd>
@@ -27,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS lognormal_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/normal_distribution.h b/libcxx/include/__random/normal_distribution.h
index 14649f1757ff6..9d68ea6118fd0 100644
--- a/libcxx/include/__random/normal_distribution.h
+++ b/libcxx/include/__random/normal_distribution.h
@@ -28,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS normal_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/piecewise_constant_distribution.h b/libcxx/include/__random/piecewise_constant_distribution.h
index f5b90290209bb..3a60e5533d773 100644
--- a/libcxx/include/__random/piecewise_constant_distribution.h
+++ b/libcxx/include/__random/piecewise_constant_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS piecewise_constant_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/piecewise_linear_distribution.h b/libcxx/include/__random/piecewise_linear_distribution.h
index 824e089b0c349..895f0f2dfe949 100644
--- a/libcxx/include/__random/piecewise_linear_distribution.h
+++ b/libcxx/include/__random/piecewise_linear_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/student_t_distribution.h b/libcxx/include/__random/student_t_distribution.h
index d52510dca4ed7..0879a411c0c56 100644
--- a/libcxx/include/__random/student_t_distribution.h
+++ b/libcxx/include/__random/student_t_distribution.h
@@ -29,6 +29,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS student_t_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/uniform_real_distribution.h b/libcxx/include/__random/uniform_real_distribution.h
index c5ee527353ab2..28b6e9181c5f3 100644
--- a/libcxx/include/__random/uniform_real_distribution.h
+++ b/libcxx/include/__random/uniform_real_distribution.h
@@ -27,6 +27,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/include/__random/weibull_distribution.h b/libcxx/include/__random/weibull_distribution.h
index 099cfdd6171b1..9967992810310 100644
--- a/libcxx/include/__random/weibull_distribution.h
+++ b/libcxx/include/__random/weibull_distribution.h
@@ -11,6 +11,7 @@
#include <__config>
#include <__random/exponential_distribution.h>
+#include <__random/is_valid.h>
#include <cmath>
#include <iosfwd>
#include <limits>
@@ -27,6 +28,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template<class _RealType = double>
class _LIBCPP_TEMPLATE_VIS weibull_distribution
{
+ static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
+ "RealType must be a supported floating-point type");
+
public:
// types
typedef _RealType result_type;
diff --git a/libcxx/test/libcxx/numerics/rand/rand.req.urng/valid_real_type.verify.cpp b/libcxx/test/libcxx/numerics/rand/rand.req.urng/valid_real_type.verify.cpp
new file mode 100644
index 0000000000000..6c6a109227df9
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/rand/rand.req.urng/valid_real_type.verify.cpp
@@ -0,0 +1,109 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+#include <random>
+
+void test() {
+ {
+ std::uniform_real_distribution<int>
+ baddist; //expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::uniform_real_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+ {
+ std::exponential_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::exponential_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::gamma_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::gamma_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::weibull_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::weibull_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::extreme_value_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::extreme_value_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::normal_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::normal_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::lognormal_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::lognormal_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::chi_squared_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::chi_squared_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::cauchy_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::cauchy_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::fisher_f_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::fisher_f_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::student_t_distribution<int> baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::student_t_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::piecewise_constant_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::piecewise_constant_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+
+ {
+ std::piecewise_linear_distribution<int>
+ baddist; // expected-error@*:* {{RealType must be a supported floating-point type}}
+ std::piecewise_linear_distribution<double> okdist;
+ (void)baddist;
+ (void)okdist;
+ }
+}
More information about the libcxx-commits
mailing list