]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: requires-expr and access checking [PR107179]
authorPatrick Palka <ppalka@redhat.com>
Thu, 3 Nov 2022 19:35:18 +0000 (15:35 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 3 Nov 2022 19:35:18 +0000 (15:35 -0400)
Like during satisfaction, we also need to avoid deferring access checks
during substitution of a requires-expr because the outcome of an access
check can determine the value of the requires-expr.  Otherwise (in
deferred access checking contexts such as within a base-clause), the
requires-expr may evaluate to the wrong result, and along the way a
failed access check may leak out from it into a non-SFINAE context and
cause a hard error (as in the below testcase).

PR c++/107179

gcc/cp/ChangeLog:

* constraint.cc (tsubst_requires_expr): Make sure we're not
deferring access checks.

gcc/testsuite/ChangeLog:

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

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

index 5e6a3bcf0598ef436f37f9b414e1565dc5b5a515..f6ef078171a7f7e540d38f23e1c1ca5a76f6bbc1 100644 (file)
@@ -2252,6 +2252,9 @@ tsubst_requires_expr (tree t, tree args, sat_info info)
 {
   local_specialization_stack stack (lss_copy);
 
+  /* We need to check access during the substitution.  */
+  deferring_access_check_sentinel acs (dk_no_deferred);
+
   /* A requires-expression is an unevaluated context.  */
   cp_unevaluated u;
 
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-requires31.C b/gcc/testsuite/g++.dg/cpp2a/concepts-requires31.C
new file mode 100644 (file)
index 0000000..cd26b9c
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/107179
+// { dg-do compile { target c++20 } }
+
+template<bool B> struct bool_constant { static constexpr bool value = B; };
+
+template<typename T>
+  struct is_implicitly_default_constructible
+  : bool_constant<requires { T(); }>
+  { };
+
+struct X { private: X(); };
+struct Y { };
+
+static_assert( !is_implicitly_default_constructible<X>::value );
+static_assert(  is_implicitly_default_constructible<Y>::value );