]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: undiagnosed error_mark_node from cp_build_c_cast [PR112658]
authorPatrick Palka <ppalka@redhat.com>
Fri, 8 Dec 2023 18:33:55 +0000 (13:33 -0500)
committerPatrick Palka <ppalka@redhat.com>
Fri, 8 Dec 2023 18:33:55 +0000 (13:33 -0500)
When cp_build_c_cast commits to an erroneous const_cast, we neglect to
replay errors from build_const_cast_1 which can lead to us incorrectly
accepting (and "miscompiling") the cast, or triggering the assert in
finish_expr_stmt.

This patch fixes this oversight.  This was the original fix for the ICE
in PR112658 before r14-5941-g305a2686c99bf9 made us accept the testcase
there after all.  I wasn't able to come up with an alternate testcase for
which this fix has an effect anymore, but below is a reduced version of
the PR112658 testcase (accepted ever since r14-5941) for good measure.

PR c++/112658
PR c++/94264

gcc/cp/ChangeLog:

* typeck.cc (cp_build_c_cast): If we're committed to a const_cast
and the result is erroneous, call build_const_cast_1 a second
time to issue errors.  Use complain=tf_none instead of =false.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/initlist-array20.C: New test.

gcc/cp/typeck.cc
gcc/testsuite/g++.dg/cpp0x/initlist-array20.C [new file with mode: 0644]

index 8e4cfae08aa645f17e09da67588e1ca0b5bb106d..258cfd43114dc964f05defc30469c7bbc2219de5 100644 (file)
@@ -9213,6 +9213,8 @@ cp_build_c_cast (location_t loc, tree type, tree expr,
          maybe_warn_about_useless_cast (loc, type, value, complain);
          maybe_warn_about_cast_ignoring_quals (loc, type, complain);
        }
+      else if (complain & tf_error)
+       build_const_cast_1 (loc, type, value, tf_error, &valid_p);
       return result;
     }
 
@@ -9248,7 +9250,7 @@ cp_build_c_cast (location_t loc, tree type, tree expr,
         to succeed.  */
       if (!same_type_p (non_reference (type), non_reference (result_type)))
        {
-         result = build_const_cast_1 (loc, type, result, false, &valid_p);
+         result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
          gcc_assert (valid_p);
        }
       return result;
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-array20.C b/gcc/testsuite/g++.dg/cpp0x/initlist-array20.C
new file mode 100644 (file)
index 0000000..048c5b4
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/112658
+// PR c++/94264
+// { dg-do compile { target c++11 } }
+
+void f(int*);
+
+int main() {
+  using array = int[];
+  f(array{42});
+  f((int*)array{42});
+}