[gcc r17-3291] c++: constexpr class access in generic lambda [PR126754]

Jason Merrill via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:654eb1f70643a2c57b3c6a31c250c1213a23aa4f

commit r17-3291-g654eb1f70643a2c57b3c6a31c250c1213a23aa4f
Author: Jason Merrill <[email protected]>
Date:   Tue Aug 11 16:12:10 2026 -0400

    c++: constexpr class access in generic lambda [PR126754]
    
    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.
    Let's not do the latter in a template.
    
            PR c++/126754
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (cxx_eval_outermost_constant_expr): Don't
            wrap in TARGET_EXPR when processing_template_decl.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1y/constexpr-lambda2.C: New test.

Diff:
---
 gcc/cp/constexpr.cc                            |  3 +++
 gcc/testsuite/g++.dg/cpp1y/constexpr-lambda2.C | 11 +++++++++++
 2 files changed, 14 insertions(+)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index e16c878d7258..c9ebc0027b7d 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -11288,6 +11288,9 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
 	/* Don't add a TARGET_EXPR if our argument didn't have one.  */;
       else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
 	r = get_target_expr (r);
+      else if (processing_template_decl)
+	/* Don't insert TARGET_EXPR in template trees.  */
+	return r;
       else
 	{
 	  r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
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 000000000000..1ad31f83fc66
--- /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);
+}
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.