lime created this revision.
lime added reviewers: cor3ntin, shafik, erichkeane, aaron.ballman, ychen, 
clang-language-wg.
lime added a project: clang.
Herald added a project: All.
lime requested review of this revision.
Herald added a subscriber: cfe-commits.

Unevaluated lambdas can cause the instantiator pumping dependent diagnostics 
from independent declaration contexts, and triggering the assertion in the 
following iteration.

This patch adds the check of the dependency before pumping diagnostics and 
fixes issues:
https://github.com/llvm/llvm-project/issues/57155
https://github.com/llvm/llvm-project/issues/57170


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140614

Files:
  clang/lib/Sema/SemaTemplateInstantiate.cpp
  clang/test/SemaCXX/lambda-unevaluated.cpp


Index: clang/test/SemaCXX/lambda-unevaluated.cpp
===================================================================
--- clang/test/SemaCXX/lambda-unevaluated.cpp
+++ clang/test/SemaCXX/lambda-unevaluated.cpp
@@ -122,6 +122,22 @@
 static_assert(!__is_same(decltype(foo<int>), void));
 } // namespace GH51641
 
+namespace GH57155 {
+auto foo(int t) {
+  int(*f)(int) = [](auto t) -> decltype([=] { return t; } ()) { return t; };
+  return f;
+}
+} // namespace GH57155
+
+namespace GH57170 {
+int(*f)(int) = [](auto t) -> decltype([] {
+    return 0;
+  } ()
+){
+  return t;
+};
+} // namespace GH57170
+
 namespace StaticLambdas {
 template <auto> struct Nothing {};
 Nothing<[]() static { return 0; }()> nothing;
Index: clang/lib/Sema/SemaTemplateInstantiate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1230,8 +1230,10 @@
 
       // We recreated a local declaration, but not by instantiating it. There
       // may be pending dependent diagnostics to produce.
-      if (auto *DC = dyn_cast<DeclContext>(Old))
-        SemaRef.PerformDependentDiagnostics(DC, TemplateArgs);
+      if (auto *DC = dyn_cast<DeclContext>(Old)) {
+        if (DC->isDependentContext())
+          SemaRef.PerformDependentDiagnostics(DC, TemplateArgs);
+      }
     }
 
     /// Transform the definition of the given declaration by


Index: clang/test/SemaCXX/lambda-unevaluated.cpp
===================================================================
--- clang/test/SemaCXX/lambda-unevaluated.cpp
+++ clang/test/SemaCXX/lambda-unevaluated.cpp
@@ -122,6 +122,22 @@
 static_assert(!__is_same(decltype(foo<int>), void));
 } // namespace GH51641
 
+namespace GH57155 {
+auto foo(int t) {
+  int(*f)(int) = [](auto t) -> decltype([=] { return t; } ()) { return t; };
+  return f;
+}
+} // namespace GH57155
+
+namespace GH57170 {
+int(*f)(int) = [](auto t) -> decltype([] {
+    return 0;
+  } ()
+){
+  return t;
+};
+} // namespace GH57170
+
 namespace StaticLambdas {
 template <auto> struct Nothing {};
 Nothing<[]() static { return 0; }()> nothing;
Index: clang/lib/Sema/SemaTemplateInstantiate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1230,8 +1230,10 @@
 
       // We recreated a local declaration, but not by instantiating it. There
       // may be pending dependent diagnostics to produce.
-      if (auto *DC = dyn_cast<DeclContext>(Old))
-        SemaRef.PerformDependentDiagnostics(DC, TemplateArgs);
+      if (auto *DC = dyn_cast<DeclContext>(Old)) {
+        if (DC->isDependentContext())
+          SemaRef.PerformDependentDiagnostics(DC, TemplateArgs);
+      }
     }
 
     /// Transform the definition of the given declaration by
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D140614: [C++20] Check ... Liming Liu via Phabricator via cfe-commits

Reply via email to