]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: ICE with constexpr call that returns a PMF [PR98551]
authorPatrick Palka <ppalka@redhat.com>
Fri, 8 Jan 2021 15:11:25 +0000 (10:11 -0500)
committerPatrick Palka <ppalka@redhat.com>
Sat, 9 Jan 2021 03:49:21 +0000 (22:49 -0500)
We shouldn't do replace_result_decl after evaluating a call that returns
a PMF because PMF temporaries aren't wrapped in a TARGET_EXPR (and so we
can't trust ctx->object), and PMF initializers can't be self-referential
anyway, so replace_result_decl would always be a no-op.

To that end, this patch changes the relevant AGGREGATE_TYPE_P test to
CLASS_TYPE_P, which should rule out PMFs (as well as arrays, which we
can't return and therefore won't see here).  This fixes an ICE from the
sanity check in replace_result_decl in the below testcase during
constexpr evaluation of the call f() in the initializer g(f()).

gcc/cp/ChangeLog:

PR c++/98551
* constexpr.c (cxx_eval_call_expression): Check CLASS_TYPE_P
instead of AGGREGATE_TYPE_P before calling replace_result_decl.

gcc/testsuite/ChangeLog:

PR c++/98551
* g++.dg/cpp0x/constexpr-pmf2.C: New test.

(cherry picked from commit bb1f0b50abbfa01e0ed720a5225a11aa7af32a89)

gcc/cp/constexpr.c
gcc/testsuite/g++.dg/cpp0x/constexpr-pmf2.C [new file with mode: 0644]

index c5051c020c594b52feddc719ea83ada2fe00ed93..5b1e3c181a01f934533ddb642700481d7ca220b9 100644 (file)
@@ -2634,7 +2634,7 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
            /* Rewrite all occurrences of the function's RESULT_DECL with the
               current object under construction.  */
            if (!*non_constant_p && ctx->object
-               && AGGREGATE_TYPE_P (TREE_TYPE (res))
+               && CLASS_TYPE_P (TREE_TYPE (res))
                && !is_empty_class (TREE_TYPE (res)))
              if (replace_result_decl (&result, res, ctx->object))
                cacheable = false;
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-pmf2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-pmf2.C
new file mode 100644 (file)
index 0000000..eed4e3d
--- /dev/null
@@ -0,0 +1,9 @@
+// PR c++/98551
+// { dg-do compile { target c++11 } }
+
+struct A {};
+struct B { int t(); };
+using pmf = decltype(&B::t);
+constexpr pmf f() { return &B::t; }
+constexpr A g(pmf) { return {}; }
+constexpr A x = g(f());