]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: constant expressions are evaluated [PR93314]
authorJason Merrill <jason@redhat.com>
Wed, 14 Apr 2021 15:24:50 +0000 (11:24 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 18 May 2021 21:18:21 +0000 (17:18 -0400)
My GCC 11 patch for PR93314 turned off cp_unevaluated_operand while
processing an id-expression that names a non-static data member, but the
broader issue is that in general, a constant-expression is evaluated even in
an unevaluated operand.

This also fixes 100205, introduced by the earlier patch that couldn't
distinguish between the different allow_non_constant_p cases.

PR c++/100205
PR c++/93314

gcc/cp/ChangeLog:

* cp-tree.h (cp_evaluated): Add reset parm to constructor.
* parser.c (cp_parser_constant_expression): Change
allow_non_constant_p to int.  Use cp_evaluated.
(cp_parser_initializer_clause): Pass 2 to allow_non_constant_p.
* semantics.c (finish_id_expression_1): Don't mess with
cp_unevaluated_operand here.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/decltype-nonstatic1.C: New test.

gcc/cp/cp-tree.h
gcc/cp/parser.c
gcc/cp/semantics.c
gcc/testsuite/g++.dg/cpp0x/decltype-nonstatic1.C [new file with mode: 0644]

index 23a77a2b2e0722c2e2f5b96a6f3ad9587b011c84..fbf33b9150ee0e33d3a11733168f8a8ceac46c21 100644 (file)
@@ -5480,9 +5480,10 @@ class cp_evaluated
 public:
   int uneval;
   int inhibit;
-  cp_evaluated ()
+  cp_evaluated (bool reset = true)
     : uneval(cp_unevaluated_operand), inhibit(c_inhibit_evaluation_warnings)
-  { cp_unevaluated_operand = c_inhibit_evaluation_warnings = 0; }
+  { if (reset)
+      cp_unevaluated_operand = c_inhibit_evaluation_warnings = 0; }
   ~cp_evaluated ()
   { cp_unevaluated_operand = uneval;
     c_inhibit_evaluation_warnings = inhibit; }
index a3dae3164ce14a4fe1cd25e946ddbfee583018ef..c1001713ac8a6bd46680db4f41a001b3b3964d1c 100644 (file)
@@ -2163,7 +2163,7 @@ static enum tree_code cp_parser_assignment_operator_opt
 static cp_expr cp_parser_expression
   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
 static cp_expr cp_parser_constant_expression
-  (cp_parser *, bool = false, bool * = NULL, bool = false);
+  (cp_parser *, int = 0, bool * = NULL, bool = false);
 static cp_expr cp_parser_builtin_offsetof
   (cp_parser *);
 static cp_expr cp_parser_lambda_expression
@@ -10374,13 +10374,15 @@ cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
-  is false, NON_CONSTANT_P should be NULL.  If STRICT_P is true,
+  is false, NON_CONSTANT_P should be NULL.  If ALLOW_NON_CONSTANT_P is
+  greater than 1, this isn't really a constant-expression, only a
+  potentially constant-evaluated expression.  If STRICT_P is true,
   only parse a conditional-expression, otherwise parse an
   assignment-expression.  See below for rationale.  */
 
 static cp_expr
 cp_parser_constant_expression (cp_parser* parser,
-                              bool allow_non_constant_p,
+                              int allow_non_constant_p,
                               bool *non_constant_p,
                               bool strict_p)
 {
@@ -10416,6 +10418,11 @@ cp_parser_constant_expression (cp_parser* parser,
   parser->allow_non_integral_constant_expression_p
     = (allow_non_constant_p || cxx_dialect >= cxx11);
   parser->non_integral_constant_expression_p = false;
+
+  /* A manifestly constant-evaluated expression is evaluated even in an
+     unevaluated operand.  */
+  cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
+
   /* Although the grammar says "conditional-expression", when not STRICT_P,
      we parse an "assignment-expression", which also permits
      "throw-expression" and the use of assignment operators.  In the case
@@ -24236,7 +24243,7 @@ cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
     {
       initializer
        = cp_parser_constant_expression (parser,
-                                       /*allow_non_constant_p=*/true,
+                                       /*allow_non_constant_p=*/2,
                                        non_constant_p);
     }
   else
index 3a6468fd5f3b5a4033117207f99f8f65079829e2..27a50d70899996527d20a8e6a1d3704ee4b85e27 100644 (file)
@@ -4093,12 +4093,6 @@ finish_id_expression_1 (tree id_expression,
 
          cp_warn_deprecated_use_scopes (scope);
 
-         /* In a constant-expression context, turn off cp_unevaluated_operand
-            so finish_non_static_data_member will complain (93314).  */
-         auto eval = make_temp_override (cp_unevaluated_operand);
-         if (integral_constant_expression_p && TREE_CODE (decl) == FIELD_DECL)
-           cp_unevaluated_operand = 0;
-
          if (TYPE_P (scope))
            decl = finish_qualified_id_expr (scope,
                                             decl,
@@ -4112,10 +4106,6 @@ finish_id_expression_1 (tree id_expression,
        }
       else if (TREE_CODE (decl) == FIELD_DECL)
        {
-         auto eval = make_temp_override (cp_unevaluated_operand);
-         if (integral_constant_expression_p)
-           cp_unevaluated_operand = 0;
-
          /* Since SCOPE is NULL here, this is an unqualified name.
             Access checking has been performed during name lookup
             already.  Turn off checking to avoid duplicate errors.  */
diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype-nonstatic1.C b/gcc/testsuite/g++.dg/cpp0x/decltype-nonstatic1.C
new file mode 100644 (file)
index 0000000..bc488ff
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/100205
+// { dg-do compile { target c++11 } }
+
+struct coordinate_matrix {
+  using index_t = unsigned;
+  struct convert_to_matrix_coordinate {
+    index_t column_id;
+  };
+  index_t column_id;
+
+  // does not work
+  using value_type2 = decltype(convert_to_matrix_coordinate{column_id});
+
+  // does work
+  using value_type5 = decltype(column_id);
+};