https://gcc.gnu.org/g:269ce3de8c49c2244a5b4aeee5c3a90a26253da1

commit r16-7338-g269ce3de8c49c2244a5b4aeee5c3a90a26253da1
Author: Jonathan Wakely <[email protected]>
Date:   Tue Feb 3 15:57:47 2026 +0000

    libstdc++: Fix ambiguity caused by new std::source_location constructor
    
    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 <[email protected]>

Diff:
---
 libstdc++-v3/include/std/contracts                    |  5 ++++-
 libstdc++-v3/include/std/source_location              |  4 ----
 .../testsuite/18_support/contracts/includes.cc        |  6 ------
 libstdc++-v3/testsuite/18_support/contracts/srcloc.cc | 19 +++++++++++++++++++
 4 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/libstdc++-v3/include/std/contracts 
b/libstdc++-v3/include/std/contracts
index 0fd9b10247ea..c1fe54750af9 100644
--- a/libstdc++-v3/include/std/contracts
+++ b/libstdc++-v3/include/std/contracts
@@ -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
diff --git a/libstdc++-v3/include/std/source_location 
b/libstdc++-v3/include/std/source_location
index f773879755c9..8115e059536d 100644
--- a/libstdc++-v3/include/std/source_location
+++ b/libstdc++-v3/include/std/source_location
@@ -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
index 91f793f1e201..000000000000
--- a/libstdc++-v3/testsuite/18_support/contracts/includes.cc
+++ /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
index 000000000000..cbafa1e69bc8
--- /dev/null
+++ b/libstdc++-v3/testsuite/18_support/contracts/srcloc.cc
@@ -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" }
+}

Reply via email to