]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: std::make_signed_t<cv bool> should be ill-formed
authorJonathan Wakely <jwakely@redhat.com>
Fri, 7 Oct 2022 11:18:57 +0000 (12:18 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 10 Oct 2022 11:19:11 +0000 (12:19 +0100)
Currently we only reject std::make_signed_t<bool> but not cv bool.
Similarly for std::make_unsigned_t<cv bool>.

As well as making those ill-formed, this adds a requires-clause to the
make_signed and make_unsigned primary templates. This makes
non-integral, non-enum cases fail immediately with a clear error, rather
than giving an error about __make_signed_selector<T, false, false> being
incomplete.

libstdc++-v3/ChangeLog:

* include/std/type_traits (make_signed, make_unsigned): Add
specializations for cv bool. Add requires-clause for C++20 to
improve diagnostics for non-integral, non-enum cases.
* testsuite/20_util/make_signed/requirements/typedefs_neg.cc:
Check cv bool.
* testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc:
Likewise.
* testsuite/24_iterators/range_access/range_access_cpp20_neg.cc:
Adjust expected errors for C++20 and later.
* testsuite/lib/prune.exp: Prune "in requirements  [with ...]"
lines from diagnostics.

libstdc++-v3/include/std/type_traits
libstdc++-v3/testsuite/20_util/make_signed/requirements/typedefs_neg.cc
libstdc++-v3/testsuite/20_util/make_unsigned/requirements/typedefs_neg.cc
libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp20_neg.cc
libstdc++-v3/testsuite/lib/prune.exp

index b74565eb521ee55159caff17e8771463f3264c8c..6108b98aa6a6ce207fcc872fcb9a576e18673903 100644 (file)
@@ -1802,12 +1802,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   // Primary template.
   /// make_unsigned
   template<typename _Tp>
+#if __cpp_concepts
+    requires is_integral<_Tp>::value || __is_enum(_Tp)
+#endif
     struct make_unsigned
     { typedef typename __make_unsigned_selector<_Tp>::__type type; };
 
   // Integral, but don't define.
-  template<>
-    struct make_unsigned<bool>;
+  template<> struct make_unsigned<bool>;
+  template<> struct make_unsigned<bool const>;
+  template<> struct make_unsigned<bool volatile>;
+  template<> struct make_unsigned<bool const volatile>;
 
   /// @cond undocumented
 
@@ -1932,12 +1937,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   // Primary template.
   /// make_signed
   template<typename _Tp>
+#if __cpp_concepts
+    requires is_integral<_Tp>::value || __is_enum(_Tp)
+#endif
     struct make_signed
     { typedef typename __make_signed_selector<_Tp>::__type type; };
 
   // Integral, but don't define.
-  template<>
-    struct make_signed<bool>;
+  template<> struct make_signed<bool>;
+  template<> struct make_signed<bool const>;
+  template<> struct make_signed<bool volatile>;
+  template<> struct make_signed<bool const volatile>;
 
 #if __cplusplus > 201103L
   /// Alias template for make_signed
index 051bb64c710c9005f5be109b858465cc774635e2..88b8ae887ef42fe9e4433c9ba491607e7c3968e9 100644 (file)
 // <http://www.gnu.org/licenses/>.
 
 #include <type_traits>
-#include <testsuite_character.h>
 
-enum test_enum { first_selection };
+struct pod_class { };
 
 void test01()
 {
   using std::make_signed;
 
   // Negative tests.
-  typedef make_signed<bool>::type      test1_type;
+  using T1 = make_signed<bool>::type; // { dg-error "incomplete" }
+  using T2 = make_signed<const bool>::type; // { dg-error "incomplete" }
+  using T3 = make_signed<volatile bool>::type; // { dg-error "incomplete" }
+  using T4 = make_signed<const volatile bool>::type; // { dg-error "incomplete" }
 
-  typedef make_signed<__gnu_test::pod_uint>::type      test2_type;
+  using T5 = make_signed<pod_class>::type; // { dg-error "here" }
 
-  typedef make_signed<int[4]>::type     test3_type;
+  using T6 = make_signed<int[4]>::type; // { dg-error "here" }
 
-  typedef void (fn_type) ();
-  typedef make_signed<fn_type>::type   test4_type;
+  using fn_type = void ();
+  using T7 = make_signed<fn_type>::type; // { dg-error "here" }
 
-  typedef make_signed<float>::type     test5_type;
+  using T8 = make_signed<float>::type; // { dg-error "here" }
 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 32 }
-// { dg-error "required from here" "" { target *-*-* } 34 }
-// { dg-error "required from here" "" { target *-*-* } 36 }
-// { dg-error "required from here" "" { target *-*-* } 39 }
-// { dg-error "required from here" "" { target *-*-* } 41 }
-
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 0 }
+// { dg-error "invalid use of incomplete type" "" { target c++17_down } 0 }
+// { dg-error "constraint failure" "" { target c++20 } 0 }
index ff98cc42ef7b262f401a031d3eb26e98b0e8a4eb..50f15e7037c4dd18d76c86b97887c0cb5b17f6f8 100644 (file)
@@ -21,7 +21,6 @@
 
 #include <type_traits>
 
