]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: constantness of local var in constexpr fn [PR111703, PR112269]
authorPatrick Palka <ppalka@redhat.com>
Wed, 15 Nov 2023 17:03:16 +0000 (12:03 -0500)
committerPatrick Palka <ppalka@redhat.com>
Wed, 15 Nov 2023 17:03:16 +0000 (12:03 -0500)
potential_constant_expression was incorrectly treating most local
variables from a constexpr function as constant because it wasn't
considering the 'now' parameter.  This patch fixes this by relaxing
its var_in_maybe_constexpr_fn checks accordingly, which turns out to
partially fix two recently reported regressions:

PR111703 is a regression caused by r11-550-gf65a3299a521a4 for restricting
constexpr evaluation during warning-dependent folding.  The mechanism is
intended to restrict only constant evaluation of the instantiated
non-dependent expression, but it also ends up restricting constant
evaluation occurring during instantiation of the expression, in particular
when instantiating the converted argument 'x' (a VIEW_CONVERT_EXPR) into
a copy constructor call.  This seems like a flaw in the mechanism, though
I don't know if we want to fix the mechanism or get rid of it completely
since the original testcases which motivated the mechanism are fixed more
simply by r13-1225-gb00b95198e6720.  In any case, this patch partially
fixes this by making us correctly treat 'x' as non-constant which prevents
the problematic warning-dependent folding from occurring at all.

PR112269 is caused by r14-4796-g3e3d73ed5e85e7 for merging tsubst_copy
into tsubst_copy_and_build.  tsubst_copy used to exit early when 'args'
was empty, behavior which that commit deliberately didn't preserve.
This early exit masked the fact that COMPLEX_EXPR wasn't handled by
tsubst at all, and is a tree code that apparently we could see during
warning-dependent folding on some targets.  A complete fix is to add
handling for this tree code in tsubst_expr, but this patch should fix
the reported testsuite failures since the COMPLEX_EXPRs that crop up
in <complex> are considered non-constant expressions after this patch.

PR c++/111703
PR c++/112269

gcc/cp/ChangeLog:

* constexpr.cc (potential_constant_expression_1) <case VAR_DECL>:
Only consider var_in_maybe_constexpr_fn if 'now' is false.
<case INDIRECT_REF>: Likewise.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/concepts-fn8.C: New test.

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

index c05760e6789d035bd949ff5ff0e42b75015e736b..8a6b210144afac1b35ba61f9c94ced5e216a68c9 100644 (file)
@@ -9623,7 +9623,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
          return RECUR (DECL_VALUE_EXPR (t), rval);
        }
       if (want_rval
-         && !var_in_maybe_constexpr_fn (t)
+         && (now || !var_in_maybe_constexpr_fn (t))
          && !type_dependent_expression_p (t)
          && !decl_maybe_constant_var_p (t)
          && (strict
@@ -9737,7 +9737,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
         STRIP_NOPS (x);
         if (is_this_parameter (x) && !is_capture_proxy (x))
          {
-           if (!var_in_maybe_constexpr_fn (x))
+           if (now || !var_in_maybe_constexpr_fn (x))
              {
                if (flags & tf_error)
                  constexpr_error (loc, fundef_p, "use of %<this%> in a "
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C b/gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C
new file mode 100644 (file)
index 0000000..3f63a5b
--- /dev/null
@@ -0,0 +1,24 @@
+// PR c++/111703
+// { dg-do compile { target c++20 } }
+
+template<class T>
+constexpr bool always_true() { return true; }
+
+struct P {
+  P() = default;
+
+  template<class T>
+    requires (always_true<T>()) // { dg-bogus "used before its definition" }
+  constexpr P(const T&) { }
+
+  int n, m;
+};
+
+void (*f)(P);
+
+template<class T>
+constexpr bool g() {
+  P x;
+  f(x); // { dg-bogus "from here" }
+  return true;
+}