public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access
@ 2023-06-09 18:29 matt at godbolt dot org
  2023-06-09 18:37 ` [Bug c++/110197] [13/14 Regression] " mpolacek at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: matt at godbolt dot org @ 2023-06-09 18:29 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110197
           Summary: Empty constexpr object constructor erronously claims
                    out of range access
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matt at godbolt dot org
  Target Milestone: ---

The following code, compiled on GCC 13.1 complains of a non-constexpr safe
array access:

```
#include <array>
#include <string_view>

struct Currency {
    // commenting-in the `i` below fixes, as does passing a `const char *` to
the ctor
    // int i{};
    constexpr explicit Currency(std::string_view name) {}
    // constexpr explicit Currency(const char *name) {}
};

static Currency get_c() {
    static constexpr std::array c{Currency{"BWP"}};
    return c[0];
}
```

The error is:

```
<source>: In function 'Currency get_c()':
<source>:12:50:   in 'constexpr' expansion of
'Currency(std::basic_string_view<char>(((const char*)"BWP")))'
<source>:12:50: error: accessing uninitialized array element
   12 |     static constexpr std::array c{Currency{"BWP"}};
      |                                                  
```

The same code compiles without issue on GCC 12.1, and on clang. Passing either
a const char * to the constructor, or adding a single field to the class makes
it compile.

https://godbolt.org/z/G355frEM4

Was also reproducible on trunk as of 14da764809d3bffe9336e72999312ba3fb019dc6

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

* [Bug c++/110197] [13/14 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
@ 2023-06-09 18:37 ` mpolacek at gcc dot gnu.org
  2023-06-09 18:40 ` mpolacek at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-06-09 18:37 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-06-09
     Ever confirmed|0                           |1
   Target Milestone|---                         |13.2
           Keywords|                            |rejects-valid
                 CC|                            |mpolacek at gcc dot gnu.org
            Summary|Empty constexpr object      |[13/14 Regression] Empty
                   |constructor erronously      |constexpr object
                   |claims out of range access  |constructor erronously
                   |                            |claims out of range access

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

* [Bug c++/110197] [13/14 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
  2023-06-09 18:37 ` [Bug c++/110197] [13/14 Regression] " mpolacek at gcc dot gnu.org
@ 2023-06-09 18:40 ` mpolacek at gcc dot gnu.org
  2023-07-18 18:15 ` mpolacek at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-06-09 18:40 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Started with r13-963:

commit d19b4342c19e5a7fd84888aa06ebc106438d0c46
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jun 1 17:30:25 2022 -0400

    c++: more constexpr empty base [PR105795]

    Following on from the previous patch, for trunk let's consistently set
    ctx->ctor to NULL_TREE for empty subobjects.

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

