public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99700] New: gcc takes an uninitialized variable as a constant expression
@ 2021-03-21 18:22 hewillk at gmail dot com
  2021-04-15  0:14 ` [Bug c++/99700] uninitialized variable accepted as a constant expression in C++ 20 msebor at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: hewillk at gmail dot com @ 2021-03-21 18:22 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99700
           Summary: gcc takes an uninitialized variable as a constant
                    expression
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hewillk at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/93qM1b

====

struct A {
  constexpr A() { c[0] = 0; }
  char c[2];
};

constexpr A a;

====

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

* [Bug c++/99700] uninitialized variable accepted as a constant expression in C++ 20
  2021-03-21 18:22 [Bug c++/99700] New: gcc takes an uninitialized variable as a constant expression hewillk at gmail dot com
@ 2021-04-15  0:14 ` msebor at gcc dot gnu.org
  2021-04-15  0:15 ` [Bug c++/99700] [10/11 Regression] " msebor at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-15  0:14 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
            Summary|gcc takes an uninitialized  |uninitialized variable
                   |variable as a constant      |accepted as a constant
                   |expression                  |expression in C++ 20
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-04-15

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
Confirmed with -std=c++20.  In earlier modes GCC rejects the test case as
expected.

$ (set -x && gcc -O2 -S -Wall -std=c++20 pr99700.C && gcc -O2 -S -Wall
-std=c++17 pr99700.C)
+ gcc -O2 -S -Wall -std=c++20 pr99700.C
+ gcc -O2 -S -Wall -std=c++17 pr99700.C
pr99700.C: In constructor ‘constexpr A::A()’:
pr99700.C:2:29: error: member ‘A::c’ must be initialized by mem-initializer in
‘constexpr’ constructor
    2 |   constexpr A() { c[0] = 0; }
      |                             ^
pr99700.C:3:8: note: declared here
    3 |   char c[2];
      |        ^
pr99700.C: At global scope:
pr99700.C:6:13: error: ‘constexpr A::A()’ called in a constant expression
    6 | constexpr A a;
      |             ^
pr99700.C:2:13: note: ‘constexpr A::A()’ declared here
    2 |   constexpr A() { c[0] = 0; }
      |             ^

When reporting bugs please include all the information we ask for here:
https://gcc.gnu.org/bugs/#need.  Links to other sites are not a substitute.

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

* [Bug c++/99700] [10/11 Regression] uninitialized variable accepted as a constant expression in C++ 20
  2021-03-21 18:22 [Bug c++/99700] New: gcc takes an uninitialized variable as a constant expression hewillk at gmail dot com
  2021-04-15  0:14 ` [Bug c++/99700] uninitialized variable accepted as a constant expression in C++ 20 msebor at gcc dot gnu.org
@ 2021-04-15  0:15 ` msebor at gcc dot gnu.org
  2021-04-15 16:55 ` ppalka at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-15  0:15 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|uninitialized variable      |[10/11 Regression]
                   |accepted as a constant      |uninitialized variable
                   |expression in C++ 20        |accepted as a constant
                   |                            |expression in C++ 20
      Known to fail|                            |10.2.0, 11.0

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
Bisection points to r279019 AKA the fix for PR c++/91353 as the first revision
when the code started to be accepted.

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

* [Bug c++/99700] [10/11 Regression] uninitialized variable accepted as a constant expression in C++ 20
  2021-03-21 18:22 [Bug c++/99700] New: gcc takes an uninitialized variable as a constant expression hewillk at gmail dot com
  2021-04-15  0:14 ` [Bug c++/99700] uninitialized variable accepted as a constant expression in C++ 20 msebor at gcc dot gnu.org
  2021-04-15  0:15 ` [Bug c++/99700] [10/11 Regression] " msebor at gcc dot gnu.org
