]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix ambiguity caused by new std::source_location constructor
authorJonathan Wakely <jwakely@redhat.com>
Tue, 3 Feb 2026 15:57:47 +0000 (15:57 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 5 Feb 2026 16:48:20 +0000 (16:48 +0000)
The new constructor added for Contracts support was not explicit, so
caused ambiguities when arbitrary pointers were used in contexts which
could convert to std::source_location.

We don't actually need a constructor, the contract_violation::location()
function can just set the data member directly.

libstdc++-v3/ChangeLog:

* include/std/contracts (contract_violation::location): Use
source_location default constructor and then set _M_impl.
* include/std/source_location (source_location(const void*)):
Remove constructor.
* testsuite/18_support/contracts/includes.cc: Move to...
* testsuite/18_support/contracts/srcloc.cc: ...here. Test for
ambiguity caused by new constructor.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/std/contracts
libstdc++-v3/include/std/source_location
libstdc++-v3/testsuite/18_support/contracts/includes.cc [deleted file]
libstdc++-v3/testsuite/18_support/contracts/srcloc.cc [new file with mode: 0644]

index 0fd9b10247eac38b98feb054d78348ad8a72a617..c1fe54750af96b207bef492956251abb403e75cc 100644 (file)
@@ -92,7 +92,10 @@ namespace contracts
     detection_mode mode() const noexcept { return _M_detection_mode; }
     const char* comment() const noexcept { return _M_comment; }
     std::source_location location() const noexcept {
-      return std::source_location (_M_src_loc_ptr);
+      std::source_location __loc;
+      __loc._M_impl
+          = static_cast<const source_location::__impl*>(_M_src_loc_ptr);
+      return __loc;
     }
     bool is_terminating () const noexcept {
       return _M_evaluation_semantic == std::contracts::evaluation_semantic::enforce
index f773879755c9643904307d7771ffd1bf9a763a3d..8115e059536d867aa8daea13182595869c4ba272 100644 (file)
@@ -89,11 +89,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   private:
     const __impl* _M_impl = nullptr;
 
-    constexpr source_location (const void *__t)
-      : _M_impl (static_cast <const __impl*>(__t)) {}
-
 #ifdef __glibcxx_contracts
-    /* To enable use of the source __impl*.  */
     friend class std::contracts::contract_violation;
 #endif
   };
diff --git a/libstdc++-v3/testsuite/18_support/contracts/includes.cc b/libstdc++-v3/testsuite/18_support/contracts/includes.cc
deleted file mode 100644 (file)
index 91f793f..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-// { dg-options "-fcontracts" }
-// { dg-do compile { target c++26 } }
-
-// We should not get errors from including this before <contracts>:
-#include <source_location>
-#include <contracts>
diff --git a/libstdc++-v3/testsuite/18_support/contracts/srcloc.cc b/libstdc++-v3/testsuite/18_support/contracts/srcloc.cc
new file mode 100644 (file)
index 0000000..cbafa1e
--- /dev/null
@@ -0,0 +1,19 @@
+// { dg-options "-fcontracts" }
+// { dg-do compile { target c++26 } }
+
+// We should not get errors from including this before <contracts>:
+#include <source_location>
+#include <contracts>
+
+// There should be no private std::source_location ctor that participates in
+// the calls to f below.
+struct S { S(char const *); };
+void f(S);
+void f(std::source_location);
+
+void
+test01()
+{
+  f("");   // { dg-bogus "ambiguous" }
+  f({""}); // { dg-bogus "ambiguous" }
+}