From cf8ba0fbdb0e9da1162b1aa2600439ba48e50534 Mon Sep 17 00:00:00 2001 From: Nathan Wilson Date: Fri, 29 Jan 2016 04:43:59 +0000 Subject: [PATCH] [Concepts] Implement a portion of Concepts TS[dcl.spec.concept]p5 and p6: Diagnose if the return type of a function concept or declaration type of a variable concept is not bool. Reviewers: hubert.reinterpretcast Differential Revision: http://reviews.llvm.org/D16163 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@259159 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 4 +++ lib/Sema/SemaDecl.cpp | 17 +++++++++++++ .../dcl.dcl/dcl.spec/dcl.spec.concept/p5.cpp | 12 +++++++++ .../dcl.dcl/dcl.spec/dcl.spec.concept/p6.cpp | 25 +++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 test/CXX/concepts-ts/dcl.dcl/dcl.spec/dcl.spec.concept/p6.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index a71111ada6..f501c8a97a 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2074,6 +2074,10 @@ def err_concept_decl_invalid_specifiers : Error< "'%select{thread_local|inline|friend|constexpr}1'">; def err_function_concept_with_params : Error< "function concept cannot have any parameters">; +def err_function_concept_bool_ret : Error< + "declared return type of function concept must be 'bool'">; +def err_variable_concept_bool_decl : Error< + "declared type of variable concept must be 'bool'">; // C++11 char16_t/char32_t def warn_cxx98_compat_unicode_type : Warning< diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index cb79948341..11d51a0979 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -6001,6 +6001,15 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, << 0 << 3; NewVD->setInvalidDecl(true); } + + // C++ Concepts TS [dcl.spec.concept]p6: A variable concept has the + // following restrictions: + // - The declared type shall have the type bool. + if (!Context.hasSameType(NewVD->getType(), Context.BoolTy) && + !NewVD->isInvalidDecl()) { + Diag(D.getIdentifierLoc(), diag::err_variable_concept_bool_decl); + NewVD->setInvalidDecl(true); + } } } @@ -7681,6 +7690,14 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, Context.adjustExceptionSpec(NewFD, EST_BasicNoexcept); } + // C++ Concepts TS [dcl.spec.concept]p5: A function concept has the + // following restrictions: + // - The declared return type shall have the type bool. + if (!Context.hasSameType(FPT->getReturnType(), Context.BoolTy)) { + Diag(D.getIdentifierLoc(), diag::err_function_concept_bool_ret); + NewFD->setInvalidDecl(); + } + // C++ Concepts TS [dcl.spec.concept]p5: A function concept has the // following restrictions: // - The declaration's parameter list shall be equivalent to an empty diff --git a/test/CXX/concepts-ts/dcl.dcl/dcl.spec/dcl.spec.concept/p5.cpp b/test/CXX/concepts-ts/dcl.dcl/dcl.spec/dcl.spec.concept/p5.cpp index 38593bccaf..69672ca830 100644 --- a/test/CXX/concepts-ts/dcl.dcl/dcl.spec/dcl.spec.concept/p5.cpp +++ b/test/CXX/concepts-ts/dcl.dcl/dcl.spec/dcl.spec.concept/p5.cpp @@ -11,3 +11,15 @@ concept bool fcpp(Ts... ts) { return true; } // expected-error {{function concep template concept bool fcpva(...) { return true; } // expected-error {{function concept cannot have any parameters}} + +template +concept const bool fcrtc() { return true; } // expected-error {{declared return type of function concept must be 'bool'}} + +template +concept int fcrti() { return 5; } // expected-error {{declared return type of function concept must be 'bool'}} + +template +concept float fcrtf() { return 5.5; } // expected-error {{declared return type of function concept must be 'bool'}} + +template +concept decltype(auto) fcrtd(void) { return true; } // expected-error {{declared return type of function concept must be 'bool'}} diff --git a/test/CXX/concepts-ts/dcl.dcl/dcl.spec/dcl.spec.concept/p6.cpp b/test/CXX/concepts-ts/dcl.dcl/dcl.spec/dcl.spec.concept/p6.cpp new file mode 100644 index 0000000000..f8a1bb72e3 --- /dev/null +++ b/test/CXX/concepts-ts/dcl.dcl/dcl.spec/dcl.spec.concept/p6.cpp @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -std=c++14 -fconcepts-ts -x c++ -verify %s + +template +concept bool vc { true }; + +template +struct B { typedef bool Boolean; }; + +template +B::Boolean concept vctb(!0); + +template +concept const bool vctc { true }; // expected-error {{declared type of variable concept must be 'bool'}} + +template +concept int vcti { 5 }; // expected-error {{declared type of variable concept must be 'bool'}} + +template +concept float vctf { 5.5 }; // expected-error {{declared type of variable concept must be 'bool'}} + +template +concept auto vcta { true }; // expected-error {{declared type of variable concept must be 'bool'}} + +template +concept decltype(auto) vctd { true }; // expected-error {{declared type of variable concept must be 'bool'}} -- 2.40.0