From: Jonathan Wakely Date: Tue, 3 Feb 2026 15:57:47 +0000 (+0000) Subject: libstdc++: Fix ambiguity caused by new std::source_location constructor X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=269ce3de8c49c2244a5b4aeee5c3a90a26253da1;p=thirdparty%2Fgcc.git 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 --- diff --git a/libstdc++-v3/include/std/contracts b/libstdc++-v3/include/std/contracts index 0fd9b10247e..c1fe54750af 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(_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 f773879755c..8115e059536 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 (__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 91f793f1e20..00000000000 --- 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 : -#include -#include 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 00000000000..cbafa1e69bc --- /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 : +#include +#include + +// 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" } +}