public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: decltype of (non-captured variable) [PR83167]
@ 2023-11-14 16:10 Patrick Palka
  2023-11-14 22:43 ` Jason Merrill
  0 siblings, 1 reply; 6+ messages in thread
From: Patrick Palka @ 2023-11-14 16:10 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

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.

	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&;
+  };
+}
-- 
2.43.0.rc1


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-12-04  3:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-14 16:10 [PATCH] c++: decltype of (non-captured variable) [PR83167] Patrick Palka
2023-11-14 22:43 ` Jason Merrill
2023-12-01 17:32   ` Patrick Palka
2023-12-01 20:33     ` Jason Merrill
2023-12-01 22:42       ` Patrick Palka
2023-12-04  3:49         ` Jason Merrill

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).