diff mbox series

c++: decltype of capture proxy [PR79378, PR96917]

Message ID 20231107195237.1658753-1-ppalka@redhat.com
State New
Headers show
Series c++: decltype of capture proxy [PR79378, PR96917] | expand

Commit Message

Patrick Palka Nov. 7, 2023, 7:52 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

-- >8 --

We usually don't see capture proxies in finish_decltype_type because
process_outer_var_ref is a no-op inside an unevaluated context and
so a use of a capture inside decltype refers directly to the captured
variable.  But we can still see a capture proxy during decltype(auto)
deduction and for decltype of an init-capture, which suggests we need
to handle capture proxies specially within finish_decltype_type (since
they're always implicitly const).  This patch adds such handling.

	PR c++/79378
	PR c++/96917

gcc/cp/ChangeLog:

	* semantics.cc (finish_decltype_type): Handle an id-expression
	naming a capture proxy specially.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1y/decltype-auto7.C: New test.
	* g++.dg/cpp1y/lambda-init20.C: New test.
---
 gcc/cp/semantics.cc                         | 28 +++++++++--
 gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C | 53 +++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp1y/lambda-init20.C  | 22 +++++++++
 3 files changed, 98 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/lambda-init20.C

Comments

Jason Merrill Nov. 10, 2023, 12:40 a.m. UTC | #1
On 11/7/23 14:52, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

OK.

> -- >8 --
> 
> We usually don't see capture proxies in finish_decltype_type because
> process_outer_var_ref is a no-op inside an unevaluated context and
> so a use of a capture inside decltype refers directly to the captured
> variable.  But we can still see a capture proxy during decltype(auto)
> deduction and for decltype of an init-capture, which suggests we need
> to handle capture proxies specially within finish_decltype_type (since
> they're always implicitly const).  This patch adds such handling.
> 
> 	PR c++/79378
> 	PR c++/96917
> 
> gcc/cp/ChangeLog:
> 
> 	* semantics.cc (finish_decltype_type): Handle an id-expression
> 	naming a capture proxy specially.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp1y/decltype-auto7.C: New test.
> 	* g++.dg/cpp1y/lambda-init20.C: New test.
> ---
>   gcc/cp/semantics.cc                         | 28 +++++++++--
>   gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C | 53 +++++++++++++++++++++
>   gcc/testsuite/g++.dg/cpp1y/lambda-init20.C  | 22 +++++++++
>   3 files changed, 98 insertions(+), 5 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp1y/lambda-init20.C
> 
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 4059e74bdb7..f583dedd6cf 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -11643,12 +11643,30 @@ finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
>             /* Fall through for fields that aren't bitfields.  */
>   	  gcc_fallthrough ();
>   
> -        case FUNCTION_DECL:
>           case VAR_DECL:
> -        case CONST_DECL:
> -        case PARM_DECL:
> -        case RESULT_DECL:
> -        case TEMPLATE_PARM_INDEX:
> +	  if (is_capture_proxy (expr))
> +	    {
> +	      if (is_normal_capture_proxy (expr))
> +		{
> +		  expr = DECL_CAPTURED_VARIABLE (expr);
> +		  type = TREE_TYPE (expr);
> +		  type = non_reference (type);
> +		}
> +	      else
> +		{
> +		  expr = DECL_VALUE_EXPR (expr);
> +		  gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
> +		  expr = TREE_OPERAND (expr, 1);
> +		  type = TREE_TYPE (expr);
> +		}
> +	      break;
> +	    }
> +	  /* Fall through.  */
> +	case FUNCTION_DECL:
> +	case CONST_DECL:
> +	case PARM_DECL:
> +	case RESULT_DECL:
> +	case TEMPLATE_PARM_INDEX:
>   	  expr = mark_type_use (expr);
>             type = TREE_TYPE (expr);
>   	  if (VAR_P (expr) && DECL_NTTP_OBJECT_P (expr))
> diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C
> new file mode 100644
> index 00000000000..a37b9db38d4
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C
> @@ -0,0 +1,53 @@
> +// PR c++/96917
> +// { dg-do compile { target c++14 } }
> +
> +int main() {
> +  int x = 0;
> +  int y = 0;
> +  const int cx = 0;
> +  const int cy = 0;
> +
> +  [x, &y, cx, &cy] {
> +    decltype(auto) a = x;
> +    using ty1 = int;
> +    using ty1 = decltype(x);
> +    using ty1 = decltype(a);
> +
> +    decltype(auto) b = y;
> +    using ty2 = int;
> +    using ty2 = decltype(y);
> +    using ty2 = decltype(b);
> +
> +    decltype(auto) ca = cx;
> +    using ty3 = const int;
> +    using ty3 = decltype(cx);
> +    using ty3 = decltype(ca);
> +
> +    decltype(auto) cb = cy;
> +    using ty4 = const int;
> +    using ty4 = decltype(cy);
> +    using ty4 = decltype(cb);
> +  };
> +
> +  [x=x, &y=y, cx=cx, &cy=cy] {
> +    decltype(auto) a = x;
> +    using ty1 = int;
> +    using ty1 = decltype(x);
> +    using ty1 = decltype(a);
> +
> +    decltype(auto) b = y;
> +    using ty2 = int&;
> +    using ty2 = decltype(y);
> +    using ty2 = decltype(b);
> +
> +    decltype(auto) ca = cx;
> +    using ty3 = int;
> +    using ty3 = decltype(cx);
> +    using ty3 = decltype(ca);
> +
> +    decltype(auto) cb = cy;
> +    using ty4 = const int&;
> +    using ty4 = decltype(cy);
> +    using ty4 = decltype(cb);
> +  };
> +}
> diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-init20.C b/gcc/testsuite/g++.dg/cpp1y/lambda-init20.C
> new file mode 100644
> index 00000000000..a06b77a664d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1y/lambda-init20.C
> @@ -0,0 +1,22 @@
> +// PR c++/79378
> +// { dg-do compile { target c++14 } }
> +
> +int main() {
> +  int x = 0;
> +  [x=x, &r=x] {
> +    using ty1 = int;
> +    using ty1 = decltype(x);
> +
> +    using ty2 = int&;
> +    using ty2 = decltype(r);
> +  };
> +
> +  const int cx = 0;
> +  [x=cx, &r=cx] {
> +    using ty1 = int;
> +    using ty1 = decltype(x);
> +
> +    using ty2 = const int&;
> +    using ty2 = decltype(r);
> +  };
> +}
diff mbox series

Patch

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 4059e74bdb7..f583dedd6cf 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -11643,12 +11643,30 @@  finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
           /* Fall through for fields that aren't bitfields.  */
 	  gcc_fallthrough ();
 
-        case FUNCTION_DECL:
         case VAR_DECL:
-        case CONST_DECL:
-        case PARM_DECL:
-        case RESULT_DECL:
-        case TEMPLATE_PARM_INDEX:
+	  if (is_capture_proxy (expr))
+	    {
+	      if (is_normal_capture_proxy (expr))
+		{
+		  expr = DECL_CAPTURED_VARIABLE (expr);
+		  type = TREE_TYPE (expr);
+		  type = non_reference (type);
+		}
+	      else
+		{
+		  expr = DECL_VALUE_EXPR (expr);
+		  gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
+		  expr = TREE_OPERAND (expr, 1);
+		  type = TREE_TYPE (expr);
+		}
+	      break;
+	    }
+	  /* Fall through.  */
+	case FUNCTION_DECL:
+	case CONST_DECL:
+	case PARM_DECL:
+	case RESULT_DECL:
+	case TEMPLATE_PARM_INDEX:
 	  expr = mark_type_use (expr);
           type = TREE_TYPE (expr);
 	  if (VAR_P (expr) && DECL_NTTP_OBJECT_P (expr))
diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C
new file mode 100644
index 00000000000..a37b9db38d4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C
@@ -0,0 +1,53 @@ 
+// PR c++/96917
+// { dg-do compile { target c++14 } }
+
+int main() {
+  int x = 0;
+  int y = 0;
+  const int cx = 0;
+  const int cy = 0;
+
+  [x, &y, cx, &cy] {
+    decltype(auto) a = x;
+    using ty1 = int;
+    using ty1 = decltype(x);
+    using ty1 = decltype(a);
+
+    decltype(auto) b = y;
+    using ty2 = int;
+    using ty2 = decltype(y);
+    using ty2 = decltype(b);
+
+    decltype(auto) ca = cx;
+    using ty3 = const int;
+    using ty3 = decltype(cx);
+    using ty3 = decltype(ca);
+
+    decltype(auto) cb = cy;
+    using ty4 = const int;
+    using ty4 = decltype(cy);
+    using ty4 = decltype(cb);
+  };
+
+  [x=x, &y=y, cx=cx, &cy=cy] {
+    decltype(auto) a = x;
+    using ty1 = int;
+    using ty1 = decltype(x);
+    using ty1 = decltype(a);
+
+    decltype(auto) b = y;
+    using ty2 = int&;
+    using ty2 = decltype(y);
+    using ty2 = decltype(b);
+
+    decltype(auto) ca = cx;
+    using ty3 = int;
+    using ty3 = decltype(cx);
+    using ty3 = decltype(ca);
+
+    decltype(auto) cb = cy;
+    using ty4 = const int&;
+    using ty4 = decltype(cy);
+    using ty4 = decltype(cb);
+  };
+}
diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-init20.C b/gcc/testsuite/g++.dg/cpp1y/lambda-init20.C
new file mode 100644
index 00000000000..a06b77a664d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/lambda-init20.C
@@ -0,0 +1,22 @@ 
+// PR c++/79378
+// { dg-do compile { target c++14 } }
+
+int main() {
+  int x = 0;
+  [x=x, &r=x] {
+    using ty1 = int;
+    using ty1 = decltype(x);
+
+    using ty2 = int&;
+    using ty2 = decltype(r);
+  };
+
+  const int cx = 0;
+  [x=cx, &r=cx] {
+    using ty1 = int;
+    using ty1 = decltype(x);
+
+    using ty2 = const int&;
+    using ty2 = decltype(r);
+  };
+}