[clang] [clang][Sema] Mark lambda in `noexcept` specifier of an abbreviated function template as dependent (PR #249469)

via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 6 11:01:46 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Manuel Sainz de Baranda y Goñi (redcode)

<details>
<summary>Changes</summary>

A lambda in the noexcept specifier of an abbreviated function template is not marked dependent, even when it captures a dependent entity:

```cpp
void f(auto&& fn) noexcept(noexcept([_ = fn] {})) {}
```

The closure type then reports `isDependentType() == false`. That is what was making `PaddingChecker` compute the layout of the dependent field and crash, which #<!-- -->232986 worked around with a guard in `PaddingChecker::shouldSkipDecl`.

This PR:

* Marks the lambda dependent in that case. In `Sema::ActOnLambdaExpressionAfterIntroducer`, it looks for a function declaration scope whose parameter is dependent (an abbreviated function template), keeping the existing handling of lambdas inside a requires expression. Lambda scopes themselves are skipped so a lambda in the default argument of a generic lambda is not made dependent (#<!-- -->172814).
* Removes the `PaddingChecker` guard.
* Updates the unit test documenting the Sema bug to expect `isDependentType() == true`, and adds a test for the explicit template variant.

Fixes #<!-- -->233102.


---
Full diff: https://github.com/llvm/llvm-project/pull/249469.diff


3 Files Affected:

- (modified) clang/lib/Sema/SemaLambda.cpp (+21-15) 
- (modified) clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp (-10) 
- (modified) clang/unittests/AST/LambdaDependencyTest.cpp (+15-5) 


``````````diff
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index 7c648108eacef..1a820c01a9c6e 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1150,21 +1150,16 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
 
   // Determine if we're within a context where we know that the lambda will
   // be dependent, because there are template parameters in scope.
-  //
-  // FIXME: A lambda in the noexcept specifier of an abbreviated function
-  // template (e.g. `auto&&`) is not marked dependent when it captures
-  // a dependent entity. See:
-  // * clang/unittests/AST/LambdaDependencyTest.cpp
-  // * https://github.com/llvm/llvm-project/issues/232874
-  // * https://github.com/llvm/llvm-project/issues/233102
-  // A temporary guard in PaddingChecker::shouldSkipDecl skips fields of
-  // dependent type as a workaround; remove it together with this FIXME
-  // once the bug is fixed.
   CXXRecordDecl::LambdaDependencyKind LambdaDependencyKind =
       CXXRecordDecl::LDK_Unknown;
   if (CurScope->getTemplateParamParent() != nullptr) {
     LambdaDependencyKind = CXXRecordDecl::LDK_AlwaysDependent;
   } else if (Scope *ParentScope = CurScope->getParent()) {
+    // A lambda can appear in the declarator of a function template (e.g. in a
+    // requires expression or a noexcept specifier) before the enclosing
+    // function template has been created, so its DeclContext is the enclosing
+    // non-dependent context.
+    //
     // Given a lambda defined inside a requires expression,
     //
     // struct S {
@@ -1180,11 +1175,22 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
            LookupScope->getEntity()->isRequiresExprBody())
       LookupScope = LookupScope->getParent();
 
-    if (LookupScope != ParentScope &&
-        LookupScope->isFunctionDeclarationScope() &&
-        llvm::any_of(LookupScope->decls(), [](Decl *D) {
-          return isa<ParmVarDecl>(D) &&
-                 cast<ParmVarDecl>(D)->getType()->isTemplateTypeParmType();
+    if (LookupScope->isFunctionDeclarationScope() &&
+        llvm::any_of(LookupScope->decls(), [&](Decl *D) {
+          if (!isa<ParmVarDecl>(D))
+            return false;
+          QualType T = cast<ParmVarDecl>(D)->getType();
+          // Inside a requires-expression body the parameters have already been
+          // converted to template parameters; in other declarator positions
+          // (e.g. a noexcept specifier of an abbreviated function template)
+          // they are still dependent. A lambda scope is itself a function
+          // declaration scope, so skip it: a lambda inside a generic lambda's
+          // parameter default argument must not become dependent just because
+          // the parameter is.
+          return LookupScope != ParentScope
+                     ? T->isTemplateTypeParmType()
+                     : (!(LookupScope->getFlags() & Scope::LambdaScope) &&
+                        T->isDependentType());
         }))
       LambdaDependencyKind = CXXRecordDecl::LDK_AlwaysDependent;
   }
diff --git a/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
index 6fdadde09ed3f..d3dd9a39ed0be 100644
--- a/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
@@ -181,16 +181,6 @@ class PaddingChecker : public Checker<check::ASTDecl<TranslationUnitDecl>> {
       if (Ty->isIncompleteArrayType())
         return true;
 
-      // Defensive backstop: a lambda closure type can be left non-dependent due
-      // to a bug, leaving the closure with a field of a bare dependent type
-      // whose layout cannot be computed. This workaround is temporary and
-      // should be removed together with the related FIXME in
-      // Sema::ActOnLambdaExpressionAfterIntroducer once the bug is fixed. See:
-      // * https://github.com/llvm/llvm-project/issues/232874
-      // * https://github.com/llvm/llvm-project/issues/233102
-      if (Ty->isDependentType())
-        return true;
-
       return false;
     };
 
diff --git a/clang/unittests/AST/LambdaDependencyTest.cpp b/clang/unittests/AST/LambdaDependencyTest.cpp
index a32b6606e8c15..0d1732c1357de 100644
--- a/clang/unittests/AST/LambdaDependencyTest.cpp
+++ b/clang/unittests/AST/LambdaDependencyTest.cpp
@@ -30,11 +30,21 @@ TEST(LambdaDependencyTest, NoexceptSpecifierOfAbbreviatedFunctionTemplate) {
                                 cxxRecordDecl(isLambda()));
   ASSERT_NE(nullptr, Closure);
 
-  // FIXME: This documents a flawed behavior: the closure type of a lambda that
-  // captures a dependent entity is not marked dependent, even though it should
-  // be.
-  // See: https://github.com/llvm/llvm-project/issues/233102
-  EXPECT_FALSE(Closure->isDependentType());
+  EXPECT_TRUE(Closure->isDependentType());
+}
+
+TEST(LambdaDependencyTest, NoexceptSpecifierOfExplicitFunctionTemplate) {
+  auto AST = buildASTFromCodeWithArgs(
+      "template <typename T> void f(T&& fn) noexcept(noexcept([_ = fn] {})) {}",
+      /*Args=*/{"-std=c++20"});
+  ASSERT_TRUE(AST);
+
+  FirstDeclMatcher<CXXRecordDecl> Matcher;
+  auto *Closure = Matcher.match(AST->getASTContext().getTranslationUnitDecl(),
+                                cxxRecordDecl(isLambda()));
+  ASSERT_NE(nullptr, Closure);
+
+  EXPECT_TRUE(Closure->isDependentType());
 }
 
 } // namespace

``````````

</details>


https://github.com/llvm/llvm-project/pull/249469


More information about the cfe-commits mailing list