@ 2021-04-15 16:55 ` ppalka at gcc dot gnu.org
  2021-04-16 13:25 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-04-15 16:55 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Patrick Palka <ppalka at gcc dot gnu.org> ---
It seems reduced_constant_expression_p needs to check that a
CONSTRUCTOR_NO_CLEARING array ctor initializes all elements.

I'm testing:

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index c8d9dae36fb..6e530f9c88d 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -46,6 +46,7 @@ do {                                                         
        \

 static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
                                          bool insert = false);
+static int array_index_cmp (tree key, tree index);

 /* Returns true iff FUN is an instantiation of a constexpr function
    template or a defaulted constexpr function.  */
@@ -2912,7 +2913,25 @@ reduced_constant_expression_p (tree t)
          else if (cxx_dialect >= cxx20
                   /* An ARRAY_TYPE doesn't have any TYPE_FIELDS.  */
                   && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
-           field = NULL_TREE;
+           {
+             tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
+             tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
+             if (find_array_ctor_elt (t, min) == -1
+                 || find_array_ctor_elt (t, max) == -1)
+               return false;
+             tree cursor = size_zero_node;
+             FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val)
+               {
+                 if (!reduced_constant_expression_p (val))
+                   return false;
+                 if (array_index_cmp (cursor, idx) != 0)
+                   return false;
+                 if (TREE_CODE (idx) == RANGE_EXPR)
+                   cursor = TREE_OPERAND (idx, 1);
+                 cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node);
+               }
+             return true;
+           }
          else if (cxx_dialect >= cxx20
                   && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
            {

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

* [Bug c++/99700] [10/11 Regression] uninitialized variable accepted as a constant expression in C++ 20
  2021-03-21 18:22 [Bug c++/99700] New: gcc takes an uninitialized variable as a constant expression hewillk at gmail dot com
                   ` (2 preceding siblings ...)
  2021-04-15 16:55 ` ppalka at gcc dot gnu.org
@ 2021-04-16 13:25 ` cvs-commit at gcc dot gnu.org
  2021-04-16 13:27 ` [Bug c++/99700] [10 " ppalka at gcc dot gnu.org
  2022-05-11 17:07 ` ppalka at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-04-16 13:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 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:baf05d54dc919c968d12de9d049e36e5bac10dec

commit r11-8216-gbaf05d54dc919c968d12de9d049e36e5bac10dec
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Apr 16 09:24:46 2021 -0400

    c++: partially initialized constexpr array [PR99700]

    Here, reduced_constant_expression_p is incorrectly returning true for a
    partially initialized array CONSTRUCTOR (in C++20 mode) because when the
    CONSTRUCTOR_NO_CLEARING flag is set, the predicate doesn't check that
    the CONSTRUCTOR spans the entire array like it does for class CONSTRUCTORS.
    This patch adds a dedicated loop for the array case that simultaneously
    verifies the CONSTRUCTOR spans the entire array and is made up of valid
    constant expressions.

    gcc/cp/ChangeLog:

            PR c++/99700
            * constexpr.c (reduced_constant_expression_p): For array
            CONSTRUCTORs, use a dedicated loop that additionally verifies
            the CONSTRUCTOR spans the entire array.

    gcc/testsuite/ChangeLog:

            PR c++/99700
            * g++.dg/cpp2a/constexpr-init21.C: New test.

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

* [Bug c++/99700] [10 Regression] uninitialized variable accepted as a constant expression in C++ 20
  2021-03-21 18:22 [Bug c++/99700] New: gcc takes an uninitialized variable as a constant expression hewillk at gmail dot com
                   ` (3 preceding siblings ...)
  2021-04-16 13:25 ` cvs-commit at gcc dot gnu.org
@ 2021-04-16 13:27 ` ppalka at gcc dot gnu.org
  2022-05-11 17:07 ` ppalka at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-04-16 13:27 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[10/11 Regression]          |[10 Regression]
                   |uninitialized variable      |uninitialized variable
                   |accepted as a constant      |accepted as a constant
                   |expression in C++ 20        |expression in C++ 20
   Target Milestone|---                         |10.4

--- Comment #5 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 11 so far.

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

* [Bug c++/99700] [10 Regression] uninitialized variable accepted as a constant expression in C++ 20
  2021-03-21 18:22 [Bug c++/99700] New: gcc takes an uninitialized variable as a constant expression hewillk at gmail dot com
                   ` (4 preceding siblings ...)
  2021-04-16 13:27 ` [Bug c++/99700] [10 " ppalka at gcc dot gnu.org
@ 2022-05-11 17:07 ` ppalka at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-05-11 17:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.4                        |11.0
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
The patch doesn't apply cleanly to the 10 branch, and since this is only a
C++20 regression, it doesn't seem worth backporting.

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

end of thread, other threads:[~2022-05-11 17:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-21 18:22 [Bug c++/99700] New: gcc takes an uninitialized variable as a constant expression hewillk at gmail dot com
2021-04-15  0:14 ` [Bug c++/99700] uninitialized variable accepted as a constant expression in C++ 20 msebor at gcc dot gnu.org
2021-04-15  0:15 ` [Bug c++/99700] [10/11 Regression] " msebor at gcc dot gnu.org
2021-04-15 16:55 ` ppalka at gcc dot gnu.org
2021-04-16 13:25 ` cvs-commit at gcc dot gnu.org
2021-04-16 13:27 ` [Bug c++/99700] [10 " ppalka at gcc dot gnu.org
2022-05-11 17:07 ` ppalka 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).