Author: rsmith Date: Fri Feb 10 16:35:37 2017 New Revision: 294796 URL: http://llvm.org/viewvc/llvm-project?rev=294796&view=rev Log: [c++1z] Diagnose attempts to use variables with deduced class template specialization types from within their own initializers.
Added: cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1z.cpp Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td cfe/trunk/include/clang/Sema/DeclSpec.h cfe/trunk/lib/Sema/SemaDecl.cpp cfe/trunk/lib/Sema/SemaExpr.cpp cfe/trunk/lib/Sema/SemaExprCXX.cpp cfe/trunk/lib/Sema/SemaType.cpp cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1y.cpp cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3.cpp cfe/trunk/test/Sema/auto-type.c cfe/trunk/test/SemaCXX/cxx1y-deduced-return-type.cpp Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original) +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Feb 10 16:35:37 2017 @@ -1861,8 +1861,8 @@ def warn_cxx98_compat_auto_type_specifie "'auto' type specifier is incompatible with C++98">, InGroup<CXX98Compat>, DefaultIgnore; def err_auto_variable_cannot_appear_in_own_initializer : Error< - "variable %0 declared with %select{'auto'|'decltype(auto)'|'__auto_type'}1 " - "type cannot appear in its own initializer">; + "variable %0 declared with deduced type %1 " + "cannot appear in its own initializer">; def err_binding_cannot_appear_in_own_initializer : Error< "binding %0 cannot appear in the initializer of its own " "decomposition declaration">; Modified: cfe/trunk/include/clang/Sema/DeclSpec.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/DeclSpec.h?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/include/clang/Sema/DeclSpec.h (original) +++ cfe/trunk/include/clang/Sema/DeclSpec.h Fri Feb 10 16:35:37 2017 @@ -519,7 +519,7 @@ public: SourceRange getTypeofParensRange() const { return TypeofParensRange; } void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; } - bool containsPlaceholderType() const { + bool hasAutoTypeSpec() const { return (TypeSpecType == TST_auto || TypeSpecType == TST_auto_type || TypeSpecType == TST_decltype_auto); } Modified: cfe/trunk/lib/Sema/SemaDecl.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaDecl.cpp (original) +++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Feb 10 16:35:37 2017 @@ -6120,7 +6120,7 @@ NamedDecl *Sema::ActOnVariableDeclarator D.getIdentifierLoc(), II, R, TInfo, SC); - if (D.getDeclSpec().containsPlaceholderType() && R->getContainedAutoType()) + if (R->getContainedDeducedType()) ParsingInitForAutoVars.insert(NewVD); if (D.isInvalidType()) @@ -6256,7 +6256,7 @@ NamedDecl *Sema::ActOnVariableDeclarator // If this decl has an auto type in need of deduction, make a note of the // Decl so we can diagnose uses of it in its own initializer. - if (D.getDeclSpec().containsPlaceholderType() && R->getContainedAutoType()) + if (R->getContainedDeducedType()) ParsingInitForAutoVars.insert(NewVD); if (D.isInvalidType() || Invalid) { @@ -11202,7 +11202,7 @@ Sema::DeclGroupPtrTy Sema::FinalizeDecla FirstDeclaratorInGroup = DD; if (!FirstDecompDeclaratorInGroup) FirstDecompDeclaratorInGroup = dyn_cast<DecompositionDecl>(D); - if (!FirstNonDeducedAutoInGroup && DS.containsPlaceholderType() && + if (!FirstNonDeducedAutoInGroup && DS.hasAutoTypeSpec() && !hasDeducedAuto(DD)) FirstNonDeducedAutoInGroup = DD; @@ -11935,7 +11935,7 @@ bool Sema::canDelayFunctionBody(const De // We can't delay parsing the body of a function template with a deduced // return type (yet). - if (D.getDeclSpec().containsPlaceholderType()) { + if (D.getDeclSpec().hasAutoTypeSpec()) { // If the placeholder introduces a non-deduced trailing return type, // we can still delay parsing it. if (D.getNumTypeObjects()) { Modified: cfe/trunk/lib/Sema/SemaExpr.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaExpr.cpp (original) +++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Feb 10 16:35:37 2017 @@ -333,10 +333,8 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl * Diag(Loc, diag::err_binding_cannot_appear_in_own_initializer) << D->getDeclName(); } else { - const AutoType *AT = cast<VarDecl>(D)->getType()->getContainedAutoType(); - Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) - << D->getDeclName() << (unsigned)AT->getKeyword(); + << D->getDeclName() << cast<VarDecl>(D)->getType(); } return true; } Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original) +++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Fri Feb 10 16:35:37 2017 @@ -1537,7 +1537,7 @@ Sema::ActOnCXXNew(SourceLocation StartLo if (D.getNumTypeObjects() > 0 && D.getTypeObject(0).Kind == DeclaratorChunk::Array) { DeclaratorChunk &Chunk = D.getTypeObject(0); - if (D.getDeclSpec().containsPlaceholderType()) + if (D.getDeclSpec().hasAutoTypeSpec()) return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) << D.getSourceRange()); if (Chunk.Arr.hasStatic) Modified: cfe/trunk/lib/Sema/SemaType.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaType.cpp (original) +++ cfe/trunk/lib/Sema/SemaType.cpp Fri Feb 10 16:35:37 2017 @@ -4160,7 +4160,7 @@ static TypeSourceInfo *GetFullTypeForDec if (!D.isInvalidType()) { // trailing-return-type is only required if we're declaring a function, // and not, for instance, a pointer to a function. - if (D.getDeclSpec().containsPlaceholderType() && + if (D.getDeclSpec().hasAutoTypeSpec() && !FTI.hasTrailingReturnType() && chunkIndex == 0 && !S.getLangOpts().CPlusPlus14) { S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), Modified: cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1y.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1y.cpp?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1y.cpp (original) +++ cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1y.cpp Fri Feb 10 16:35:37 2017 @@ -3,9 +3,9 @@ // FIXME: This is in p11 (?) in C++1y. void f() { - decltype(auto) a = a; // expected-error{{variable 'a' declared with 'decltype(auto)' type cannot appear in its own initializer}} - if (decltype(auto) b = b) {} // expected-error {{variable 'b' declared with 'decltype(auto)' type cannot appear in its own initializer}} - decltype(auto) c = ({ decltype(auto) d = c; 0; }); // expected-error {{variable 'c' declared with 'decltype(auto)' type cannot appear in its own initializer}} + decltype(auto) a = a; // expected-error{{variable 'a' declared with deduced type 'decltype(auto)' cannot appear in its own initializer}} + if (decltype(auto) b = b) {} // expected-error {{variable 'b' declared with deduced type 'decltype(auto)' cannot appear in its own initializer}} + decltype(auto) c = ({ decltype(auto) d = c; 0; }); // expected-error {{variable 'c' declared with deduced type 'decltype(auto)' cannot appear in its own initializer}} } void g() { Added: cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1z.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1z.cpp?rev=294796&view=auto ============================================================================== --- cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1z.cpp (added) +++ cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3-1z.cpp Fri Feb 10 16:35:37 2017 @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++1z + +// FIXME: This is in p10 (?) in C++1z. +template<typename T> struct A { + A(T); +}; +template<typename T> A(T) -> A<T>; +A a = a; // expected-error{{variable 'a' declared with deduced type 'A' cannot appear in its own initializer}} Modified: cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3.cpp?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3.cpp (original) +++ cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.spec.auto/p3.cpp Fri Feb 10 16:35:37 2017 @@ -1,11 +1,11 @@ // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++98 -Wno-c++11-extensions -Wc++11-compat void f() { - auto a = a; // expected-error{{variable 'a' declared with 'auto' type cannot appear in its own initializer}} - auto *b = b; // expected-error{{variable 'b' declared with 'auto' type cannot appear in its own initializer}} - const auto c = c; // expected-error{{variable 'c' declared with 'auto' type cannot appear in its own initializer}} - if (auto d = d) {} // expected-error {{variable 'd' declared with 'auto' type cannot appear in its own initializer}} - auto e = ({ auto f = e; 0; }); // expected-error {{variable 'e' declared with 'auto' type cannot appear in its own initializer}} + auto a = a; // expected-error{{variable 'a' declared with deduced type 'auto' cannot appear in its own initializer}} + auto *b = b; // expected-error{{variable 'b' declared with deduced type 'auto *' cannot appear in its own initializer}} + const auto c = c; // expected-error{{variable 'c' declared with deduced type 'const auto' cannot appear in its own initializer}} + if (auto d = d) {} // expected-error {{variable 'd' declared with deduced type 'auto' cannot appear in its own initializer}} + auto e = ({ auto f = e; 0; }); // expected-error {{variable 'e' declared with deduced type 'auto' cannot appear in its own initializer}} } void g() { Modified: cfe/trunk/test/Sema/auto-type.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/auto-type.c?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/test/Sema/auto-type.c (original) +++ cfe/trunk/test/Sema/auto-type.c Fri Feb 10 16:35:37 2017 @@ -7,7 +7,7 @@ __auto_type b = 5.0; __auto_type c = &b; __auto_type d = (struct {int a;}) {5}; _Static_assert(__builtin_types_compatible_p(__typeof(a), int), ""); -__auto_type e = e; // expected-error {{variable 'e' declared with '__auto_type' type cannot appear in its own initializer}} +__auto_type e = e; // expected-error {{variable 'e' declared with deduced type '__auto_type' cannot appear in its own initializer}} struct s { __auto_type a; }; // expected-error {{'__auto_type' not allowed in struct member}} Modified: cfe/trunk/test/SemaCXX/cxx1y-deduced-return-type.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx1y-deduced-return-type.cpp?rev=294796&r1=294795&r2=294796&view=diff ============================================================================== --- cfe/trunk/test/SemaCXX/cxx1y-deduced-return-type.cpp (original) +++ cfe/trunk/test/SemaCXX/cxx1y-deduced-return-type.cpp Fri Feb 10 16:35:37 2017 @@ -314,7 +314,7 @@ namespace NoReturn { } namespace UseBeforeComplete { - auto n = n; // expected-error {{variable 'n' declared with 'auto' type cannot appear in its own initializer}} + auto n = n; // expected-error {{variable 'n' declared with deduced type 'auto' cannot appear in its own initializer}} auto f(); // expected-note {{declared here}} void g() { &f; } // expected-error {{function 'f' with deduced return type cannot be used before it is defined}} auto sum(int i) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits