public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/107735] New: Inconsistent error messages for std::array out of bound
@ 2022-11-17 15:00 andrzej at rpi dot pl
  2022-11-17 16:31 ` [Bug c++/107735] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: andrzej at rpi dot pl @ 2022-11-17 15:00 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107735
           Summary: Inconsistent error messages for std::array out of
                    bound
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andrzej at rpi dot pl
  Target Milestone: ---

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

The second and third lines of following code produce different `out of bound`
compile errors, one pointing to the original line, the other pointing to a line
inside standard library that is trying to access internal (non-std::) array  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
constexpr std::array<int, 3> array = {1, 2, 3};
constexpr int v1 = array[3];
constexpr int v2 = array[4];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compilation line:
`g++-12 a-minimal_example.ii`

Compiler output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
minimal_example.cpp:6:27: error: array subscript value ‘3’ is outside the
bounds of array type ‘std::__array_traits<int, 3>::_Type’ {aka ‘const int [3]’}
    6 | constexpr int v1 = array[3];
      |                           ^
In file included from minimal_example.cpp:1:
minimal_example.cpp:9:27:   in ‘constexpr’ expansion of ‘array.std::array<int,
3>::operator[](4)’
/usr/include/c++/12/array:219:25:   in ‘constexpr’ expansion of
‘std::__array_traits<int, 3>::_S_ref(((const std::array<int,
3>*)this)->std::array<int, 3>::_M_elems, __n)’
/usr/include/c++/12/array:61:36: error: array subscript value ‘4’ is outside
the bounds of array type ‘std::__array_traits<int, 3>::_Type’ {aka ‘const int
[3]’}
   61 |       { return const_cast<_Tp&>(__t[__n]); }
      |                                 ~~~^
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

GCC version:
gcc-12 (Ubuntu 12.1.0-2ubuntu1~22.04) 12.1.0

System version:
Linux 5.15.0-52-generic #58-Ubuntu SMP x86_64

GCC build options:
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
12.1.0-2ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-12
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-12-sZcx2y/gcc-12-12.1.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-sZcx2y/gcc-12-12.1.0/debian/tmp-gcn/usr
--enable-offload-defaulted --without-cuda-driver --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu

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

* [Bug c++/107735] Inconsistent error messages for std::array out of bound
  2022-11-17 15:00 [Bug libstdc++/107735] New: Inconsistent error messages for std::array out of bound andrzej at rpi dot pl
@ 2022-11-17 16:31 ` redi at gcc dot gnu.org
  2022-11-17 22:24 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-17 16:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-11-17
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
          Component|libstdc++                   |c++

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Libstdc++ doesn't print those errors, this is component=c++

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

* [Bug c++/107735] Inconsistent error messages for std::array out of bound
  2022-11-17 15:00 [Bug libstdc++/107735] New: Inconsistent error messages for std::array out of bound andrzej at rpi dot pl
  2022-11-17 16:31 ` [Bug c++/107735] " redi at gcc dot gnu.org
@ 2022-11-17 22:24 ` pinskia at gcc dot gnu.org
  2022-11-18  0:46 ` [Bug c++/107735] Inconsistent error messages for std::array out of bound due to taking the address of one-past-the-end is valid redi at gcc dot gnu.org
  2022-11-18  0:49 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-17 22:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I wonder if this is because doing 
constexpr const int *v1 = &array[3];

is valid and well defined.

Even clang gives two different error messages:
<source>:3:21: error: constexpr variable 'v1' must be initialized by a constant
expression
constexpr const int v1 = array[3];
                    ^    ~~~~~~~~
<source>:3:26: note: read of dereferenced one-past-the-end pointer is not
allowed in a constant expression
constexpr const int v1 = array[3];
                         ^
<source>:4:15: error: constexpr variable 'v2' must be initialized by a constant
expression
constexpr int v2 = array[4];
              ^    ~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/13.0.0/../../../../include/c++/13.0.0/array:213:9:
note: cannot refer to element 4 of array of 3 elements in a constant expression
        return _M_elems[__n];
               ^
<source>:4:20: note: in call to '&array->operator[](4)'
constexpr int v2 = array[4];
                   ^
---- CUT ----
I do think clang's note of one-past-the-end gives more of a hint of why the
error message is different though.

And I do think GCC could add that note.

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

* [Bug c++/107735] Inconsistent error messages for std::array out of bound due to taking the address of one-past-the-end is valid
  2022-11-17 15:00 [Bug libstdc++/107735] New: Inconsistent error messages for std::array out of bound andrzej at rpi dot pl
  2022-11-17 16:31 ` [Bug c++/107735] " redi at gcc dot gnu.org
  2022-11-17 22:24 ` pinskia at gcc dot gnu.org
@ 2022-11-18  0:46 ` redi at gcc dot gnu.org
  2022-11-18  0:49 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-18  0:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> I wonder if this is because doing 
> constexpr const int *v1 = &array[3];
> 
> is valid and well defined.

It's not, but &array.data()[3] is.

I agree that's probably the reason for the different diagnostics.

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

* [Bug c++/107735] Inconsistent error messages for std::array out of bound due to taking the address of one-past-the-end is valid
  2022-11-17 15:00 [Bug libstdc++/107735] New: Inconsistent error messages for std::array out of bound andrzej at rpi dot pl
                   ` (2 preceding siblings ...)
  2022-11-18  0:46 ` [Bug c++/107735] Inconsistent error messages for std::array out of bound due to taking the address of one-past-the-end is valid redi at gcc dot gnu.org
@ 2022-11-18  0:49 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-18  0:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #3)
> (In reply to Andrew Pinski from comment #2)
> > I wonder if this is because doing 
> > constexpr const int *v1 = &array[3];
> > 
> > is valid and well defined.
> 
> It's not, but &array.data()[3] is.
> 
> I agree that's probably the reason for the different diagnostics.

Interesting because both GCC and clang accept "constexpr const int *v1 =
&array[3];" though.

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

end of thread, other threads:[~2022-11-18  0:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-17 15:00 [Bug libstdc++/107735] New: Inconsistent error messages for std::array out of bound andrzej at rpi dot pl
2022-11-17 16:31 ` [Bug c++/107735] " redi at gcc dot gnu.org
2022-11-17 22:24 ` pinskia at gcc dot gnu.org
2022-11-18  0:46 ` [Bug c++/107735] Inconsistent error messages for std::array out of bound due to taking the address of one-past-the-end is valid redi at gcc dot gnu.org
2022-11-18  0:49 ` pinskia 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).