On 11/14/23 11:10, Patrick Palka wrote:
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

-- >8 --

For decltype((x)) within a lambda where x is not captured, we dubiously
require that the lambda has a capture default, unlike for decltype(x).
This patch fixes this inconsistency; I couldn't find a justification for
it in the standard.

The relevant passage seems to be

https://eel.is/c++draft/expr.prim#id.unqual-3

"If naming the entity from outside of an unevaluated operand within S would refer to an entity captured by copy in some intervening lambda-expression, then let E be the innermost such lambda-expression.

If there is such a lambda-expression and if P is in E's function parameter scope but not its parameter-declaration-clause, then the type of the expression is the type of a class member access expression ([expr.ref]) naming the non-static data member that would be declared for such a capture in the object parameter ([dcl.fct]) of the function call operator of E."

In this case I guess there is no such lambda-expression because naming x won't refer to a capture by copy if the lambda doesn't capture anything, so we ignore the lambda.

Maybe refer to that in a comment?  OK with that change.

I'm surprised that it refers specifically to capture by copy, but I guess a capture by reference should have the same decltype as the captured variable?

Jason

        PR c++/83167

gcc/cp/ChangeLog:

        * semantics.cc (finish_decltype_type): If capture_decltype
        returns NULL_TREE, fall back to the ordinary code path.
        (capture_decltype): Return NULL_TREE if the lambda has no
        capture-default.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp0x/lambda/lambda-decltype4.C: New test.
---
  gcc/cp/semantics.cc                               |  6 +++---
  .../g++.dg/cpp0x/lambda/lambda-decltype4.C        | 15 +++++++++++++++
  2 files changed, 18 insertions(+), 3 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype4.C

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 8090c71809f..6fdd6c45972 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -11732,7 +11732,8 @@ finish_decltype_type (tree expr, bool 
id_expression_or_member_access_p,
        /* If the expression is just "this", we want the
           cv-unqualified pointer for the "this" type.  */
        type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
-      else
+
+      if (!type)
        {
          /* Otherwise, where T is the type of e, if e is an lvalue,
             decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
@@ -12639,8 +12640,7 @@ capture_decltype (tree decl)
      switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
        {
        case CPLD_NONE:
-       error ("%qD is not captured", decl);
-       return error_mark_node;
+       return NULL_TREE;
case CPLD_COPY:
        type = TREE_TYPE (decl);
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype4.C 
b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype4.C
new file mode 100644
index 00000000000..0062d7b8672
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-decltype4.C
@@ -0,0 +1,15 @@
+// PR c++/83167
+// { dg-do compile { target c++11 } }
+
+int main() {
+  int x;
+  const int y = 42;
+
+  [] {
+    using ty1 = decltype((x));
+    using ty1 = int&;
+
+    using ty2 = decltype((y));
+    using ty2 = const int&;
+  };
+}

Reply via email to