[clang] [clang] return type not correctly deduced for discarded lambdas (PR #153921)

Oliver Hunt via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 15 21:10:40 PDT 2025


https://github.com/ojhunt created https://github.com/llvm/llvm-project/pull/153921

The early return for lamda expressions with deduced return types in Sema::ActOnCapScopeReturnStmt meant that we were not actually perform the required return type deduction for such lambdas when in a discarded context.

This PR removes that early return allowing the existing return type deduction steps to be performed.

Fixes #GH153884

>From eca813498a82b41252a709e2fe619b57ef1312b7 Mon Sep 17 00:00:00 2001
From: Oliver Hunt <oliver at apple.com>
Date: Fri, 15 Aug 2025 21:04:07 -0700
Subject: [PATCH] [clang] return type not correctly deduced for discarded
 lambdas

The early return for lamda expressions with deduced return types
in Sema::ActOnCapScopeReturnStmt meant that we were not actually
perform the required return type deduction for such lambdas when
in a discarded context.

This PR removes that early return allowing the existing return
type deduction steps to be performed.

Fixes #GH153884
---
 clang/lib/Sema/SemaStmt.cpp                      |  2 --
 .../CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp     | 16 ++++++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index bc1ddb80961a2..4bde0e54568dd 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3533,8 +3533,6 @@ StmtResult Sema::ActOnCapScopeReturnStmt(SourceLocation ReturnLoc,
         return StmtError();
       RetValExp = ER.get();
     }
-    return ReturnStmt::Create(Context, ReturnLoc, RetValExp,
-                              /* NRVOCandidate=*/nullptr);
   }
 
   if (HasDeducedReturnType) {
diff --git a/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp b/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp
index abb42447d3e0b..05830de9891fe 100644
--- a/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.if/p2.cpp
@@ -239,5 +239,21 @@ void f2() {
 
 }
 
+namespace GH153884 {
+  bool f1() {
+    auto f = [](auto) { return true; };
+    if constexpr (0)
+      return f(1);
+    return false;
+  }
+  bool f2() {
+    auto f = [](auto x) { if (x) return 1.5; else return "wat"; };
+    // expected-error at -1 {{'auto' in return type deduced as 'const char *' here but deduced as 'double' in earlier return statement}}
+    if constexpr (0)
+      return f(1);
+    // expected-note at -1 {{in instantiation of function template specialization 'GH153884::f2()}}
+    return false;
+  }
+}
 
 #endif



More information about the cfe-commits mailing list