-enum test_enum { first_selection };
 struct pod_class { };
 
 void test01()
@@ -29,22 +28,20 @@ void test01()
   using std::make_unsigned;
 
   // Negative tests.
-  typedef make_unsigned<bool>::type            test1_type;
+  using T1 = make_unsigned<bool>::type; // { dg-error "incomplete" }
+  using T2 = make_unsigned<const bool>::type; // { dg-error "incomplete" }
+  using T3 = make_unsigned<volatile bool>::type; // { dg-error "incomplete" }
+  using T4 = make_unsigned<const volatile bool>::type; // { dg-error "incomplete" }
 
-  typedef make_unsigned<pod_class>::type       test2_type;
+  using T5 = make_unsigned<pod_class>::type; // { dg-error "here" }
 
-  typedef make_unsigned<int[4]>::type     test3_type;
+  using T6 = make_unsigned<int[4]>::type; // { dg-error "here" }
 
-  typedef void (fn_type) ();
-  typedef make_unsigned<fn_type>::type         test4_type;
+  using fn_type = void ();
+  using T7 = make_unsigned<fn_type>::type; // { dg-error "here" }
 
-  typedef make_unsigned<float>::type           test5_type;
+  using T8 = make_unsigned<float>::type; // { dg-error "here" }
 }
 
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 32 }
-// { dg-error "required from here" "" { target *-*-* } 34 }
-// { dg-error "required from here" "" { target *-*-* } 36 }
-// { dg-error "required from here" "" { target *-*-* } 39 }
-// { dg-error "required from here" "" { target *-*-* } 41 }
-
-// { dg-error "invalid use of incomplete type" "" { target *-*-* } 0 }
+// { dg-error "invalid use of incomplete type" "" { target c++17_down } 0 }
+// { dg-error "constraint failure" "" { target c++20 } 0 }
index c0825a585871248d3e9b7013024ad3dc2671f754..26c8ae0ee1e7a98b7876cb0b64842f5d880bad50 100644 (file)
@@ -46,4 +46,5 @@ test03()
   C c;
   std::ssize(c);  // { dg-error "no matching function" }
 }
-// { dg-error "incomplete type .*make_signed.*S" "" { target *-*-* } 0 }
+// { dg-error "incomplete type .*make_signed.*S" "" { target c++17_down } 0 }
+// { dg-error "constraint failure" "" { target c++20 } 0 }
index d457e9752180eb900e74b25ea87039007be51080..6d0b77a8ccd86a02d3a5d18a69a8b5cdd9ef7b52 100644 (file)
@@ -51,6 +51,7 @@ proc libstdc++-dg-prune { system text } {
     regsub -all "(^|\n)\[^\n\]*:   (recursively )?required \[^\n\]*" $text "" text
     regsub -all "(^|\n)\[^\n\]*:   . skipping \[0-9\]* instantiation contexts \[^\n\]*" $text "" text
     regsub -all "(^|\n)\[^\n\]*:   in .constexpr. expansion \[^\n\]*" $text "" text
+    regsub -all "(^|\n)\[^\n\]*:   in requirements  .with\[^\n\]*" $text "" text
     regsub -all "(^|\n)    inlined from \[^\n\]*" $text "" text
     # Why doesn't GCC need these to strip header context?
     regsub -all "(^|\n)In file included from \[^\n\]*" $text "" text