]> gcc.gnu.org Git - gcc.git/commitdiff
c++: lambda in concept [PR105652]
authorJason Merrill <jason@redhat.com>
Fri, 27 May 2022 02:43:05 +0000 (22:43 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 31 May 2022 19:31:03 +0000 (15:31 -0400)
We currently check satisfaction in the context of the constrained
declaration (which may be wrong, see PR104111).  When checking C<int>
for S<int>, we currently substitute into the lambda in the context of
S<T> (rather than S<int>, which seems wrong if the above isn't wrong), so
the new closure type thinks its context is S<T>, which confuses debug
output.  For the moment, let's work around all of this by overriding the
context of the closure.

PR c++/105652

gcc/cp/ChangeLog:

* pt.cc (tsubst_lambda_expr): Don't let a namespace-scope lambda
instantiate into a class-scope lambda.

gcc/testsuite/ChangeLog:

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

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

index f18b4a5fa914098178b0d83840c5be1f298c1054..1c1b0e39543c9b404039edd76aa35d1a5f78f25f 100644 (file)
@@ -19724,11 +19724,18 @@ tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
     return error_mark_node;
 
   if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
-    /* A lambda in a default argument outside a class gets no
-       LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI.  But
-       tsubst_default_argument calls start_lambda_scope, so we need to
-       specifically ignore it here, and use the global scope.  */
-    record_null_lambda_scope (r);
+    {
+      /* A lambda in a default argument outside a class gets no
+        LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI.  But
+        tsubst_default_argument calls start_lambda_scope, so we need to
+        specifically ignore it here, and use the global scope.  */
+      record_null_lambda_scope (r);
+
+      /* If we're pushed into another scope (PR105652), fix it.  */
+      if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t)))
+       TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type))
+         = TYPE_CONTEXT (TREE_TYPE (t));
+    }
   else
     record_lambda_scope (r);
 
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-lambda20.C b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda20.C
new file mode 100644 (file)
index 0000000..40e5973
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/105652
+// { dg-do compile { target c++20 } }
+// { dg-additional-options -g }
+
+template<int>
+struct I {};
+
+template<class T>
+concept C = []<int N>(I<N>) { return true; } (I<0>{});
+
+template<class T>
+struct S { };
+
+template<C T>
+struct S<T> { constexpr static bool value = true; };
+
+static_assert(S<int>::value);
This page took 0.106211 seconds and 5 git commands to generate.