* [Bug c++/110197] [13/14 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
  2023-06-09 18:37 ` [Bug c++/110197] [13/14 Regression] " mpolacek at gcc dot gnu.org
  2023-06-09 18:40 ` mpolacek at gcc dot gnu.org
@ 2023-07-18 18:15 ` mpolacek at gcc dot gnu.org
  2023-07-19 15:14 ` ppalka at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-07-18 18:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
// PR c++/110197

namespace std {
constexpr bool __is_constant_evaluated() {
  return __builtin_is_constant_evaluated();
}
template <bool, typename _Tp> using enable_if_t = _Tp;
template <typename _Tp, int _Nm> struct __array_traits {
  typedef _Tp _Type[_Nm];
};
template <typename _Tp, int _Nm> struct array {
  typename __array_traits<_Tp, _Nm>::_Type _M_elems;
};
template <typename _Tp> array(_Tp) -> array<enable_if_t<0, _Tp>, 1>;
struct char_traits {
  static constexpr unsigned length() {
    __is_constant_evaluated();
    return 0;
  }
};
struct basic_string_view {
  using traits_type = char_traits;
  constexpr basic_string_view(const char *)
      : _M_len{traits_type::length()}, _M_str{} {}
  long _M_len;
  char _M_str;
};
} // namespace std
struct Currency {
  constexpr Currency(std::basic_string_view) {}
};
void get_c() { constexpr std::array c{Currency{""}}; }

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

* [Bug c++/110197] [13/14 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
                   ` (2 preceding siblings ...)
  2023-07-18 18:15 ` mpolacek at gcc dot gnu.org
@ 2023-07-19 15:14 ` ppalka at gcc dot gnu.org
  2023-07-20 16:51 ` ppalka at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-07-19 15:14 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #3 from Patrick Palka <ppalka at gcc dot gnu.org> ---
More reduced:

struct Currency {
  constexpr Currency(int) { }
};
constexpr int f() { return __builtin_is_constant_evaluated(); };
constexpr Currency _M_elems[1] = {Currency{f()}};

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

* [Bug c++/110197] [13/14 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
                   ` (3 preceding siblings ...)
  2023-07-19 15:14 ` ppalka at gcc dot gnu.org
@ 2023-07-20 16:51 ` ppalka at gcc dot gnu.org
  2023-07-27  9:26 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-07-20 16:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Looks like this issue isn't specific to constexpr array construction:

struct Currency {
  constexpr Currency(int) { }
};
constexpr int f() { return __builtin_is_constant_evaluated(); };
struct A {
  Currency _M_elems;
};
constexpr A _M_elems = {Currency{f()}};

Since r13-963 we reject this with

<stdin>:8:38: error: ‘A{(void)0}’ is not a constant expression

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

* [Bug c++/110197] [13/14 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
                   ` (4 preceding siblings ...)
  2023-07-20 16:51 ` ppalka at gcc dot gnu.org
@ 2023-07-27  9:26 ` rguenth at gcc dot gnu.org
  2023-07-27 13:10 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-27  9:26 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|13.2                        |13.3

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 13.2 is being released, retargeting bugs to GCC 13.3.

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

* [Bug c++/110197] [13/14 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
                   ` (5 preceding siblings ...)
  2023-07-27  9:26 ` rguenth at gcc dot gnu.org
@ 2023-07-27 13:10 ` cvs-commit at gcc dot gnu.org
  2023-07-27 13:11 ` [Bug c++/110197] [13 " ppalka at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-07-27 13:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:a426b91b27e28985f47d16827a532fbc28c09bd7

commit r14-2820-ga426b91b27e28985f47d16827a532fbc28c09bd7
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Jul 27 09:10:07 2023 -0400

    c++: constexpr empty subobject elision [PR110197]

    Now that init_subob_ctx no longer sets new_ctx.ctor for a subobject of
    empty type, it seems we need to ensure its callers also consistently
    omit entries in the parent ctx->ctor for such subobjects.  We also need
    to allow cxx_eval_array_reference to synthesize an empty subobject even
    if the array CONSTRUCTOR has CONSTRUCTOR_NO_CLEARING set.

            PR c++/110197

    gcc/cp/ChangeLog:

            * constexpr.cc (cxx_eval_array_reference): Allow synthesizing an
            empty subobject even if CONSTRUCTOR_NO_CLEARING is set.
            (cxx_eval_bare_aggregate): Set 'no_slot' to true more generally
            whenever new_ctx.ctor is set to NULL_TREE by init_subob_ctx,
            i.e. whenever initializing an subobject of empty type.
            (cxx_eval_vec_init_1): Define 'no_slot' as above and use it
            accordingly.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp0x/constexpr-empty18.C: New test.
            * g++.dg/cpp0x/constexpr-empty19.C: New test.

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

* [Bug c++/110197] [13 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
                   ` (6 preceding siblings ...)
  2023-07-27 13:10 ` cvs-commit at gcc dot gnu.org
@ 2023-07-27 13:11 ` ppalka at gcc dot gnu.org
  2023-07-27 13:27 ` matt at godbolt dot org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-07-27 13:11 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |ppalka at gcc dot gnu.org
      Known to work|                            |14.0
            Summary|[13/14 Regression] Empty    |[13 Regression] Empty
                   |constexpr object            |constexpr object
                   |constructor erronously      |constructor erronously
                   |claims out of range access  |claims out of range access
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=105795
             Status|NEW                         |ASSIGNED

--- Comment #7 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed on trunk so far.

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

* [Bug c++/110197] [13 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
                   ` (7 preceding siblings ...)
  2023-07-27 13:11 ` [Bug c++/110197] [13 " ppalka at gcc dot gnu.org
@ 2023-07-27 13:27 ` matt at godbolt dot org
  2023-08-07 23:27 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: matt at godbolt dot org @ 2023-07-27 13:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Matt Godbolt <matt at godbolt dot org> ---
Fantastic: thanks everyone!

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

* [Bug c++/110197] [13 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
                   ` (8 preceding siblings ...)
  2023-07-27 13:27 ` matt at godbolt dot org
@ 2023-08-07 23:27 ` cvs-commit at gcc dot gnu.org
  2023-08-21 14:04 ` ppalka at gcc dot gnu.org
  2023-08-21 14:09 ` matt at godbolt dot org
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-08-07 23:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Patrick Palka
<ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:61d24d3f40638326b4a24baadeb25a88610d76d8

commit r13-7693-g61d24d3f40638326b4a24baadeb25a88610d76d8
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu Jul 27 09:10:07 2023 -0400

    c++: constexpr empty subobject elision [PR110197]

    Now that init_subob_ctx no longer sets new_ctx.ctor for a subobject of
    empty type, it seems we need to ensure its callers also consistently
    omit entries in the parent ctx->ctor for such subobjects.  We also need
    to allow cxx_eval_array_reference to synthesize an empty subobject even
    if the array CONSTRUCTOR has CONSTRUCTOR_NO_CLEARING set.

            PR c++/110197

    gcc/cp/ChangeLog:

            * constexpr.cc (cxx_eval_array_reference): Allow synthesizing an
            empty subobject even if CONSTRUCTOR_NO_CLEARING is set.
            (cxx_eval_bare_aggregate): Set 'no_slot' to true more generally
            whenever new_ctx.ctor is set to NULL_TREE by init_subob_ctx,
            i.e. whenever initializing an subobject of empty type.
            (cxx_eval_vec_init_1): Define 'no_slot' as above and use it
            accordingly.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp0x/constexpr-empty18.C: New test.
            * g++.dg/cpp0x/constexpr-empty19.C: New test.

    (cherry picked from commit a426b91b27e28985f47d16827a532fbc28c09bd7)

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

* [Bug c++/110197] [13 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
                   ` (9 preceding siblings ...)
  2023-08-07 23:27 ` cvs-commit at gcc dot gnu.org
@ 2023-08-21 14:04 ` ppalka at gcc dot gnu.org
  2023-08-21 14:09 ` matt at godbolt dot org
  11 siblings, 0 replies; 13+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-08-21 14:04 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

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

--- Comment #10 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 13.3

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

* [Bug c++/110197] [13 Regression] Empty constexpr object constructor erronously claims out of range access
  2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
                   ` (10 preceding siblings ...)
  2023-08-21 14:04 ` ppalka at gcc dot gnu.org
@ 2023-08-21 14:09 ` matt at godbolt dot org
  11 siblings, 0 replies; 13+ messages in thread
From: matt at godbolt dot org @ 2023-08-21 14:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Matt Godbolt <matt at godbolt dot org> ---
Thank you Patrick! Great news! About 1/3 of my build's output is this warning
right now :)

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

end of thread, other threads:[~2023-08-21 14:09 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-09 18:29 [Bug c++/110197] New: Empty constexpr object constructor erronously claims out of range access matt at godbolt dot org
2023-06-09 18:37 ` [Bug c++/110197] [13/14 Regression] " mpolacek at gcc dot gnu.org
2023-06-09 18:40 ` mpolacek at gcc dot gnu.org
2023-07-18 18:15 ` mpolacek at gcc dot gnu.org
2023-07-19 15:14 ` ppalka at gcc dot gnu.org
2023-07-20 16:51 ` ppalka at gcc dot gnu.org
2023-07-27  9:26 ` rguenth at gcc dot gnu.org
2023-07-27 13:10 ` cvs-commit at gcc dot gnu.org
2023-07-27 13:11 ` [Bug c++/110197] [13 " ppalka at gcc dot gnu.org
2023-07-27 13:27 ` matt at godbolt dot org
2023-08-07 23:27 ` cvs-commit at gcc dot gnu.org
2023-08-21 14:04 ` ppalka at gcc dot gnu.org
2023-08-21 14:09 ` matt at godbolt dot 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).