[clang] Improve error message for invalid lambda captures (PR #94865)

via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 8 12:11:12 PDT 2024


https://github.com/CedricSwa created https://github.com/llvm/llvm-project/pull/94865

close issue #94764

>From 012849c5410960001ca5bbcb90ea2cf4a661b840 Mon Sep 17 00:00:00 2001
From: Cedric Schwarzer <Cedric.Schwarzer at stud.uni-hannover.de>
Date: Sat, 8 Jun 2024 17:52:02 +0200
Subject: [PATCH] Improve error message for invalid lambda captures

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++
 clang/lib/Sema/SemaLambda.cpp                    | 7 ++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9f0b6f5a36389..fdf4409125c00 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -8173,6 +8173,8 @@ let CategoryName = "Lambda Issue" in {
     "'&' must precede a capture when the capture default is '='">;
   def err_capture_does_not_name_variable : Error<
     "%0 in capture list does not name a variable">;
+  def err_capture_class_member_does_not_name_variable : Error<
+    "class member %0 cannot appear in capture list as it is not a variable">;
   def err_capture_non_automatic_variable : Error<
     "%0 cannot be captured because it does not have automatic storage "
     "duration">;
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index e9476a0c93c5d..79c10c3bad6d7 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1246,7 +1246,12 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
 
       if (auto *BD = R.getAsSingle<BindingDecl>())
         Var = BD;
-      else
+      else if (auto *FD = R.getAsSingle<FieldDecl>()) {
+        Var = R.getAsSingle<VarDecl>();
+        Diag(C->Loc, diag::err_capture_class_member_does_not_name_variable)
+            << C->Id;
+        continue;
+      } else
         Var = R.getAsSingle<VarDecl>();
       if (Var && DiagnoseUseOfDecl(Var, C->Loc))
         continue;



More information about the cfe-commits mailing list