1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s 2a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith// RUN: %clang_cc1 -fsyntax-only -verify -std=c++1y %s -DCXX1Y 39f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 49f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct NonLiteral { NonLiteral(); }; 59f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 69f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// A type is a literal type if it is: 79f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 8a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith// [C++1y] - void 9a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr void f() {} 10a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#ifndef CXX1Y 11a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith// expected-error@-2 {{'void' is not a literal type}} 12a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith#endif 13a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith 149f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// - a scalar type 1586c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f1(double) { return 0; } 169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// - a reference type 189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct S { S(); }; 1986c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f2(S &) { return 0; } 209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 21ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedmanstruct BeingDefined; 22ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedmanextern BeingDefined beingdefined; 23ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedmanstruct BeingDefined { 24ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman static constexpr BeingDefined& t = beingdefined; 25ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman}; 26ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman 279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// - a class type that has all of the following properties: 289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 29ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman// (implied) - it is complete 30ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman 31c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smithstruct Incomplete; // expected-note 2{{forward declaration of 'Incomplete'}} 32ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedmantemplate<class T> struct ClassTemp {}; 33ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman 34c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smithconstexpr Incomplete incomplete = {}; // expected-error {{constexpr variable cannot have non-literal type 'const Incomplete'}} expected-note {{incomplete type 'const Incomplete' is not a literal type}} 35c799a6a5c884831c3c3ea57d30fbe4ab35709d49Richard Smithconstexpr Incomplete incomplete2[] = {}; // expected-error {{constexpr variable cannot have non-literal type 'Incomplete const[]'}} expected-note {{incomplete type 'Incomplete const[]' is not a literal type}} 36ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedmanconstexpr ClassTemp<int> classtemplate = {}; 37ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedmanconstexpr ClassTemp<int> classtemplate2[] = {}; 38ee0653963537f6ea60f655856fb0c83d7d4010dbEli Friedman 399f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// - it has a trivial destructor 409f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct UserProvDtor { 41840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith constexpr int f() const; // expected-error {{non-literal type 'UserProvDtor' cannot have constexpr members}} 429f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith ~UserProvDtor(); // expected-note {{has a user-provided destructor}} 439f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 4445fa560c72441069d9e4eb1e66efd87349caa552Douglas Gregor 459f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct NonTrivDtor { 4686c3ae46250cdcc57778c27826060779a92f3815Richard Smith constexpr NonTrivDtor(); 47840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith constexpr int f() const; // expected-error {{non-literal type 'NonTrivDtor' cannot have constexpr members}} 48ac71351acdefc9de0c770c1d717e621ac9e684bfRichard Smith virtual ~NonTrivDtor() = default; // expected-note {{has a non-trivial destructor}} expected-note {{because it is virtual}} 499f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 509f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct NonTrivDtorBase { 519f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith ~NonTrivDtorBase(); 529f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 539f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> 549f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct DerivedFromNonTrivDtor : T { // expected-note {{'DerivedFromNonTrivDtor<NonTrivDtorBase>' is not literal because it has base class 'NonTrivDtorBase' of non-literal type}} 559f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith constexpr DerivedFromNonTrivDtor(); 569f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 5786c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(DerivedFromNonTrivDtor<NonTrivDtorBase>) { return 0; } // expected-error {{constexpr function's 1st parameter type 'DerivedFromNonTrivDtor<NonTrivDtorBase>' is not a literal type}} 589f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct TrivDtor { 599f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith constexpr TrivDtor(); 609f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 6186c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(TrivDtor) { return 0; } 629f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct TrivDefaultedDtor { 639f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith constexpr TrivDefaultedDtor(); 649f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith ~TrivDefaultedDtor() = default; 659f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 6686c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(TrivDefaultedDtor) { return 0; } 679f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 689f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// - it is an aggregate type or has at least one constexpr constructor or 699f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// constexpr constructor template that is not a copy or move constructor 709f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct Agg { 719f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith int a; 729f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith char *b; 739f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 749f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithconstexpr int f3(Agg a) { return a.a; } 759f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct CtorTemplate { 769f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith template<typename T> constexpr CtorTemplate(T); 779f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 789f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct CopyCtorOnly { // expected-note {{'CopyCtorOnly' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors}} 7986c3ae46250cdcc57778c27826060779a92f3815Richard Smith constexpr CopyCtorOnly(CopyCtorOnly&); 80840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith constexpr int f() const; // expected-error {{non-literal type 'CopyCtorOnly' cannot have constexpr members}} 819f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 829f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct MoveCtorOnly { // expected-note {{no constexpr constructors other than copy or move constructors}} 8386c3ae46250cdcc57778c27826060779a92f3815Richard Smith constexpr MoveCtorOnly(MoveCtorOnly&&); 84840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith constexpr int f() const; // expected-error {{non-literal type 'MoveCtorOnly' cannot have constexpr members}} 859f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 869f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> 8786c3ae46250cdcc57778c27826060779a92f3815Richard Smithstruct CtorArg { 8886c3ae46250cdcc57778c27826060779a92f3815Richard Smith constexpr CtorArg(T); 899f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 9086c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(CtorArg<int>) { return 0; } // ok 9186c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(CtorArg<NonLiteral>) { return 0; } // ok, ctor is still constexpr 929f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// We have a special-case diagnostic for classes with virtual base classes. 939f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct VBase {}; 949f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct HasVBase : virtual VBase {}; // expected-note 2{{virtual base class declared here}} 959f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct Derived : HasVBase { 9686c3ae46250cdcc57778c27826060779a92f3815Richard Smith constexpr Derived() {} // expected-error {{constexpr constructor not allowed in struct with virtual base class}} 979f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 989f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> struct DerivedFromVBase : T { // expected-note {{struct with virtual base class is not a literal type}} 999f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith constexpr DerivedFromVBase(); 1009f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 10186c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(DerivedFromVBase<HasVBase>) {} // expected-error {{constexpr function's 1st parameter type 'DerivedFromVBase<HasVBase>' is not a literal type}} 10286c3ae46250cdcc57778c27826060779a92f3815Richard Smithtemplate<typename T> constexpr DerivedFromVBase<T>::DerivedFromVBase() : T() {} 10386c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int nVBase = (DerivedFromVBase<HasVBase>(), 0); // expected-error {{constant expression}} expected-note {{cannot construct object of type 'DerivedFromVBase<HasVBase>' with virtual base class in a constant expression}} 1049f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 1059f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith// - it has all non-static data members and base classes of literal types 1069f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct NonLitMember { 1079f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith S s; // expected-note {{has data member 's' of non-literal type 'S'}} 1089f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 10986c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(NonLitMember) {} // expected-error {{1st parameter type 'NonLitMember' is not a literal type}} 1109f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct NonLitBase : 1119f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith S { // expected-note {{base class 'S' of non-literal type}} 11286c3ae46250cdcc57778c27826060779a92f3815Richard Smith constexpr NonLitBase(); 113840462670ba7a6bc26265a2306b35f2f0f01f51cRichard Smith constexpr int f() const { return 0; } // expected-error {{non-literal type 'NonLitBase' cannot have constexpr members}} 1149f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 1159f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct LitMemBase : Agg { 1169f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith Agg agg; 1179f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 1189f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithtemplate<typename T> 1199f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct MemberType { 1209f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith T t; // expected-note {{'MemberType<NonLiteral>' is not literal because it has data member 't' of non-literal type 'NonLiteral'}} 1219f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith constexpr MemberType(); 1229f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 12386c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(MemberType<int>) { return 0; } 12486c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(MemberType<NonLiteral>) { return 0; } // expected-error {{not a literal type}} 1259f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 126a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith// - an array of literal type [C++1y] other than an array of runtime bound 1279f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct ArrGood { 1289f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith Agg agg[24]; 1299f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith double d[12]; 1309f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith TrivDtor td[3]; 131099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith TrivDefaultedDtor tdd[3]; 1329f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 13386c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(ArrGood) { return 0; } 1349f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith 1359f569cca2a4c5fb6026005434e27025b9e71309dRichard Smithstruct ArrBad { 1369f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith S s[3]; // expected-note {{data member 's' of non-literal type 'S [3]'}} 1379f569cca2a4c5fb6026005434e27025b9e71309dRichard Smith}; 13886c3ae46250cdcc57778c27826060779a92f3815Richard Smithconstexpr int f(ArrBad) { return 0; } // expected-error {{1st parameter type 'ArrBad' is not a literal type}} 139a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith 140a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smithconstexpr int arb(int n) { 141a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith int a[n]; // expected-error {{variable of non-literal type 'int [n]' cannot be defined in a constexpr function}} 142a10b97898ee6339c3110e6ca33f178ff52f05238Richard Smith} 143