Skip to content

Commit 505a9ae

Browse files
authored
[Clang] Look through type sugar when accessing FunctionProtoType (#88428)
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`.
1 parent b45c9c3 commit 505a9ae

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
@@ -20704,7 +20704,7 @@ static void FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter(
2070420704
if (MD->getType().isNull())
2070520705
continue;
2070620706

20707-
const auto *Ty = cast<FunctionProtoType>(MD->getType());
20707+
const auto *Ty = MD->getType()->getAs<FunctionProtoType>();
2070820708
if (!Ty || !MD->isExplicitObjectMemberFunction() ||
2070920709
!Ty->getParamType(0)->isDependentType())
2071020710
continue;

clang/lib/Sema/SemaExprCXX.cpp

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

1491-
const auto *Ty = cast<FunctionProtoType>(MD->getType());
1491+
const auto *Ty = MD->getType()->getAs<FunctionProtoType>();
14921492
return Ty && MD->isExplicitObjectMemberFunction() &&
14931493
Ty->getParamType(0)->isDependentType();
14941494
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,18 @@ void TestMutationInLambda() {
312312
l1();
313313
l2();
314314
}
315+
316+
// Check that we don't crash if the lambda has type sugar.
317+
const auto l15 = [=](this auto&&) [[clang::annotate_type("foo")]] [[clang::annotate_type("bar")]] {
318+
return x;
319+
};
320+
321+
const auto l16 = [=]() [[clang::annotate_type("foo")]] [[clang::annotate_type("bar")]] {
322+
return x;
323+
};
324+
325+
l15();
326+
l16();
315327
}
316328

317329
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
@@ -762,3 +762,12 @@ template auto t::operator()<int>(int a) const; // expected-note {{in instantiati
762762

763763
}
764764
#endif
765+
766+
namespace GH84473_bug {
767+
void f1() {
768+
int b;
769+
(void) [=] [[gnu::regcall]] () { // expected-warning {{an attribute specifier sequence in this position is a C++23 extension}}
770+
(void) b;
771+
};
772+
}
773+
}

0 commit comments

Comments
 (0)