[llvm-bugs] [Bug 50001] New: string_view equality operator does not compile with clang-cl

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Apr 16 11:07:44 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50001

            Bug ID: 50001
           Summary: string_view equality operator does not compile with
                    clang-cl
           Product: libc++
           Version: unspecified
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: mhochk at microsoft.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

Context first:

There is a name-mangling issue/bug/limitation in clang-cl (doesn't appear to
impact clang directly) easily repro'd by attempting to compile the following
(locally named clang-cl-issue.cpp):

template <class T>
struct X {
  T member;
};
template <class T>
struct ConvertibleToX {
  operator X<T>() { return {T{}}; }
};
template <class T>
struct identity {
  using type = T;
};
template <class T>
bool operator==(const X<T> lhs, const X<T> rhs) {
  return (true);
}
template <class T>
bool operator==(const X<T> lhs, const typename identity<X<T>>::type rhs) {
  return (true);
}
template <class T>
bool operator==(const typename identity<X<T>>::type lhs, const X<T> rhs) {
  return (true);
}
int main() {
  X<int> xVal{42};
  ConvertibleToX<int> xConvVal;
  bool result = xVal == xVal;
  result = xVal == xConvVal;
  result = xConvVal == xVal;
  return result ? 1 : 0;
}



RESULT:
clang-cl-issue.cpp(18,6): error: definition with same mangled name
'??$?8H@@YA_NU?$X at H@@0 at Z' as another definition
bool operator==(const X<T> lhs, const typename identity<X<T>>::type rhs) {
     ^
clang-cl-issue.cpp(14,6): note: previous definition is here
bool operator==(const X<T> lhs, const X<T> rhs) {
     ^
clang-cl-issue.cpp(22,6): error: definition with same mangled name
'??$?8H@@YA_NU?$X at H@@0 at Z' as another definition
bool operator==(const typename identity<X<T>>::type lhs, const X<T> rhs) {
     ^
clang-cl-issue.cpp(14,6): note: previous definition is here
bool operator==(const X<T> lhs, const X<T> rhs) {



This issue is relevant to the libc++ component because the string_view
implementation currently uses the same pattern, raising the same errors with
clang-cl with very simple usage. For example, attempting to compile:

#include <string>
#include <string_view>
#include <unordered_set>

constexpr std::string_view kSomeView("Hello");

bool TestFunction(
  const std::unordered_set<std::string_view>& telemetry,
  const std::string& s) noexcept
{
  if (telemetry.find(s) != telemetry.end()) {
    return s == kSomeView;
  }
  return false;
}



RESULT:
{MyLocalPath}\libcxx\include\string_view(614,6): error: definition with same
mangled name
     
'??$?8DU?$char_traits at D@__1 at std@@@__1 at std@@YA_NV?$basic_string_view at DU?$char_traits at D@__1 at std@@@01 at 0@Z'
as another definition
bool operator==(basic_string_view<_CharT, _Traits> __lhs,
     ^
{MyLocalPath}\libcxx\include\string_view(632,6): note: previous definition is
here
bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type
__lhs,
     ^



Though the root issue is with clang-cl, this can be worked around very easily
by adding additional qualifiers to the string_view equality operator templates
to force them to be unique. Added below ", int = 1" and ", int = 2" fixes this
problem for string_view usage.

// [string.view.comparison]
// operator ==
template<class _CharT, class _Traits>
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
bool operator==(basic_string_view<_CharT, _Traits> __lhs,
                basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
{
    if ( __lhs.size() != __rhs.size()) return false;
    return __lhs.compare(__rhs) == 0;
}

template<class _CharT, class _Traits, int = 1>
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
bool operator==(basic_string_view<_CharT, _Traits> __lhs,
                typename common_type<basic_string_view<_CharT, _Traits> >::type
__rhs) _NOEXCEPT
{
    if ( __lhs.size() != __rhs.size()) return false;
    return __lhs.compare(__rhs) == 0;
}

template<class _CharT, class _Traits, int = 2>
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type
__lhs,
                basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
{
    if ( __lhs.size() != __rhs.size()) return false;
    return __lhs.compare(__rhs) == 0;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210416/3511508d/attachment-0001.html>


More information about the llvm-bugs mailing list