public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang)
@ 2023-06-06  9:18 joseph.faulls at imgtec dot com
  2023-06-06  9:18 ` [Bug libstdc++/110139] " joseph.faulls at imgtec dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: joseph.faulls at imgtec dot com @ 2023-06-06  9:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

            Bug ID: 110139
           Summary: [libstdc++] Ambiguous use of [] operator for 0-sized
                    arrays (with clang)
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: joseph.faulls at imgtec dot com
  Target Milestone: ---

Created attachment 55269
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55269&action=edit
Preprocessed test

Hi,

This issue only happens when clang uses the array headers on a 32-bit platform.
It's entirely possible that clang itself is the issue, so apologies if this bug
report is unsuitable. With a similar test case (attached as
similar_test_case.cpp), GCC does give me a warning. See bottom of this bug
report for test case and error.

What I think is happening:
By defining the * operator for the _Type of struct __array_traits<_Tp, 0> we
bring in the built-in [] operator for the type we offset by. For example,
offsetting by an integer, the overload resolution sees both the built in
operator `operator[](int *, int)` and the defined [] operator for _Type `_Tp&
operator[](size_t) const noexcept { __builtin_trap(); }`. The reason this only
happens on 32-bit platforms is because the offset type size would match the
size_t.


test.cpp:
#include <array>
template class std::array<int, 0> ;


Error:
include/c++/13.1.0/array:243:9: error: use of overloaded operator '[]' is
ambiguous (with operand types 'typename __array_traits<int, 0U>::_Type' and
'int')
        return _M_elems[0];
               ^~~~~~~~ ~
test.cpp:3:21: note: in instantiation of member function 'std::array<int,
0>::front' requested here
template class std::array<int, 0> ;
                    ^
include/c++/13.1.0/array:68:13: note: candidate function
       _Tp& operator[](size_t) const noexcept { __builtin_trap(); }
            ^
include/c++/13.1.0/array:243:9: note: built-in candidate operator[](int *, int)
        return _M_elems[0];
               ^
include/c++/13.1.0/array:243:9: note: built-in candidate operator[](const int
*, int)
include/c++/13.1.0/array:243:9: note: built-in candidate operator[](volatile
int *, int)
include/c++/13.1.0/array:243:9: note: built-in candidate operator[](const
volatile int *, int)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/110139] [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang)
  2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
@ 2023-06-06  9:18 ` joseph.faulls at imgtec dot com
  2023-06-06 10:26 ` [Bug libstdc++/110139] [13/14 Regression] " redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: joseph.faulls at imgtec dot com @ 2023-06-06  9:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

--- Comment #1 from Joseph Faulls <joseph.faulls at imgtec dot com> ---
Created attachment 55270
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55270&action=edit
Similar test case that causes a warning in gcc / error in clang

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/110139] [13/14 Regression] Ambiguous use of [] operator for 0-sized arrays (with clang)
  2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
  2023-06-06  9:18 ` [Bug libstdc++/110139] " joseph.faulls at imgtec dot com
