]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: non-dependent assignment checking [PR63198, PR18474]
authorPatrick Palka <ppalka@redhat.com>
Mon, 18 Sep 2023 18:47:52 +0000 (14:47 -0400)
committerPatrick Palka <ppalka@redhat.com>
Mon, 18 Sep 2023 18:47:52 +0000 (14:47 -0400)
This patch makes us recognize and check non-dependent simple assigments
ahead of time, like we already do for compound assignments.  This means
the templated representation of such assignments will now usually have
an implicit INDIRECT_REF (due to the reference return type), which the
-Wparentheses code needs to handle.  As a drive-by improvement, this
patch also makes maybe_convert_cond issue -Wparentheses warnings ahead
of time, and removes a seemingly unnecessary suppress_warning call in
build_x_modify_expr.

On the libstdc++ side, some tests were attempting to modify a data
member from a uninstantiated const member function, which this patch
minimally fixes by making the data member mutable.

PR c++/63198
PR c++/18474

gcc/cp/ChangeLog:

* semantics.cc (maybe_convert_cond): Look through implicit
INDIRECT_REF when deciding whether to issue a -Wparentheses
warning, and consider templated assignment expressions as well.
(finish_parenthesized_expr): Look through implicit INDIRECT_REF
when suppressing -Wparentheses warning.
* typeck.cc (build_x_modify_expr): Check simple assignments
ahead time too, not just compound assignments.  Give the second
operand of MODOP_EXPR a non-null type so that it's not considered
always instantiation-dependent.  Don't call suppress_warning.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/static_assert15.C: Expect diagnostic for
non-constant static_assert condition.
* g++.dg/expr/unary2.C: Remove xfails.
* g++.dg/template/init7.C: Make initializer type-dependent to
preserve intent of test.
* g++.dg/template/recurse3.C: Likewise for the erroneous
statement.
* g++.dg/template/non-dependent26.C: New test.
* g++.dg/warn/Wparentheses-32.C: New test.

libstdc++-v3/ChangeLog:

* testsuite/26_numerics/random/discard_block_engine/cons/seed_seq2.cc:
Make data member seed_seq::called mutable.
* testsuite/26_numerics/random/independent_bits_engine/cons/seed_seq2.cc:
Likewise.
* testsuite/26_numerics/random/linear_congruential_engine/cons/seed_seq2.cc:
Likewise.
* testsuite/26_numerics/random/mersenne_twister_engine/cons/seed_seq2.cc:
Likewise.
* testsuite/26_numerics/random/shuffle_order_engine/cons/seed_seq2.cc:
Likewise.
* testsuite/26_numerics/random/subtract_with_carry_engine/cons/seed_seq2.cc:
Likewise.
* testsuite/ext/random/simd_fast_mersenne_twister_engine/cons/seed_seq2.cc:
Likewise.

15 files changed:
gcc/cp/semantics.cc
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/cpp0x/static_assert15.C
gcc/testsuite/g++.dg/expr/unary2.C
gcc/testsuite/g++.dg/template/init7.C
gcc/testsuite/g++.dg/template/non-dependent26.C [new file with mode: 0644]
gcc/testsuite/g++.dg/template/recurse3.C
gcc/testsuite/g++.dg/warn/Wparentheses-32.C [new file with mode: 0644]
libstdc++-v3/testsuite/26_numerics/random/discard_block_engine/cons/seed_seq2.cc
libstdc++-v3/testsuite/26_numerics/random/independent_bits_engine/cons/seed_seq2.cc
libstdc++-v3/testsuite/26_numerics/random/linear_congruential_engine/cons/seed_seq2.cc
libstdc++-v3/testsuite/26_numerics/random/mersenne_twister_engine/cons/seed_seq2.cc
libstdc++-v3/testsuite/26_numerics/random/shuffle_order_engine/cons/seed_seq2.cc
libstdc++-v3/testsuite/26_numerics/random/subtract_with_carry_engine/cons/seed_seq2.cc
libstdc++-v3/testsuite/ext/random/simd_fast_mersenne_twister_engine/cons/seed_seq2.cc

