Skip to content

Conversation

Sirraide
Copy link
Member

This fixes a bug introduced by #84473: if a lambda’s type is type sugar (e.g. an AttributedType), we need to use getAs() instead of cast() to retrieve the FunctionProtoType.

@Sirraide Sirraide added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Apr 11, 2024
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Apr 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2024

@llvm/pr-subscribers-clang

Author: None (Sirraide)

Changes

This fixes a bug introduced by #84473: if a lambda’s type is type sugar (e.g. an AttributedType), we need to use getAs() instead of cast() to retrieve the FunctionProtoType.


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

4 Files Affected:

  • (modified) clang/lib/Sema/SemaExpr.cpp (+1-1)
  • (modified) clang/lib/Sema/SemaExprCXX.cpp (+1-1)
  • (modified) clang/test/SemaCXX/cxx2b-deducing-this.cpp (+12)
  • (modified) clang/test/SemaCXX/lambda-expressions.cpp (+9)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 45acbf197ea6b4..2d8b09df939db9 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -20743,7 +20743,7 @@ static void FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter(
       if (MD->getType().isNull())
         continue;
 
-      const auto *Ty = cast<FunctionProtoType>(MD->getType());
+      const auto *Ty = MD->getType()->getAs<FunctionProtoType>();
       if (!Ty || !MD->isExplicitObjectMemberFunction() ||
           !Ty->getParamType(0)->isDependentType())
         continue;
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 9822477260e592..58b3607b2fdc43 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -1489,7 +1489,7 @@ void Sema::MarkThisReferenced(CXXThisExpr *This) {
         if (MD->getType().isNull())
           return false;
 
-        const auto *Ty = cast<FunctionProtoType>(MD->getType());
+        const auto *Ty = MD->getType()->getAs<FunctionProtoType>();
         return Ty && MD->isExplicitObjectMemberFunction() &&
                Ty->getParamType(0)->isDependentType();
       }
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index c14c971afd2359..5f29a955e053c3 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -312,6 +312,18 @@ void TestMutationInLambda() {
       l1();
       l2();
     }
+
+    // Check that we don't crash if the lambda has type sugar.
+    const auto l15 = [=](this auto&&) [[clang::annotate_type("foo")]] [[clang::annotate_type("bar")]] {
+        return x;
+    };
+
+    const auto l16 = [=]() [[clang::annotate_type("foo")]] [[clang::annotate_type("bar")]] {
+        return x;
+    };
+
+    l15();
+    l16();
 }
 
 struct Over_Call_Func_Example {
diff --git a/clang/test/SemaCXX/lambda-expressions.cpp b/clang/test/SemaCXX/lambda-expressions.cpp
index 151d74f21d64dc..6be338064452ed 100644
--- a/clang/test/SemaCXX/lambda-expressions.cpp
+++ b/clang/test/SemaCXX/lambda-expressions.cpp
@@ -762,3 +762,12 @@ template auto t::operator()<int>(int a) const; // expected-note {{in instantiati
 
 }
 #endif
+
+namespace GH84473_bug {
+void f1() {
+  int b;
+  (void) [=] [[gnu::regcall]] () { // expected-warning {{an attribute specifier sequence in this position is a C++23 extension}}
+    (void) b;
+  };
+}
+}

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooof, yeah, good catch. I should have caught that in the review, I've fallen for that one before :)

@Sirraide
Copy link
Member Author

Ooof, yeah, good catch. I should have caught that in the review, I've fallen for that one before :)

I was just now made aware of the fact that this asserts and the only reason I found the problem fairly quickly is because I happen to have a pr open at the moment that deals w/ type sugar on lambdas.

@Sirraide Sirraide merged commit 505a9ae into llvm:main Apr 12, 2024
@Sirraide Sirraide deleted the dependent-captures-fix branch April 12, 2024 04:50
Sirraide added a commit that referenced this pull request Apr 12, 2024
#88428 ended up breaking CI because it included a test that uses the
`regcall` calling convention, which isn’t supported on all targets; I’ve
moved it into a separate file that sets the triple.
erikolofsson pushed a commit to Malterlib/llvm-project that referenced this pull request May 11, 2024
…m#88428)

This fixes a bug introduced by llvm#84473: if a lambda’s type is type sugar
(e.g. an `AttributedType`), we need to use `getAs()` instead of `cast()`
to retrieve the `FunctionProtoType`.
erikolofsson pushed a commit to Malterlib/llvm-project that referenced this pull request Jun 9, 2024
…m#88428)

This fixes a bug introduced by llvm#84473: if a lambda’s type is type sugar
(e.g. an `AttributedType`), we need to use `getAs()` instead of `cast()`
to retrieve the `FunctionProtoType`.
erikolofsson pushed a commit to Malterlib/llvm-project that referenced this pull request Aug 7, 2024
…m#88428)

This fixes a bug introduced by llvm#84473: if a lambda’s type is type sugar
(e.g. an `AttributedType`), we need to use `getAs()` instead of `cast()`
to retrieve the `FunctionProtoType`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants