public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110401] New: Unhelpful "goto is not a constant expression" in ill-formed constexpr function template
@ 2023-06-25  8:56 xry111 at gcc dot gnu.org
  2023-06-25 13:15 ` [Bug c++/110401] [10/11/12/13/14 Regression] Unhelpful "goto is not a constant expression" in ill-formed pre c++20 " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-06-25  8:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110401
           Summary: Unhelpful "goto is not a constant expression" in
                    ill-formed constexpr function template
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xry111 at gcc dot gnu.org
  Target Milestone: ---

#include <array>

template <int N>
constexpr std::array<int, N> get_sqr()
{
    std::array<int, N> ret;
    for (int i = 0; i < N; i++)
        ret[i] = i * i;
    return ret;
}

constexpr auto sqr = get_sqr<10000>();

GCC says "error: 'goto' is not a constant expression".  This is puzzling
because there is no "goto" in the function.

Note that if it's not a template, GCC outputs the correct message:

#include <array>

constexpr std::array<int, 10000> get_sqr()
{
    std::array<int, 10000> ret;
    for (int i = 0; i < 10000; i++)
        ret[i] = i * i;
    return ret;
}

constexpr auto sqr = get_sqr();

t.cc: In function 'constexpr std::array<int, 10000> get_sqr()':
t.cc:5:28: error: uninitialized variable 'ret' in 'constexpr' function
    5 |     std::array<int, 10000> ret;
      |                            ^~~

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

* [Bug c++/110401] [10/11/12/13/14 Regression] Unhelpful "goto is not a constant expression" in ill-formed pre c++20 constexpr function template
  2023-06-25  8:56 [Bug c++/110401] New: Unhelpful "goto is not a constant expression" in ill-formed constexpr function template xry111 at gcc dot gnu.org
@ 2023-06-25 13:15 ` pinskia at gcc dot gnu.org
  2023-07-07 10:45 ` [Bug c++/110401] [11/12/13/14 " rguenth at gcc dot gnu.org
  2024-03-22 13:43 ` law at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-25 13:15 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Unhelpful "goto is not a    |[10/11/12/13/14 Regression]
                   |constant expression" in     |Unhelpful "goto is not a
                   |ill-formed pre c++20        |constant expression" in
                   |constexpr function template |ill-formed pre c++20
                   |                            |constexpr function template
   Last reconfirmed|                            |2023-06-25
   Target Milestone|---                         |10.5
             Status|UNCONFIRMED                 |NEW
      Known to fail|                            |7.3.0
      Known to work|                            |7.1.0, 7.2.0
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Compiling it with -std=c++11 is even worse as it does not show the reason:
```
<source>:12:36: error: 'constexpr std::array<int, N> get_sqr() [with int N =
10000]' called in a constant expression
   12 | constexpr auto sqr = get_sqr<10000>();
      |                      ~~~~~~~~~~~~~~^~
<source>:4:30: note: 'constexpr std::array<int, N> get_sqr() [with int N =
10000]' is not usable as a 'constexpr' function because:
    4 | constexpr std::array<int, N> get_sqr()
      |                              ^~~~~~~
```

GCC 7.1 and 7.2 used to have the correct explanation for C++11+:
```
<source>: In instantiation of 'constexpr std::array<int, N> get_sqr() [with int
N = 10000]':
<source>:12:37:   required from here
<source>:6:24: error: uninitialized variable 'ret' in 'constexpr' function
     std::array<int, N> ret;
                        ^~~
In file included from <source>:1:0:
/opt/compiler-explorer/gcc-7.2.0/include/c++/7.2.0/array:94:12: note: 'struct
std::array<int, 10000>' has no user-provided default constructor
     struct array
            ^~~~~
/opt/compiler-explorer/gcc-7.2.0/include/c++/7.2.0/array:110:56: note: and the
implicitly-defined constructor does not initialize 'int std::array<int,
10000>::_M_elems [10000]'
       typename _AT_Type::_Type                         _M_elems;
                                                        ^~~~~~~~
<source>:12:37: error: 'constexpr std::array<int, N> get_sqr() [with int N =
10000]' called in a constant expression
 constexpr auto sqr = get_sqr<10000>();
                                     ^
<source>:4:30: note: 'constexpr std::array<int, N> get_sqr() [with int N =
10000]' is not usable as a constexpr function because:
 constexpr std::array<int, N> get_sqr()
                              ^~~~~~~

```


GCC 7.3+ removed the "uninitialized variable 'ret' in 'constexpr' function".

GCC 10+ then removed the 2 notes at the beginning.


Note this is actually valid C++20 so maybe the change in GCC 10 is related to
that ...

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

* [Bug c++/110401] [11/12/13/14 Regression] Unhelpful "goto is not a constant expression" in ill-formed pre c++20 constexpr function template
  2023-06-25  8:56 [Bug c++/110401] New: Unhelpful "goto is not a constant expression" in ill-formed constexpr function template xry111 at gcc dot gnu.org
  2023-06-25 13:15 ` [Bug c++/110401] [10/11/12/13/14 Regression] Unhelpful "goto is not a constant expression" in ill-formed pre c++20 " pinskia at gcc dot gnu.org
@ 2023-07-07 10:45 ` rguenth at gcc dot gnu.org
  2024-03-22 13:43 ` law at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-07 10:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.5                        |11.5

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 10 branch is being closed.

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

* [Bug c++/110401] [11/12/13/14 Regression] Unhelpful "goto is not a constant expression" in ill-formed pre c++20 constexpr function template
  2023-06-25  8:56 [Bug c++/110401] New: Unhelpful "goto is not a constant expression" in ill-formed constexpr function template xry111 at gcc dot gnu.org
  2023-06-25 13:15 ` [Bug c++/110401] [10/11/12/13/14 Regression] Unhelpful "goto is not a constant expression" in ill-formed pre c++20 " pinskia at gcc dot gnu.org
  2023-07-07 10:45 ` [Bug c++/110401] [11/12/13/14 " rguenth at gcc dot gnu.org
@ 2024-03-22 13:43 ` law at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: law at gcc dot gnu.org @ 2024-03-22 13:43 UTC (permalink / raw)
  To: gcc-bugs

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

Jeffrey A. Law <law at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |law at gcc dot gnu.org
           Priority|P3                          |P2

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

end of thread, other threads:[~2024-03-22 13:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-25  8:56 [Bug c++/110401] New: Unhelpful "goto is not a constant expression" in ill-formed constexpr function template xry111 at gcc dot gnu.org
2023-06-25 13:15 ` [Bug c++/110401] [10/11/12/13/14 Regression] Unhelpful "goto is not a constant expression" in ill-formed pre c++20 " pinskia at gcc dot gnu.org
2023-07-07 10:45 ` [Bug c++/110401] [11/12/13/14 " rguenth at gcc dot gnu.org
2024-03-22 13:43 ` law 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).