public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-9986] c++: lambda and the current instantiation [PR82980]
Date: Thu, 12 May 2022 20:14:21 +0000 (GMT)	[thread overview]
Message-ID: <20220512201421.5EE91394FC19@sourceware.org> (raw)

https://gcc.gnu.org/g:dc8739c2ab14f0108e4fabbd565fa0918e4425ee

commit r11-9986-gdc8739c2ab14f0108e4fabbd565fa0918e4425ee
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Apr 14 08:16:45 2022 -0400

    c++: lambda and the current instantiation [PR82980]
    
    When a captured variable is type-dependent, we've expressed the type of the
    capture field and proxy with a decltype variant.  But if the type is "the
    current instantiation", we need to be able to see that so that we can do
    lookup inside it just like we could with the captured variable itself.
    
    I also tried looking through lambda capture in
    cp_parser_postfix_dot_deref_expression, but this way seems cleaner.  I plan
    to treat more types as deducible in stage 1.
    
    I considered also using this in do_auto_deduction, but think that would be
    wrong: [temp.dep.expr] says an id-expression is type-dependent if it is
    "associated by name lookup with a variable declared with a type that
    contains a placeholder type where the initializer is type-dependent".  That
    doesn't clearly exclude deducing a dependent type from the initializer, but
    it seems like a barrier, and other implementations agree.
    
            PR c++/82980
    
    gcc/cp/ChangeLog:
    
            * lambda.c (type_deducible_expression_p): New.
            (lambda_capture_field_type): Check it.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/lambda/lambda-current-inst1.C: New test.

Diff:
---
 gcc/cp/lambda.c                                      | 20 +++++++++++++++++++-
 .../g++.dg/cpp0x/lambda/lambda-current-inst1.C       | 18 ++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c
index cca414c1a69..1508e232091 100644
--- a/gcc/cp/lambda.c
+++ b/gcc/cp/lambda.c
@@ -203,6 +203,24 @@ lambda_function (tree lambda)
   return lambda;
 }
 
+/* True if EXPR is an expression whose type can be used directly in lambda
+   capture.  Not to be used for 'auto'.  */
+
+static bool
+type_deducible_expression_p (tree expr)
+{
+  if (!type_dependent_expression_p (expr))
+    return true;
+  if (BRACE_ENCLOSED_INITIALIZER_P (expr)
+      || TREE_CODE (expr) == EXPR_PACK_EXPANSION)
+    return false;
+  tree t = non_reference (TREE_TYPE (expr));
+  if (!t) return false;
+  while (TREE_CODE (t) == POINTER_TYPE)
+    t = TREE_TYPE (t);
+  return currently_open_class (t);
+}
+
 /* Returns the type to use for the FIELD_DECL corresponding to the
    capture of EXPR.  EXPLICIT_INIT_P indicates whether this is a
    C++14 init capture, and BY_REFERENCE_P indicates whether we're
@@ -229,7 +247,7 @@ lambda_capture_field_type (tree expr, bool explicit_init_p,
       else
 	type = do_auto_deduction (type, expr, auto_node);
     }
-  else if (!is_this && type_dependent_expression_p (expr))
+  else if (!is_this && !type_deducible_expression_p (expr))
     {
       type = cxx_make_type (DECLTYPE_TYPE);
       DECLTYPE_TYPE_EXPR (type) = expr;
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-current-inst1.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-current-inst1.C
new file mode 100644
index 00000000000..a6631c5ca99
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-current-inst1.C
@@ -0,0 +1,18 @@
+// PR c++/82980
+// { dg-do compile { target c++11 } }
+
+template <class T>
+struct Outer
+{
+  template <class U>
+  void f();
+
+  void bar(Outer outer) {
+    [outer](){ outer.f<int>(); };
+  }
+  void baz(Outer *p) {
+    [&](){ p->f<int>(); };
+  }
+};
+
+int main() { }


                 reply	other threads:[~2022-05-12 20:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220512201421.5EE91394FC19@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).