]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: fix function body cloning when using implicit constexpr
authorYuao Ma <c8ef@outlook.com>
Mon, 22 Dec 2025 16:40:54 +0000 (00:40 +0800)
committerYuao Ma <c8ef@outlook.com>
Mon, 22 Dec 2025 17:23:16 +0000 (01:23 +0800)
When using implicit constexpr, we should not discard the function body, as it
can result in ICE during constant evaluation.

PR c++/123261

gcc/cp/ChangeLog:

* semantics.cc (expand_or_defer_fn_1): Use maybe_constexpr_fn.

gcc/testsuite/ChangeLog:

* g++.dg/ext/fimplicit-constexpr2.C: New test.

gcc/cp/semantics.cc
gcc/testsuite/g++.dg/ext/fimplicit-constexpr2.C [new file with mode: 0644]

index a2d655a60c1deb948fd07b9542ba6916640fe155..e598632d85bc77d9eeacafa58d3f2e5041bb651e 100644 (file)
@@ -5578,7 +5578,7 @@ expand_or_defer_fn_1 (tree fn)
         the maybe-in-charge cdtor and regenerate the clones from it on
         demand, so we also need to keep the body.  Otherwise we don't
         need it anymore.  */
-      if (!DECL_DECLARED_CONSTEXPR_P (fn)
+      if (!maybe_constexpr_fn (fn)
          && !(module_maybe_has_cmi_p () && vague_linkage_p (fn)))
        DECL_SAVED_TREE (fn) = void_node;
       return false;
diff --git a/gcc/testsuite/g++.dg/ext/fimplicit-constexpr2.C b/gcc/testsuite/g++.dg/ext/fimplicit-constexpr2.C
new file mode 100644 (file)
index 0000000..f72323e
--- /dev/null
@@ -0,0 +1,18 @@
+// { dg-additional-options -fimplicit-constexpr }
+// { dg-do compile { target c++23 } }
+
+class A
+{
+public:
+  A () { asm volatile (""); } // { dg-error {inline assembly is not a constant expression} }
+  ~A () {}
+};
+
+constexpr bool
+test ()
+{
+  A a; // { dg-error {'A::A\(\)' called in a constant expression} }
+  return true;
+}
+
+static_assert (test ()); // { dg-error {non-constant condition for static assertion} }