]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Implement __is_nothrow_invocable built-in trait
authorKen Matsui <kmatsui@gcc.gnu.org>
Wed, 21 Feb 2024 08:46:56 +0000 (00:46 -0800)
committerKen Matsui <kmatsui@gcc.gnu.org>
Sat, 11 May 2024 01:18:01 +0000 (18:18 -0700)
This patch implements built-in trait for std::is_nothrow_invocable.

gcc/cp/ChangeLog:

* cp-trait.def: Define __is_nothrow_invocable.
* constraint.cc (diagnose_trait_expr): Handle
CPTK_IS_NOTHROW_INVOCABLE.
* semantics.cc (trait_expr_value): Likewise.
(finish_trait_expr): Likewise.

gcc/testsuite/ChangeLog:

* g++.dg/ext/has-builtin-1.C: Test existence of
__is_nothrow_invocable.
* g++.dg/ext/is_nothrow_invocable.C: New test.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/constraint.cc
gcc/cp/cp-trait.def
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/ext/has-builtin-1.C
gcc/testsuite/g++.dg/ext/is_nothrow_invocable.C [new file with mode: 0644]

index 6d14ef7dcc75a1a9ae127f0aa0a3234636a3d2a1..8679d3ce658d8f8ec2cdc556f9375acbab7b96fa 100644 (file)
@@ -3825,6 +3825,12 @@ diagnose_trait_expr (tree expr, tree args)
     case CPTK_IS_NOTHROW_CONVERTIBLE:
          inform (loc, "  %qT is not nothrow convertible from %qE", t2, t1);
       break;
+    case CPTK_IS_NOTHROW_INVOCABLE:
+       if (!t2)
+         inform (loc, "  %qT is not nothrow invocable", t1);
+       else
+         inform (loc, "  %qT is not nothrow invocable by %qE", t1, t2);
+       break;
     case CPTK_IS_OBJECT:
       inform (loc, "  %qT is not an object type", t1);
       break;
index 4e420d5390a63a3ad6f18a32324be3931535ae63..0721ecdf3f0585e5e1c0fe432b295be5a220dfde 100644 (file)
@@ -84,6 +84,7 @@ DEFTRAIT_EXPR (IS_MEMBER_POINTER, "__is_member_pointer", 1)
 DEFTRAIT_EXPR (IS_NOTHROW_ASSIGNABLE, "__is_nothrow_assignable", 2)
 DEFTRAIT_EXPR (IS_NOTHROW_CONSTRUCTIBLE, "__is_nothrow_constructible", -1)
 DEFTRAIT_EXPR (IS_NOTHROW_CONVERTIBLE, "__is_nothrow_convertible", 2)
+DEFTRAIT_EXPR (IS_NOTHROW_INVOCABLE, "__is_nothrow_invocable", -1)
 DEFTRAIT_EXPR (IS_OBJECT, "__is_object", 1)
 DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertible_base_of", 2)
 DEFTRAIT_EXPR (IS_POD, "__is_pod", 1)
index 311e4f472d838b249f1dbc775a48e00f29bba5b6..43b175f92fdc2fea563aab70118e844453f7dba9 100644 (file)
@@ -12592,6 +12592,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_NOTHROW_CONVERTIBLE:
       return is_nothrow_convertible (type1, type2);
 
+    case CPTK_IS_NOTHROW_INVOCABLE:
+      return expr_noexcept_p (build_invoke (type1, type2, tf_none), tf_none);
+
     case CPTK_IS_OBJECT:
       return object_type_p (type1);
 
@@ -12727,6 +12730,7 @@ same_type_ref_bind_p (cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
     case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
     case CPTK_IS_INVOCABLE:
+    case CPTK_IS_NOTHROW_INVOCABLE:
     case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
     case CPTK_REF_CONVERTS_FROM_TEMPORARY:
       to = type1;
@@ -12832,6 +12836,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_INVOCABLE:
     case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
     case CPTK_IS_NOTHROW_CONVERTIBLE:
+    case CPTK_IS_NOTHROW_INVOCABLE:
     case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
     case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
     case CPTK_REF_CONVERTS_FROM_TEMPORARY:
index d1c6626785567276cdb2bd115e63a7e99bf01209..65740a118001c9ee16964b104fa4b809037ce51f 100644 (file)
 #if !__has_builtin (__is_nothrow_convertible)
 # error "__has_builtin (__is_nothrow_convertible) failed"
 #endif
+#if !__has_builtin (__is_nothrow_invocable)
+# error "__has_builtin (__is_nothrow_invocable) failed"
+#endif
 #if !__has_builtin (__is_object)
 # error "__has_builtin (__is_object) failed"
 #endif
diff --git a/gcc/testsuite/g++.dg/ext/is_nothrow_invocable.C b/gcc/testsuite/g++.dg/ext/is_nothrow_invocable.C
new file mode 100644 (file)
index 0000000..2f9b40e
--- /dev/null
@@ -0,0 +1,62 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+using func_type = void(*)();
+SA( ! __is_nothrow_invocable(func_type) );
+
+#if __cpp_noexcept_function_type
+using func_type_nt = void(*)() noexcept;
+SA(   __is_nothrow_invocable(func_type_nt) );
+#endif
+
+struct X { };
+using mem_type = int X::*;
+
+SA( ! __is_nothrow_invocable(mem_type) );
+SA( ! __is_nothrow_invocable(mem_type, int) );
+SA( ! __is_nothrow_invocable(mem_type, int&) );
+SA(   __is_nothrow_invocable(mem_type, X&) );
+
+using memfun_type = int (X::*)();
+
+SA( ! __is_nothrow_invocable(memfun_type) );
+SA( ! __is_nothrow_invocable(memfun_type, int) );
+SA( ! __is_nothrow_invocable(memfun_type, int&) );
+SA( ! __is_nothrow_invocable(memfun_type, X&) );
+SA( ! __is_nothrow_invocable(memfun_type, X*) );
+
+#if __cpp_noexcept_function_type
+using memfun_type_nt = int (X::*)() noexcept;
+
+SA( ! __is_nothrow_invocable(memfun_type_nt) );
+SA( ! __is_nothrow_invocable(memfun_type_nt, int) );
+SA( ! __is_nothrow_invocable(memfun_type_nt, int&) );
+SA(   __is_nothrow_invocable(memfun_type_nt, X&) );
+SA(   __is_nothrow_invocable(memfun_type_nt, X*) );
+#endif
+
+struct F {
+  int& operator()();
+  long& operator()() const noexcept;
+  short& operator()(int) &&;
+  char& operator()(int) const& noexcept;
+private:
+  void operator()(int, int) noexcept;
+};
+using CF = const F;
+
+SA( ! __is_nothrow_invocable(F ) );
+SA(   __is_nothrow_invocable(CF) );
+
+SA( ! __is_nothrow_invocable(F,   int) );
+SA(   __is_nothrow_invocable(F&,  int) );
+
+SA(   __is_nothrow_invocable(CF,   int) );
+SA(   __is_nothrow_invocable(CF&,  int) );
+SA( ! __is_nothrow_invocable(F, int, int) );
+
+struct FX {
+  X operator()() const noexcept { return {}; }
+};
+SA(   __is_nothrow_invocable(FX) );