Lambdas have a deleted default constructor and a deleted copy
assignment operator, per C++ [expr.prim.lambda]p19. Make it so. llvm-svn: 150345
This commit is contained in:
parent
0501c63609
commit
1a22d2889b
|
@ -635,6 +635,7 @@ DeclContext *Sema::getFunctionLevelDeclContext() {
|
||||||
if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) {
|
if (isa<BlockDecl>(DC) || isa<EnumDecl>(DC)) {
|
||||||
DC = DC->getParent();
|
DC = DC->getParent();
|
||||||
} else if (isa<CXXMethodDecl>(DC) &&
|
} else if (isa<CXXMethodDecl>(DC) &&
|
||||||
|
cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call &&
|
||||||
cast<CXXRecordDecl>(DC->getParent())->isLambda()) {
|
cast<CXXRecordDecl>(DC->getParent())->isLambda()) {
|
||||||
DC = DC->getParent()->getParent();
|
DC = DC->getParent()->getParent();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4340,6 +4340,13 @@ bool Sema::ShouldDeleteSpecialMember(CXXMethodDecl *MD, CXXSpecialMember CSM) {
|
||||||
switch (CSM) {
|
switch (CSM) {
|
||||||
case CXXDefaultConstructor:
|
case CXXDefaultConstructor:
|
||||||
IsConstructor = true;
|
IsConstructor = true;
|
||||||
|
|
||||||
|
// C++11 [expr.lambda.prim]p19:
|
||||||
|
// The closure type associated with a lambda-expression has a
|
||||||
|
// deleted (8.4.3) default constructor.
|
||||||
|
if (RD->isLambda())
|
||||||
|
return true;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case CXXCopyConstructor:
|
case CXXCopyConstructor:
|
||||||
IsConstructor = true;
|
IsConstructor = true;
|
||||||
|
@ -4621,6 +4628,12 @@ bool Sema::ShouldDeleteCopyAssignmentOperator(CXXMethodDecl *MD) {
|
||||||
if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl())
|
if (!LangOpts.CPlusPlus0x || RD->isInvalidDecl())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// C++11 [expr.lambda.prim]p19:
|
||||||
|
// The closure type associated with a lambda-expression has a
|
||||||
|
// [...] deleted copy assignment operator.
|
||||||
|
if (RD->isLambda())
|
||||||
|
return true;
|
||||||
|
|
||||||
SourceLocation Loc = MD->getLocation();
|
SourceLocation Loc = MD->getLocation();
|
||||||
|
|
||||||
// Do access control from the constructor
|
// Do access control from the constructor
|
||||||
|
|
|
@ -406,6 +406,13 @@ ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc,
|
||||||
CallOperator->setType(FunctionTy);
|
CallOperator->setType(FunctionTy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// C++ [expr.prim.lambda]p7:
|
||||||
|
// The lambda-expression's compound-statement yields the
|
||||||
|
// function-body (8.4) of the function call operator [...].
|
||||||
|
ActOnFinishFunctionBody(CallOperator, Body, /*IsInstantation=*/false);
|
||||||
|
CallOperator->setLexicalDeclContext(Class);
|
||||||
|
Class->addDecl(CallOperator);
|
||||||
|
|
||||||
// C++11 [expr.prim.lambda]p6:
|
// C++11 [expr.prim.lambda]p6:
|
||||||
// The closure type for a lambda-expression with no lambda-capture
|
// The closure type for a lambda-expression with no lambda-capture
|
||||||
// has a public non-virtual non-explicit const conversion function
|
// has a public non-virtual non-explicit const conversion function
|
||||||
|
@ -450,15 +457,8 @@ ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc,
|
||||||
Class->addDecl(Conversion);
|
Class->addDecl(Conversion);
|
||||||
}
|
}
|
||||||
|
|
||||||
// C++ [expr.prim.lambda]p7:
|
|
||||||
// The lambda-expression's compound-statement yields the
|
|
||||||
// function-body (8.4) of the function call operator [...].
|
|
||||||
ActOnFinishFunctionBody(CallOperator, Body, /*IsInstantation=*/false);
|
|
||||||
|
|
||||||
// Finalize the lambda class.
|
// Finalize the lambda class.
|
||||||
SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end());
|
SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end());
|
||||||
CallOperator->setLexicalDeclContext(Class);
|
|
||||||
Class->addDecl(CallOperator);
|
|
||||||
ActOnFields(0, Class->getLocation(), Class, Fields,
|
ActOnFields(0, Class->getLocation(), Class, Fields,
|
||||||
SourceLocation(), SourceLocation(), 0);
|
SourceLocation(), SourceLocation(), 0);
|
||||||
CheckCompletedCXXClass(Class);
|
CheckCompletedCXXClass(Class);
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
// RUN: %clang_cc1 -std=c++11 %s -Wunused -verify
|
||||||
|
|
||||||
|
struct MoveOnly {
|
||||||
|
MoveOnly(MoveOnly&&);
|
||||||
|
MoveOnly(const MoveOnly&);
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T> T &&move(T&);
|
||||||
|
void test_special_member_functions(MoveOnly mo, int i) {
|
||||||
|
// FIXME: terrible note
|
||||||
|
auto lambda1 = [i]() { }; // expected-note{{function has been explicitly marked deleted here}} \
|
||||||
|
// expected-note{{the implicit copy assignment operator}} \
|
||||||
|
// expected-note{{the implicit move assignment operator}} \
|
||||||
|
|
||||||
|
// Default constructor
|
||||||
|
decltype(lambda1) lambda2; // expected-error{{call to deleted constructor}}
|
||||||
|
|
||||||
|
// Copy assignment operator
|
||||||
|
lambda1 = lambda1; // expected-error{{overload resolution selected deleted operator '='}}
|
||||||
|
|
||||||
|
// Move assignment operator
|
||||||
|
lambda1 = move(lambda1);
|
||||||
|
|
||||||
|
// Copy constructor
|
||||||
|
decltype(lambda1) lambda3 = lambda1;
|
||||||
|
decltype(lambda1) lambda4(lambda1);
|
||||||
|
|
||||||
|
// Move constructor
|
||||||
|
decltype(lambda1) lambda5 = move(lambda1);
|
||||||
|
decltype(lambda1) lambda6(move(lambda1));
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
// RUN: %clang_cc1 -std=c++11 %s -Wunused -verify
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void destroy(T* ptr) {
|
||||||
|
ptr->~T();
|
||||||
|
(*ptr).~T();
|
||||||
|
}
|
||||||
|
|
||||||
|
void destructor() {
|
||||||
|
auto lambda = []{};
|
||||||
|
destroy(&lambda);
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ void test_attributes() {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct bogus_override_if_virtual : public T {
|
struct bogus_override_if_virtual : public T {
|
||||||
|
bogus_override_if_virtual() : T(*(T*)0) { }
|
||||||
int operator()() const;
|
int operator()() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -45,9 +45,11 @@ void test_capture_constness(int i, const int ic) {
|
||||||
|
|
||||||
struct S1 {
|
struct S1 {
|
||||||
int x, y;
|
int x, y;
|
||||||
|
S1 &operator=(int*);
|
||||||
int operator()(int);
|
int operator()(int);
|
||||||
void f() {
|
void f() {
|
||||||
[&]()->int {
|
[&]()->int {
|
||||||
|
S1 &s1 = operator=(&this->x);
|
||||||
return operator()(this->x + y);
|
return operator()(this->x + y);
|
||||||
}();
|
}();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue