]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Find make_error_code and make_error_condition via ADL only
authorJonathan Wakely <jwakely@redhat.com>
Wed, 7 Sep 2022 19:17:04 +0000 (20:17 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 28 Mar 2023 22:12:28 +0000 (23:12 +0100)
The new proposed resolution for LWG 3629 says that std::error_code and
std::error_condition should only use ADL to find their customization
points. This means we need to use a poison pill to prevent lookup from
finding overloads in the enclosing namespaces.

We can also remove the forward declarations of std::make_error_code and
std::make_error_condition, because they aren't needed now. ADL can find
them anyway (when std is an associated namespace), and unqualified name
lookup will not (and should not) find them.

libstdc++-v3/ChangeLog:

* include/std/system_error (__adl_only::make_error_code): Add
deleted function.
(__adl_only::make_error_condition): Likewise.
(error_code::error_code(ErrorCodeEnum)): Add using-declaration
for deleted function.
(error_condition::error_condition(ErrorConditionEnum)):
Likewise.
* testsuite/19_diagnostics/error_code/cons/lwg3629.cc: New test.
* testsuite/19_diagnostics/error_condition/cons/lwg3629.cc: New test.

(cherry picked from commit d3883dc77b1426984c0edea6081f57ed2305c9f2)

libstdc++-v3/include/std/system_error
libstdc++-v3/testsuite/19_diagnostics/error_code/cons/lwg3629.cc [new file with mode: 0644]
libstdc++-v3/testsuite/19_diagnostics/error_condition/cons/lwg3629.cc [new file with mode: 0644]

index 31d2f584251ca19955121dffffc2acdcc837a354..28f01da3fc2473f96c62f263b6962b9306acbbd9 100644 (file)
@@ -194,7 +194,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    * @{
    */
 
-  error_code make_error_code(errc) noexcept;
+namespace __adl_only
+{
+  void make_error_code() = delete;
+  void make_error_condition() = delete;
+}
 
   /** Class error_code
    *
@@ -225,7 +229,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     template<typename _ErrorCodeEnum, typename = typename
             enable_if<is_error_code_enum<_ErrorCodeEnum>::value>::type>
       error_code(_ErrorCodeEnum __e) noexcept
-      { *this = make_error_code(__e); }
+      {
+       using __adl_only::make_error_code;
+       *this = make_error_code(__e);
+      }
 
     void
     assign(int __v, const error_category& __cat) noexcept
@@ -328,8 +335,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
     { return (__os << __e.category().name() << ':' << __e.value()); }
 
-  error_condition make_error_condition(errc) noexcept;
-
   /** Class error_condition
    *
    * This class represents error conditions that may be visible at an API
@@ -356,7 +361,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     template<typename _ErrorConditionEnum, typename = typename
         enable_if<is_error_condition_enum<_ErrorConditionEnum>::value>::type>
       error_condition(_ErrorConditionEnum __e) noexcept
-      { *this = make_error_condition(__e); }
+      {
+       using __adl_only::make_error_condition;
+       *this = make_error_condition(__e);
+      }
 
     /// Set the value and category.
     void
diff --git a/libstdc++-v3/testsuite/19_diagnostics/error_code/cons/lwg3629.cc b/libstdc++-v3/testsuite/19_diagnostics/error_code/cons/lwg3629.cc
new file mode 100644 (file)
index 0000000..70fa5e8
--- /dev/null
@@ -0,0 +1,48 @@
+// { dg-do compile { target c++11 } }
+
+// 3629. make_error_code and make_error_condition are customization points
+// Verify that make_error_code is looked up using ADL only.
+
+namespace user
+{
+  struct E1;
+}
+
+// N.B. not in associated namespace of E1, and declared before <system_error>.
+user::E1 make_error_code(user::E1);
+
+#include <future> // declares std::make_error_code(future_errc)
+#include <system_error>
+
+namespace user
+{
+  struct E1
+  {
+    operator std::error_code() const;
+  };
+
+  struct E2
+  {
+    operator std::future_errc() const;
+  };
+
+  struct E3
+  {
+    operator std::errc() const;
+  };
+}
+
+template<> struct std::is_error_code_enum<user::E1> : std::true_type { };
+template<> struct std::is_error_code_enum<user::E2> : std::true_type { };
+template<> struct std::is_error_code_enum<user::E3> : std::true_type { };
+
+// ::make_error_code(E1) should not be found by name lookup.
+std::error_code e1( user::E1{} ); // { dg-error "here" }
+
+// std::make_error_code(future_errc) should not be found by name lookup.
+std::error_code e2( user::E2{} ); // { dg-error "here" }
+
+// std::make_error_code(errc) should not be found by name lookup.
+std::error_code e3( user::E3{} ); // { dg-error "here" }
+
+// { dg-error "use of deleted function" "" { target *-*-* } 0 }
diff --git a/libstdc++-v3/testsuite/19_diagnostics/error_condition/cons/lwg3629.cc b/libstdc++-v3/testsuite/19_diagnostics/error_condition/cons/lwg3629.cc
new file mode 100644 (file)
index 0000000..562a99a
--- /dev/null
@@ -0,0 +1,48 @@
+// { dg-do compile { target c++11 } }
+
+// 3629. make_error_code and make_error_condition are customization points
+// Verify that make_error_condition is looked up using ADL only.
+
+namespace user
+{
+  struct E1;
+}
+
+// N.B. not in associated namespace of E1, and declared before <system_error>.
+user::E1 make_error_condition(user::E1);
+
+#include <future> // declares std::make_error_condition(future_errc)
+#include <system_error>
+
+namespace user
+{
+  struct E1
+  {
+    operator std::error_code() const;
+  };
+
+  struct E2
+  {
+    operator std::future_errc() const;
+  };
+
+  struct E3
+  {
+    operator std::errc() const;
+  };
+}
+
+template<> struct std::is_error_condition_enum<user::E1> : std::true_type { };
+template<> struct std::is_error_condition_enum<user::E2> : std::true_type { };
+template<> struct std::is_error_condition_enum<user::E3> : std::true_type { };
+
+// ::make_error_condition(E1) should not be found by name lookup.
+std::error_condition e1( user::E1{} ); // { dg-error "here" }
+
+// std::make_error_condition(future_errc) should not be found by name lookup.
+std::error_condition e2( user::E2{} ); // { dg-error "here" }
+
+// std::make_error_condition(errc) should not be found by name lookup.
+std::error_condition e3( user::E3{} ); // { dg-error "here" }
+
+// { dg-error "use of deleted function" "" { target *-*-* } 0 }