@ 2023-06-06 10:26 ` redi at gcc dot gnu.org
  2023-06-06 10:30 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-06-06 10:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.2
             Status|UNCONFIRMED                 |NEW
      Known to work|                            |12.3.0
           Keywords|                            |rejects-valid
     Ever confirmed|0                           |1
      Known to fail|                            |13.1.0, 14.0
   Last reconfirmed|                            |2023-06-06
            Summary|[libstdc++] Ambiguous use   |[13/14 Regression]
                   |of [] operator for 0-sized  |Ambiguous use of []
                   |arrays (with clang)         |operator for 0-sized arrays
                   |                            |(with clang)

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This fixes it:

--- a/libstdc++-v3/include/std/array
+++ b/libstdc++-v3/include/std/array
@@ -69,7 +69,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION

        // Conversion to a pointer produces a null pointer.
        __attribute__((__always_inline__))
-       constexpr operator _Tp*() const noexcept { return nullptr; }
+       constexpr explicit operator _Tp*() const noexcept { return nullptr; }
      };

      using _Is_swappable = true_type;
@@ -274,12 +274,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
       _GLIBCXX17_CONSTEXPR pointer
       data() noexcept
-      { return _M_elems; }
+      { return static_cast<pointer>(_M_elems); }

       [[__nodiscard__]]
       _GLIBCXX17_CONSTEXPR const_pointer
       data() const noexcept
-      { return _M_elems; }
+      { return static_cast<const_pointer>(_M_elems); }
     };

 #if __cpp_deduction_guides >= 201606



Alternatively, so does this:

--- a/libstdc++-v3/include/std/array
+++ b/libstdc++-v3/include/std/array
@@ -240,7 +240,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       front() noexcept
       {
        __glibcxx_requires_nonempty();
-       return _M_elems[0];
+       return _M_elems[0u];
       }

       [[__nodiscard__]]
@@ -250,7 +250,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #if __cplusplus >= 201402L
        __glibcxx_requires_nonempty();
 #endif
-       return _M_elems[0];
+       return _M_elems[0u];
       }

       [[__nodiscard__]]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/110139] [13/14 Regression] Ambiguous use of [] operator for 0-sized arrays (with clang)
  2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
  2023-06-06  9:18 ` [Bug libstdc++/110139] " joseph.faulls at imgtec dot com
  2023-06-06 10:26 ` [Bug libstdc++/110139] [13/14 Regression] " redi at gcc dot gnu.org
@ 2023-06-06 10:30 ` redi at gcc dot gnu.org
  2023-06-06 10:44 ` joseph.faulls at imgtec dot com
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-06-06 10:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
GCC gives an error for the original testcase with -pedantic

In file included from arr.cc:1:
/home/jwakely/gcc/13.1.0/include/c++/13.1.0/array: In instantiation of
'constexpr std::array<_Tp, _Nm>::value_type& std::array<_Tp, _Nm>::front()
[with _Tp = int; unsigned int _Nm = 0; reference = int&]':
arr.cc:2:21:   required from here
/home/jwakely/gcc/13.1.0/include/c++/13.1.0/array:243:24: error: ambiguous
overload for 'operator[]' (operand types are 'std::__array_traits<int,
0>::_Type' and 'int')
  243 |         return _M_elems[0];
      |                ~~~~~~~~^
/home/jwakely/gcc/13.1.0/include/c++/13.1.0/array:243:24: note: candidate:
'operator[](int*, int)' (built-in)
/home/jwakely/gcc/13.1.0/include/c++/13.1.0/array:68:13: note: candidate: '_Tp&
std::__array_traits<_Tp, 0>::_Type::operator[](std::size_t) const [with _Tp =
int; std::size_t = unsigned int]'
   68 |        _Tp& operator[](size_t) const noexcept { __builtin_trap(); }
      |             ^~~~~~~~
/home/jwakely/gcc/13.1.0/include/c++/13.1.0/array: In instantiation of
'constexpr const std::array<_Tp, _Nm>::value_type& std::array<_Tp,
_Nm>::front() const [with _Tp = int; unsigned int _Nm = 0; const_reference =
const int&]':
arr.cc:2:21:   required from here
/home/jwakely/gcc/13.1.0/include/c++/13.1.0/array:253:24: error: ambiguous
overload for 'operator[]' (operand types are 'const std::__array_traits<int,
0>::_Type' and 'int')
  253 |         return _M_elems[0];
      |                ~~~~~~~~^
/home/jwakely/gcc/13.1.0/include/c++/13.1.0/array:253:24: note: candidate:
'operator[](int*, int)' (built-in)
/home/jwakely/gcc/13.1.0/include/c++/13.1.0/array:68:13: note: candidate: '_Tp&
std::__array_traits<_Tp, 0>::_Type::operator[](std::size_t) const [with _Tp =
int; std::size_t = unsigned int]'
   68 |        _Tp& operator[](size_t) const noexcept { __builtin_trap(); }
      |             ^~~~~~~~

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/110139] [13/14 Regression] Ambiguous use of [] operator for 0-sized arrays (with clang)
  2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
                   ` (2 preceding siblings ...)
  2023-06-06 10:30 ` redi at gcc dot gnu.org
@ 2023-06-06 10:44 ` joseph.faulls at imgtec dot com
  2023-06-06 11:02 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: joseph.faulls at imgtec dot com @ 2023-06-06 10:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

--- Comment #4 from Joseph Faulls <joseph.faulls at imgtec dot com> ---
Amazing, thanks for the swift response and fix! Ah yes, I didn't try -pedantic.

Do we want a patch for this? I can submit if you like, but it'd likely be you
who accepts it :)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/110139] [13/14 Regression] Ambiguous use of [] operator for 0-sized arrays (with clang)
  2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
                   ` (3 preceding siblings ...)
  2023-06-06 10:44 ` joseph.faulls at imgtec dot com
@ 2023-06-06 11:02 ` redi at gcc dot gnu.org
  2023-06-06 11:02 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-06-06 11:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
No need, thanks. I'm already running tests with a patched tree, I'll push it
today (and backport to gcc-13).

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/110139] [13/14 Regression] Ambiguous use of [] operator for 0-sized arrays (with clang)
  2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
                   ` (4 preceding siblings ...)
  2023-06-06 11:02 ` redi at gcc dot gnu.org
@ 2023-06-06 11:02 ` redi at gcc dot gnu.org
  2023-06-06 11:45 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-06-06 11:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