index 0f7f4e87ae424fd74b94b43ffc07eb4049097b79..4109ac33654ff83e6af15cdd70cd06c1360299c8 100644 (file)
@@ -881,13 +881,17 @@ maybe_convert_cond (tree cond)
   /* Do the conversion.  */
   cond = convert_from_reference (cond);
 
-  if ((TREE_CODE (cond) == MODIFY_EXPR || is_assignment_op_expr_p (cond))
+  tree inner = REFERENCE_REF_P (cond) ? TREE_OPERAND (cond, 0) : cond;
+  if ((TREE_CODE (inner) == MODIFY_EXPR
+       || (TREE_CODE (inner) == MODOP_EXPR
+          && TREE_CODE (TREE_OPERAND (inner, 1)) == NOP_EXPR)
+       || is_assignment_op_expr_p (inner))
       && warn_parentheses
-      && !warning_suppressed_p (cond, OPT_Wparentheses)
-      && warning_at (cp_expr_loc_or_input_loc (cond),
+      && !warning_suppressed_p (inner, OPT_Wparentheses)
+      && warning_at (cp_expr_loc_or_input_loc (inner),
                     OPT_Wparentheses, "suggest parentheses around "
                                       "assignment used as truth value"))
-    suppress_warning (cond, OPT_Wparentheses);
+    suppress_warning (inner, OPT_Wparentheses);
 
   return condition_conversion (cond);
 }
@@ -2155,8 +2159,11 @@ cp_expr
 finish_parenthesized_expr (cp_expr expr)
 {
   if (EXPR_P (expr))
-    /* This inhibits warnings in c_common_truthvalue_conversion.  */
-    suppress_warning (expr, OPT_Wparentheses);
+    {
+      /* This inhibits warnings in c_common_truthvalue_conversion.  */
+      tree inner = REFERENCE_REF_P (expr) ? TREE_OPERAND (expr, 0) : *expr;
+      suppress_warning (inner, OPT_Wparentheses);
+    }
 
   if (TREE_CODE (expr) == OFFSET_REF
       || TREE_CODE (expr) == SCOPE_REF)
index 459739d5866b7361cccbdba59e031e67ceecd136..8132bd7fccc2669c3801c45aca0df07aa9ac678c 100644 (file)
@@ -9721,13 +9721,13 @@ build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
   if (lhs == error_mark_node || rhs == error_mark_node)
     return cp_expr (error_mark_node, loc);
 
+  tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
+
   if (processing_template_decl)
     {
-      if (modifycode == NOP_EXPR
-         || type_dependent_expression_p (lhs)
+      if (type_dependent_expression_p (lhs)
          || type_dependent_expression_p (rhs))
        {
-         tree op = build_min_nt_loc (loc, modifycode, NULL_TREE, NULL_TREE);
          tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
          if (modifycode != NOP_EXPR)
            TREE_TYPE (rval)
@@ -9739,29 +9739,24 @@ build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
       rhs = build_non_dependent_expr (rhs);
     }
 
-  if (modifycode != NOP_EXPR)
+  tree rval;
+  if (modifycode == NOP_EXPR)
+    rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
+  else
+    rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
+                        lhs, rhs, op, lookups, &overload, complain);
+  if (rval == error_mark_node)
+    return error_mark_node;
+  if (processing_template_decl)
     {
-      tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
-      tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
-                               lhs, rhs, op, lookups, &overload, complain);
-      if (rval)
-       {
-         if (rval == error_mark_node)
-           return rval;
-         suppress_warning (rval /* What warning? */);
-         if (processing_template_decl)
-           {
-             if (overload != NULL_TREE)
-               return (build_min_non_dep_op_overload
-                       (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
+      if (overload != NULL_TREE)
+       return (build_min_non_dep_op_overload
+               (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
 
-             return (build_min_non_dep
-                     (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
-           }
-         return rval;
-       }
+      return (build_min_non_dep
+             (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
     }
-  return cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
+  return rval;
 }
 
 /* Helper function for get_delta_difference which assumes FROM is a base
index a740f73fd4acdceff1b865151f70f8558774c9f4..d5f50d07c9159ee0f2bde80a93b79ae7c621d7c9 100644 (file)
@@ -5,6 +5,6 @@ template<int x>
 struct a {
   constexpr void b() {
     int c;
-    static_assert(c %= 1, "");
+    static_assert(c %= 1, ""); // { dg-error "constant" }
   }
 };
index 5962bfe19c9437d734121e4939fdd2c78ca279de..4db68375944dcd19bb9036d5f51c419a2fecc03a 100644 (file)
@@ -1,9 +1,7 @@
+// PR c++/18474
 // { dg-do compile }
 // Unary plus/minus are not lvalues.
 
-// In templates we require an instantiation to emit the diagnostic. This
-//  is wrong and it is PR 18474.
-
 int n;
 
 void f(void)
@@ -15,6 +13,6 @@ void f(void)
 template <int>
 void g(void)
 {
-  -n = 0;        // { dg-error "lvalue" "" { xfail *-*-* } }
-  +n = 0;        // { dg-error "lvalue" "" { xfail *-*-* } }
+  -n = 0;        // { dg-error "lvalue" "" }
+  +n = 0;        // { dg-error "lvalue" "" }
 }
index bb26c8f92b5d62c91a72478be4de261f857b237d..94fc22f578c9f8bc9aca52be9f8a8af426b3a94f 100644 (file)
@@ -6,4 +6,4 @@ template<typename> struct A
   static const int i=0;
 };
 
-template<typename T> const int A<T>::i = 0=0; /* { dg-error "duplicate initialization" } */
+template<typename T> const int A<T>::i = T()=0; /* { dg-error "duplicate initialization" } */
diff --git a/gcc/testsuite/g++.dg/template/non-dependent26.C b/gcc/testsuite/g++.dg/template/non-dependent26.C
new file mode 100644 (file)
index 0000000..1faa39a
--- /dev/null
@@ -0,0 +1,25 @@
+// Verify non-dependent assignment expressions are recognized as such
+// and are checked ahead of time.
+// PR c++/63198
+// { dg-do compile { target c++11 } }
+
+struct X { using t1 = int; };
+struct Y { X operator=(const Y&); } y;
+template<class T> void f1(decltype(y = y)::t1);
+
+int n;
+template<class T> void f2(decltype(n = n)::t1); // { dg-error "not a class" }
+template<class T> void f3(decltype(n += n)::t1); // { dg-error "not a class" }
+
+template<class T>
+void g() {
+  const int n;
+  n = 42; // { dg-error "read-only" }
+
+  const X x;
+  x = {}; // { dg-error "no match" }
+
+  const Y y;
+  y = {}; // { dg-error "no match" }
+  Y{} = X{}; // { dg-error "no match" }
+}
index f1db7c5cbca04d33c72a8bdff9635d8f59c9fce4..70c6152d0633af1d54bec483a96560d87577cf77 100644 (file)
@@ -1,14 +1,14 @@
 // PR c++/44609
 // { dg-options -ftemplate-depth=10 }
 
-template<int N>
+template<class T, int N>
 void f()
 {
-  0 = 0;                       // { dg-error "lvalue required" }
-  f<N+1>();                    // { dg-bogus "instantiation depth" }
+  T(0) = 0;                    // { dg-error "lvalue required" }
+  f<T, N+1>();                 // { dg-bogus "instantiation depth" }
 }
 
 int main()
 {
-  f<0>();
+  f<int, 0>();
 }
diff --git a/gcc/testsuite/g++.dg/warn/Wparentheses-32.C b/gcc/testsuite/g++.dg/warn/Wparentheses-32.C
new file mode 100644 (file)
index 0000000..719a9d9
--- /dev/null
@@ -0,0 +1,28 @@
+// Verify we issue -Wparentheses warnings at template definition time
+// (for suitable non-dependent expressions).
+// { dg-additional-options "-Wparentheses" }
+
+struct X { operator bool(); };
+struct Y { Y& operator=(const Y&); operator bool(); };
+struct Z { int m; operator bool(); };
+
+template<class T>
+void f() {
+  int n, m;
+  if (n = m) { } // { dg-warning "parentheses" }
+
+  X x1, x2;
+  if (x1 = x2) { } // { dg-warning "parentheses" }
+
+  Y y1, y2;
+  if (y1 = y2) { } // { dg-warning "parentheses" }
+
+  Z z1, z2;
+  if (z1 = z2) { } // { dg-warning "parentheses" }
+
+  bool b;
+  b = m = n; // { dg-warning "parentheses" "" { xfail *-*-* } }
+  b = x1 = x2; // { dg-warning "parentheses" "" { xfail *-*-* } }
+  b = y1 = y2; // { dg-warning "parentheses" "" { xfail *-*-* } }
+  b = z1 = z2; // { dg-warning "parentheses" "" { xfail *-*-* } }
+}
index 720ce96dc283fc89ca76f16e870b3009f52ba7d5..0d7e667382d8b8a30884730db8244ae9e703c969 100644 (file)
@@ -51,7 +51,7 @@ struct seed_seq
   // T is convertible to the engine's result_type:
   operator T() const noexcept { return T(); }
 
-  bool called = false;
+  mutable bool called = false;
 };
 
 using engine_type
index f378621805f3ef811f6d484c09845d23323f8b7a..2ef563038ccdd3ab8ec03b305d532ded955dd26f 100644 (file)
@@ -51,7 +51,7 @@ struct seed_seq
   // T is convertible to the engine's result_type:
   operator T() const noexcept { return T(); }
 
-  bool called = false;
+  mutable bool called = false;
 };
 
 using engine_type
index c1cfe9da7fbc220d0f219f2278800b1c38f060ca..071ed75d8d85f965f39d2fab9c79faec9016a9c1 100644 (file)
@@ -51,7 +51,7 @@ struct seed_seq
   // T is convertible to the engine's result_type:
   operator T() const noexcept { return T(); }
 
-  bool called = false;
+  mutable bool called = false;
 };
 
 using engine_type
index 5247602653359bfc7c7ad7da7de98393f7e8c2b9..2fb9622fda0694c70aabcee19fa2b70ebbad0568 100644 (file)
@@ -51,7 +51,7 @@ struct seed_seq
   // T is convertible to the engine's result_type:
   operator T() const noexcept { return T(); }
 
-  bool called = false;
+  mutable bool called = false;
 };
 
 using engine_type
index 1ca783e304494735fa7ee979b77c060a8542787c..3fa9c9ad8d349d559e3f7b8473777730ad1948b8 100644 (file)
@@ -51,7 +51,7 @@ struct seed_seq
   // T is convertible to the engine's result_type:
   operator T() const noexcept { return T(); }
 
-  bool called = false;
+  mutable bool called = false;
 };
 
 using engine_type
index 08beb3bcf93ab24a2a4b1eba69b97ab5292aa2ec..25d19a0ba6602c0b9a199f7f79ef35c4c0de0960 100644 (file)
@@ -51,7 +51,7 @@ struct seed_seq
   // T is convertible to the engine's result_type:
   operator T() const noexcept { return T(); }
 
-  bool called = false;
+  mutable bool called = false;
 };
 
 using engine_type
index b629053585eca6cb29114ded1d06d871e382a386..fe798f2f05be01aa8abc2ebf999bbc94aed8f024 100644 (file)
@@ -52,7 +52,7 @@ struct seed_seq
   // T is convertible to the engine's result_type:
   operator T() const noexcept { return T(); }
 
-  bool called = false;
+  mutable bool called = false;
 };
 
 using engine_type