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

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Unfortunately the above patch regressed g++.dg/opt/is_constant_evaluated3.C
test.
For constructors, even when they have VOID_TYPE_P, initialized_type actually
returns non-VOID type, so constructors are optimized (though, dunno if not
differently on arm where initialized_type will likely return class* while on
other targets class).
So, we'd need to treat that way just destructors (if it shouldn't be fixed
elsewhere):
2024-03-23  Jakub Jelinek  <ja...@redhat.com>

        PR c++/114426
        * cp-gimplify.cc (cp_fold): Don't call maybe_const_value on
        CALL_EXPRs to cdtors.

        * g++.dg/cpp2a/pr114426.C: New test.

--- gcc/cp/cp-gimplify.cc.jj    2024-02-23 18:55:05.377594277 +0100
+++ gcc/cp/cp-gimplify.cc       2024-03-22 16:46:49.381442914 +0100
@@ -3395,7 +3395,13 @@ cp_fold (tree x, fold_flags_t flags)
           Do constexpr expansion of expressions where the call itself is not
           constant, but the call followed by an INDIRECT_REF is.  */
        if (callee && DECL_DECLARED_CONSTEXPR_P (callee)
-           && !flag_no_inline)
+           && !flag_no_inline
+           /* Don't invoke it on dtors.  On
+              !targetm.cxx.cdtor_returns_this () it won't do anything as it
+              has void type, so don't do it on
+              targetm.cxx.cdtor_returns_this () targets either for
+              consistency.  */
+           && !DECL_DESTRUCTOR_P (callee))
          {
            mce_value manifestly_const_eval = mce_unknown;
            if (flags & ff_mce_false)
--- gcc/testsuite/g++.dg/cpp2a/pr114426.C.jj    2024-03-22 16:49:55.650882841
+0100
+++ gcc/testsuite/g++.dg/cpp2a/pr114426.C       2024-03-22 16:48:51.829759997
+0100
@@ -0,0 +1,6 @@
+// PR c++/114426
+// { dg-do compile }
+
+struct A { virtual ~A (); };
+struct B : virtual A { virtual void foo () = 0; };
+struct C : B { C () {} };

Reply via email to