So let's make it ASSIGNED instead of NEW.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/110139] [13/14 Regression] Ambiguous use of [] operator for 0-sized arrays (with clang)
  2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
                   ` (5 preceding siblings ...)
  2023-06-06 11:02 ` redi at gcc dot gnu.org
@ 2023-06-06 11:45 ` cvs-commit at gcc dot gnu.org
  2023-06-06 13:34 ` cvs-commit at gcc dot gnu.org
  2023-06-06 13:34 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-06-06 11:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:56001fad4ecc32396beead6644906e3846244b67

commit r14-1573-g56001fad4ecc32396beead6644906e3846244b67
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jun 6 11:38:42 2023 +0100

    libstdc++: Fix ambiguous expression in std::array<T, 0>::front() [PR110139]

    For 32-bit targets using -pedantic (or using Clang) makes the expression
    _M_elems[0] ambiguous.  The overloaded operator[] that we want to call
    has a size_t parameter, but 0 is type ptrdiff_t for many ILP32 targets,
    so using the implicit conversion from _M_elems to T* and then
    subscripting that is also viable.

    Change the 0 to (size_type)0 and also make the conversion to T*
    explicit, so that's it's not viable here. The latter change requires a
    static_cast in data() where we really do want to convert _M_elems to a
    pointer.

    libstdc++-v3/ChangeLog:

            PR libstdc++/110139
            * include/std/array (__array_traits<T, 0>::operator T*()): Make
            conversion operator explicit.
            (array::front): Use size_type as subscript operand.
            (array::data): Use static_cast to make conversion explicit.
            * testsuite/23_containers/array/element_access/110139.cc: New
            test.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/110139] [13/14 Regression] Ambiguous use of [] operator for 0-sized arrays (with clang)
  2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
                   ` (6 preceding siblings ...)
  2023-06-06 11:45 ` cvs-commit at gcc dot gnu.org
@ 2023-06-06 13:34 ` cvs-commit at gcc dot gnu.org
  2023-06-06 13:34 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-06-06 13:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:32f2b0f32816af816605dbe8060fb903cf7d5603

commit r13-7418-g32f2b0f32816af816605dbe8060fb903cf7d5603
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jun 6 11:38:42 2023 +0100

    libstdc++: Fix ambiguous expression in std::array<T, 0>::front() [PR110139]

    For 32-bit targets using -pedantic (or using Clang) makes the expression
    _M_elems[0] ambiguous.  The overloaded operator[] that we want to call
    has a size_t parameter, but 0 is type ptrdiff_t for many ILP32 targets,
    so using the implicit conversion from _M_elems to T* and then
    subscripting that is also viable.

    Change the 0 to (size_type)0 and also make the conversion to T*
    explicit, so that's it's not viable here. The latter change requires a
    static_cast in data() where we really do want to convert _M_elems to a
    pointer.

    libstdc++-v3/ChangeLog:

            PR libstdc++/110139
            * include/std/array (__array_traits<T, 0>::operator T*()): Make
            conversion operator explicit.
            (array::front): Use size_type as subscript operand.
            (array::data): Use static_cast to make conversion explicit.
            * testsuite/23_containers/array/element_access/110139.cc: New
            test.

    (cherry picked from commit 56001fad4ecc32396beead6644906e3846244b67)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/110139] [13/14 Regression] Ambiguous use of [] operator for 0-sized arrays (with clang)
  2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
                   ` (7 preceding siblings ...)
  2023-06-06 13:34 ` cvs-commit at gcc dot gnu.org
@ 2023-06-06 13:34 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-06-06 13:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110139

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 13.2, thanks for the report.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-06-06 13:34 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-06  9:18 [Bug libstdc++/110139] New: [libstdc++] Ambiguous use of [] operator for 0-sized arrays (with clang) joseph.faulls at imgtec dot com
2023-06-06  9:18 ` [Bug libstdc++/110139] " joseph.faulls at imgtec dot com
2023-06-06 10:26 ` [Bug libstdc++/110139] [13/14 Regression] " redi at gcc dot gnu.org
2023-06-06 10:30 ` redi at gcc dot gnu.org
2023-06-06 10:44 ` joseph.faulls at imgtec dot com
2023-06-06 11:02 ` redi at gcc dot gnu.org
2023-06-06 11:02 ` redi at gcc dot gnu.org
2023-06-06 11:45 ` cvs-commit at gcc dot gnu.org
2023-06-06 13:34 ` cvs-commit at gcc dot gnu.org
2023-06-06 13:34 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).