Skip to content

Commit 6d00c42

Browse files
authored
[Clang] Do not skip over RequiresExprBodyDecl when creating lambdas (#147764)
When we create a lambda, we would skip over declaration contexts representing a require expression body, which would lead to wrong lookup. Note that I wasn't able to establish why the code in `Sema::createLambdaClosureType` was there to begin with (it's not exactly recent) The changes to mangling only ensure the status quo is preserved and do not attempt to address the known issues of mangling lambdas in require clauses. In particular the itanium mangling is consistent with Clang before this patch but differs from GCC's. Fixes #147650
1 parent 16f0462 commit 6d00c42

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ Bug Fixes to C++ Support
926926
- Fix a bug where private access specifier of overloaded function not respected. (#GH107629)
927927
- Correctly handle allocations in the condition of a ``if constexpr``.(#GH120197) (#GH134820)
928928
- Fixed a crash when handling invalid member using-declaration in C++20+ mode. (#GH63254)
929+
- Fix name lookup in lambda appearing in the body of a requires expression. (#GH147650)
929930
- Fix a crash when trying to instantiate an ambiguous specialization. (#GH51866)
930931
- Improved handling of variables with ``consteval`` constructors, to
931932
consistently treat the initializer as manifestly constant-evaluated.

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,9 @@ void CXXNameMangler::mangleNameWithAbiTags(GlobalDecl GD,
10881088
return;
10891089
}
10901090

1091+
while (DC->isRequiresExprBody())
1092+
DC = DC->getParent();
1093+
10911094
if (DC->isTranslationUnit() || isStdNamespace(DC)) {
10921095
// Check if we have a template.
10931096
const TemplateArgumentList *TemplateArgs = nullptr;

clang/lib/Sema/SemaLambda.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ Sema::createLambdaClosureType(SourceRange IntroducerRange, TypeSourceInfo *Info,
249249
unsigned LambdaDependencyKind,
250250
LambdaCaptureDefault CaptureDefault) {
251251
DeclContext *DC = CurContext;
252-
while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext()))
253-
DC = DC->getParent();
254252

255253
bool IsGenericLambda =
256254
Info && getGenericLambdaTemplateParameterList(getCurLambda(), *this);

clang/test/CodeGenCXX/mangle-requires.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,16 @@ template <typename T> void g(int n) requires requires (T m) {
3232
} {}
3333
// CHECK: define {{.*}}@_Z1gIiEviQrQT__XplfL0p_fp_E(
3434
template void g<int>(int);
35+
36+
37+
namespace GH147650 {
38+
39+
template <int> int b;
40+
template <int b>
41+
void f()
42+
requires requires { [] { (void)b; }; } {}
43+
void test() {
44+
f<42>();
45+
}
46+
// CHECK-LABEL:define {{.*}} void @"_ZN8GH1476501fILi42EEEvvQrqXLNS_3$_0EEE"()
47+
}

clang/test/CodeGenCXX/ms-mangle-requires.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,15 @@ void m_fn2() {
5757
} // namespace Regression2
5858

5959
}
60+
61+
namespace GH147650 {
62+
63+
template <int> int b;
64+
template <int b>
65+
void f()
66+
requires requires { [] { (void)b; }; } {}
67+
void test() {
68+
f<42>();
69+
}
70+
// CHECK-LABEL:define {{.*}} void @"??$f@$0CK@@GH147650@@YAXXZ"()
71+
}

clang/test/SemaTemplate/concepts-lambda.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,13 @@ void foo() {
340340
}
341341

342342
}
343+
344+
namespace GH147650 {
345+
template <int> int b;
346+
template <int b>
347+
void f()
348+
requires requires { [] { (void)b; static_assert(b == 42); }; } {}
349+
void test() {
350+
f<42>();
351+
}
352+
}

0 commit comments

Comments
 (0)