Skip to content

Commit 5518b46

Browse files
authored
[clang][ASTImporter] Remove trailing return testing on lambda proto (#101031)
Lambdas without trailing return could also have return type defined inside its body. This fixes crashes (infinite recursion) on lambda expr without parameters (no parentheses).
1 parent 15895da commit 5518b46

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3717,21 +3717,18 @@ bool ASTNodeImporter::hasReturnTypeDeclaredInside(FunctionDecl *D) {
37173717
const auto *FromFPT = FromTy->getAs<FunctionProtoType>();
37183718
assert(FromFPT && "Must be called on FunctionProtoType");
37193719

3720-
auto IsCXX11LambdaWithouTrailingReturn = [&]() {
3720+
auto IsCXX11Lambda = [&]() {
37213721
if (Importer.FromContext.getLangOpts().CPlusPlus14) // C++14 or later
37223722
return false;
37233723

3724-
if (FromFPT->hasTrailingReturn())
3725-
return false;
3726-
37273724
if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
37283725
return cast<CXXRecordDecl>(MD->getDeclContext())->isLambda();
37293726

37303727
return false;
37313728
};
37323729

37333730
QualType RetT = FromFPT->getReturnType();
3734-
if (isa<AutoType>(RetT.getTypePtr()) || IsCXX11LambdaWithouTrailingReturn()) {
3731+
if (isa<AutoType>(RetT.getTypePtr()) || IsCXX11Lambda()) {
37353732
FunctionDecl *Def = D->getDefinition();
37363733
IsTypeDeclaredInsideVisitor Visitor(Def ? Def : D);
37373734
return Visitor.CheckType(RetT);

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6738,6 +6738,23 @@ TEST_P(ASTImporterOptionSpecificTestBase,
67386738
EXPECT_TRUE(ToLambda);
67396739
}
67406740

6741+
TEST_P(ASTImporterOptionSpecificTestBase,
6742+
ReturnTypeDeclaredInsideOfCXX11LambdaWithTrailingReturn) {
6743+
Decl *From, *To;
6744+
std::tie(From, To) = getImportedDecl(
6745+
R"(
6746+
void foo() {
6747+
(void) [] {
6748+
struct X {};
6749+
return X();
6750+
};
6751+
}
6752+
)",
6753+
Lang_CXX11, "", Lang_CXX11, "foo"); // c++11 only
6754+
auto *ToLambda = FirstDeclMatcher<LambdaExpr>().match(To, lambdaExpr());
6755+
EXPECT_TRUE(ToLambda);
6756+
}
6757+
67416758
TEST_P(ASTImporterOptionSpecificTestBase, LambdaInFunctionParam) {
67426759
Decl *FromTU = getTuDecl(
67436760
R"(

0 commit comments

Comments
 (0)