Skip to content

Commit f3fbe06

Browse files
Sirraideerikolofsson
authored andcommitted
[Clang] Look through type sugar when accessing FunctionProtoType (llvm#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`.
1 parent c2ee188 commit f3fbe06

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20689,7 +20689,7 @@ static void FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter(
2068920689
if (MD->getType().isNull())
2069020690
continue;
2069120691

20692-
const auto *Ty = cast<FunctionProtoType>(MD->getType());
20692+
const auto *Ty = MD->getType()->getAs<FunctionProtoType>();
2069320693
if (!Ty || !MD->isExplicitObjectMemberFunction() ||
2069420694
!Ty->getParamType(0)->isDependentType())
2069520695
continue;

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ void Sema::MarkThisReferenced(CXXThisExpr *This) {
14671467
if (MD->getType().isNull())
14681468
return false;
14691469

1470-
const auto *Ty = cast<FunctionProtoType>(MD->getType());
1470+
const auto *Ty = MD->getType()->getAs<FunctionProtoType>();
14711471
return Ty && MD->isExplicitObjectMemberFunction() &&
14721472
Ty->getParamType(0)->isDependentType();
14731473
}

clang/test/SemaCXX/cxx2b-deducing-this.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ void TestMutationInLambda() {
308308
l1();
309309
l2();
310310
}
311+
312+
// Check that we don't crash if the lambda has type sugar.
313+
const auto l15 = [=](this auto&&) [[clang::annotate_type("foo")]] [[clang::annotate_type("bar")]] {
314+
return x;
315+
};
316+
317+
const auto l16 = [=]() [[clang::annotate_type("foo")]] [[clang::annotate_type("bar")]] {
318+
return x;
319+
};
320+
321+
l15();
322+
l16();
311323
}
312324

313325
struct Over_Call_Func_Example {

clang/test/SemaCXX/lambda-expressions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,3 +732,12 @@ void GH67492() {
732732
constexpr auto test = 42;
733733
auto lambda = (test, []() noexcept(true) {});
734734
}
735+
736+
namespace GH84473_bug {
737+
void f1() {
738+
int b;
739+
(void) [=] [[gnu::regcall]] () { // expected-warning {{an attribute specifier sequence in this position is a C++23 extension}}
740+
(void) b;
741+
};
742+
}
743+
}

0 commit comments

Comments
 (0)