Skip to content

Commit 22aa368

Browse files
cor3ntinAaronBallman
authored andcommitted
[C++20] Support for lambdas in unevaluated context
Partially implement P0315R4. This patch allow lambda in unevaluated context. It does not implement temp.deduct/9.
1 parent 5cf2753 commit 22aa368

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

clang/lib/Sema/SemaConcept.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ class LogicalBinOp {
4343
LHS = BO->getLHS();
4444
RHS = BO->getRHS();
4545
} else if (auto *OO = dyn_cast<CXXOperatorCallExpr>(E)) {
46-
Op = OO->getOperator();
47-
LHS = OO->getArg(0);
48-
RHS = OO->getArg(1);
46+
// If OO is not || or && it might not have exactly 2 arguments.
47+
if (OO->getNumArgs() == 2) {
48+
Op = OO->getOperator();
49+
LHS = OO->getArg(0);
50+
RHS = OO->getArg(1);
51+
}
4952
}
5053
}
5154

clang/lib/Sema/SemaExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16722,8 +16722,10 @@ void Sema::PopExpressionEvaluationContext() {
1672216722

1672316723
if (!Rec.Lambdas.empty()) {
1672416724
using ExpressionKind = ExpressionEvaluationContextRecord::ExpressionKind;
16725-
if (Rec.ExprContext == ExpressionKind::EK_TemplateArgument || Rec.isUnevaluated() ||
16726-
(Rec.isConstantEvaluated() && !getLangOpts().CPlusPlus17)) {
16725+
if (!getLangOpts().CPlusPlus20 &&
16726+
(Rec.ExprContext == ExpressionKind::EK_TemplateArgument ||
16727+
Rec.isUnevaluated() ||
16728+
(Rec.isConstantEvaluated() && !getLangOpts().CPlusPlus17))) {
1672716729
unsigned D;
1672816730
if (Rec.isUnevaluated()) {
1672916731
// C++11 [expr.prim.lambda]p2:

clang/test/SemaCXX/anonymous-struct.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ typedef struct // expected-warning {{anonymous non-C-compatible type given name
4949
: B { // expected-note {{type is not C-compatible due to this base class}}
5050
} C; // expected-note {{type is given name 'C' for linkage purposes by this typedef declaration}}
5151

52-
#if __cplusplus > 201703L
52+
#if __cplusplus > 201703L && __cplusplus < 202002L
5353
typedef struct { // expected-warning {{anonymous non-C-compatible type given name for linkage purposes by typedef declaration; add a tag name here}}
5454
static_assert([]{ return true; }()); // expected-note {{type is not C-compatible due to this lambda expression}}
5555
} Lambda1; // expected-note {{type is given name 'Lambda1' for linkage purposes by this typedef declaration}}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %clang_cc1 -std=c++20 %s -verify
2+
3+
4+
template <auto> struct Nothing {};
5+
Nothing<[]() { return 0; }()> nothing;
6+
7+
template <typename> struct NothingT {};
8+
Nothing<[]() { return 0; }> nothingT;
9+
10+
template <typename T>
11+
concept True = [] { return true; }();
12+
static_assert(True<int>);
13+
14+
static_assert(sizeof([] { return 0; }));
15+
static_assert(sizeof([] { return 0; }()));
16+
17+
void f() noexcept(noexcept([] { return 0; }()));
18+
19+
using a = decltype([] { return 0; });
20+
using b = decltype([] { return 0; }());
21+
using c = decltype([]() noexcept(noexcept([] { return 0; }())) { return 0; });
22+
using d = decltype(sizeof([] { return 0; }));
23+
24+
template <auto T>
25+
int unique_test1();
26+
static_assert(&unique_test1<[](){}> != &unique_test1<[](){}>);
27+
28+
template <class T>
29+
auto g(T) -> decltype([]() { T::invalid; } ());
30+
auto e = g(0); // expected-error{{no matching function for call}}
31+
// expected-note@-2 {{substitution failure}}

clang/www/cxx_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ <h2 id="cxx20">C++20 implementation status</h2>
10111011
<tr>
10121012
<td>Lambdas in unevaluated contexts</td>
10131013
<td><a href="https://wg21.link/p0315r4">P0315R4</a></td>
1014-
<td class="none" align="center">No</td>
1014+
<td class="partial" align="center">Clang 13</td>
10151015
</tr>
10161016
<!-- Jacksonville papers -->
10171017
<tr>

0 commit comments

Comments
 (0)