]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: maybe_constant_init and unevaluated operands [PR109030]
authorPatrick Palka <ppalka@redhat.com>
Thu, 16 Mar 2023 18:47:43 +0000 (14:47 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 16 Mar 2023 18:47:43 +0000 (14:47 -0400)
This testcase in this PR (already fixed by r13-6526-ge4692319fd5fc7)
demonstrates that maybe_constant_init can be called on an unevaluated
operand (e.g. from massage_init_elt) so this entry point should also
limit constant evaluation in that case, like maybe_constant_value does.

PR c++/109030

gcc/cp/ChangeLog:

* constexpr.cc (maybe_constant_init_1): For an unevaluated
non-manifestly-constant operand, don't constant evaluate
and instead call fold_to_constant as in maybe_constant_value.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/constexpr-inst2.C: New test.

gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp2a/constexpr-inst2.C [new file with mode: 0644]

index abf6ee560c524664ed8d25cd1ae5dffdd46b1cfb..3de60cfd0f84ffeb8c0427689153e87438c30a18 100644 (file)
@@ -8796,6 +8796,10 @@ maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
                        && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
       if (is_static)
        manifestly_const_eval = true;
+
+      if (cp_unevaluated_operand && !manifestly_const_eval)
+       return fold_to_constant (t);
+
       t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static,
                                            mce_value (manifestly_const_eval),
                                            false, decl);
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-inst2.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-inst2.C
new file mode 100644 (file)
index 0000000..7676b62
--- /dev/null
@@ -0,0 +1,12 @@
+// { dg-do compile { target c++11 } }
+
+struct __as_receiver {
+  int empty_env;
+};
+
+template<class T>
+constexpr int f(T t) {
+  return t.fail;
+};
+
+using type = decltype(__as_receiver{f(0)}); // OK, f<int> not instantiated