[pushed 16] c++: constexpr class access in generic lambda [PR126754]

Jason Merrill <[email protected]>
Newsgroups gmane.comp.gcc.patches
Message-ID <[email protected]>
Tested x86_64-pc-linux-gnu, applying to 16 branch.

-- 8< --

We don't want to see TARGET_EXPR in template trees because it's an
implementation detail, not a representation of the source.  But constant
evaluation normally represents a constant class value with a TARGET_EXPR.
For GCC 16 let's duplicate the default case handling.

	PR c++/126754

gcc/cp/ChangeLog:

	* pt.cc (tsubst_expr) <case TARGET_EXPR>: Only unreachable
	when checking.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1y/constexpr-lambda2.C: New test.
---
 gcc/cp/pt.cc                                   |  4 +++-
 gcc/testsuite/g++.dg/cpp1y/constexpr-lambda2.C | 11 +++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-lambda2.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 9fba230846a..0f52a84fb03 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -23187,7 +23187,9 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
     case TARGET_EXPR:
       /* TARGET_EXPR represents temporary objects and should not appear in
 	 templated trees.  */
-      gcc_unreachable ();
+      if (flag_checking)
+	gcc_unreachable ();
+      RETURN (t);
 
     case OFFSET_REF:
       {
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda2.C
new file mode 100644
index 00000000000..1ad31f83fc6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda2.C
@@ -0,0 +1,11 @@
+// PR c++/126754
+// { dg-do compile { target c++14 } }
+
+struct S { int i; };
+int main() {
+  constexpr S a{1};
+  const auto f = [](auto b) {
+    return 1 - a.i;  // this expression must be independent of b in order to trigger the crash
+  };
+  return f(0);
+}

base-commit: 3cdb74f43678a97b01f1de0cfe9eb218d73460f7
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.