From: Richard Smith Date: Tue, 25 Jun 2013 18:46:26 +0000 (+0000) Subject: More of N3652: don't add an implicit 'const' to 'constexpr' member functions when... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=db2fe739e5aac227d85d149efc9036696997a26f;p=clang More of N3652: don't add an implicit 'const' to 'constexpr' member functions when checking for overloads in C++1y. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@184865 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 29644b40b7..d4d08b4bff 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -1080,7 +1080,8 @@ static bool shouldTryToOverload(Sema &S, FunctionDecl *New, FunctionDecl *Old, // or non-static member function). Add it now, on the assumption that this // is a redeclaration of OldMethod. unsigned NewQuals = NewMethod->getTypeQualifiers(); - if (NewMethod->isConstexpr() && !isa(NewMethod)) + if (!S.getLangOpts().CPlusPlus1y && NewMethod->isConstexpr() && + !isa(NewMethod)) NewQuals |= Qualifiers::Const; if (OldMethod->getTypeQualifiers() != NewQuals) return true; diff --git a/test/SemaCXX/constant-expression-cxx11.cpp b/test/SemaCXX/constant-expression-cxx11.cpp index 4e2b7edb78..ca029d9d12 100644 --- a/test/SemaCXX/constant-expression-cxx11.cpp +++ b/test/SemaCXX/constant-expression-cxx11.cpp @@ -1670,3 +1670,27 @@ namespace StmtExpr { }); } } + +namespace VirtualFromBase { + struct S1 { + virtual int f() const; + }; + struct S2 { + virtual int f(); + }; + template struct X : T { + constexpr X() {} + double d = 0.0; + constexpr int f() { return sizeof(T); } // expected-warning {{will not be implicitly 'const' in C++1y}} + }; + + // Virtual f(), not OK. + constexpr X> xxs1; + constexpr X *p = const_cast>*>(&xxs1); + static_assert(p->f() == sizeof(X), ""); // expected-error {{constant expression}} expected-note {{virtual function call}} + + // Non-virtual f(), OK. + constexpr X> xxs2; + constexpr X *q = const_cast>*>(&xxs2); + static_assert(q->f() == sizeof(S2), ""); +} diff --git a/test/SemaCXX/constant-expression-cxx1y.cpp b/test/SemaCXX/constant-expression-cxx1y.cpp index 6043a1fffc..0d099c2120 100644 --- a/test/SemaCXX/constant-expression-cxx1y.cpp +++ b/test/SemaCXX/constant-expression-cxx1y.cpp @@ -790,3 +790,27 @@ namespace StmtExpr { return 0; } } + +namespace VirtualFromBase { + struct S1 { + virtual int f() const; + }; + struct S2 { + virtual int f(); + }; + template struct X : T { + constexpr X() {} + double d = 0.0; + constexpr int f() { return sizeof(T); } + }; + + // Non-virtual f(), OK. + constexpr X> xxs1; + constexpr X *p = const_cast>*>(&xxs1); + static_assert(p->f() == sizeof(S1), ""); + + // Virtual f(), not OK. + constexpr X> xxs2; + constexpr X *q = const_cast>*>(&xxs2); + static_assert(q->f() == sizeof(X), ""); // expected-error {{constant expression}} expected-note {{virtual function call}} +}