blob: a2362526a28643dc89eb54093928315f97e4ac59 [file] [log] [blame]
Saar Razd7aae332019-07-10 21:25:49 +00001// Support parsing of concepts
2
3// RUN: %clang_cc1 -std=c++2a -fconcepts-ts -verify %s
4template<typename T> concept C1 = true; // expected-note 2{{previous}}
5
6template<typename T> concept C1 = true; // expected-error{{redefinition}}
7
8template<concept T> concept D1 = true;
9// expected-error@-1{{expected template parameter}}
10// expected-error@-2{{concept template parameter list must have at least one parameter; explicit specialization of concepts is not allowed}}
11
12template<template<typename> concept T> concept D2 = true;
13// expected-error@-1{{expected identifier}}
14// expected-error@-2{{template template parameter requires 'class' after the parameter list}}
15// expected-error@-3{{concept template parameter list must have at least one parameter; explicit specialization of concepts is not allowed}}
16
Saar Razd7aae332019-07-10 21:25:49 +000017struct S1 {
18 template<typename T> concept C1 = true; // expected-error {{concept declarations may only appear in global or namespace scope}}
19};
20
21extern "C++" {
22 template<typename T> concept C1 = true; // expected-error{{redefinition}}
23}
24
25template<typename A>
26template<typename B>
Saar Raz5d98ba62019-10-15 15:24:26 +000027concept C2 = true; // expected-error {{extraneous template parameter list in concept definition}}
Saar Razd7aae332019-07-10 21:25:49 +000028
Saar Raz5d98ba62019-10-15 15:24:26 +000029template<typename T> concept C3 = true; // expected-note {{previous}} expected-note {{previous}}
30int C3; // expected-error {{redefinition}}
31struct C3 {}; // expected-error {{redefinition}}
Saar Razd7aae332019-07-10 21:25:49 +000032
Saar Raz5d98ba62019-10-15 15:24:26 +000033struct C4 {}; // expected-note{{previous definition is here}}
34template<typename T> concept C4 = true;
35// expected-error@-1{{redefinition of 'C4' as different kind of symbol}}
Saar Razd7aae332019-07-10 21:25:49 +000036
37// TODO: Add test to prevent explicit specialization, partial specialization
38// and explicit instantiation of concepts.
39
40template<typename T, T v>
41struct integral_constant { static constexpr T value = v; };
42
43namespace N {
Saar Raz5d98ba62019-10-15 15:24:26 +000044 template<typename T> concept C5 = true;
Saar Razd7aae332019-07-10 21:25:49 +000045}
Saar Raz5d98ba62019-10-15 15:24:26 +000046using N::C5;
Saar Razd7aae332019-07-10 21:25:49 +000047
Saar Raz5d98ba62019-10-15 15:24:26 +000048template <bool word> concept C6 = integral_constant<bool, wor>::value;
Saar Razd7aae332019-07-10 21:25:49 +000049// expected-error@-1{{use of undeclared identifier 'wor'; did you mean 'word'?}}
50// expected-note@-2{{'word' declared here}}
51
Saar Raz5d98ba62019-10-15 15:24:26 +000052template<typename T> concept bool C7 = true;
Saar Razd7aae332019-07-10 21:25:49 +000053// expected-warning@-1{{ISO C++2a does not permit the 'bool' keyword after 'concept'}}
54
Saar Raz5d98ba62019-10-15 15:24:26 +000055template<> concept C8 = false;
Saar Razd7aae332019-07-10 21:25:49 +000056// expected-error@-1{{concept template parameter list must have at least one parameter; explicit specialization of concepts is not allowed}}
57
Saar Raz5d98ba62019-10-15 15:24:26 +000058template<> concept C7<int> = false;
Saar Razd7aae332019-07-10 21:25:49 +000059// expected-error@-1{{name defined in concept definition must be an identifier}}
60
Saar Raz5d98ba62019-10-15 15:24:26 +000061template<typename T> concept N::C9 = false;
Saar Razd7aae332019-07-10 21:25:49 +000062// expected-error@-1{{name defined in concept definition must be an identifier}}
63
64class A { };
65// expected-note@-1{{'A' declared here}}
66
Saar Raz5d98ba62019-10-15 15:24:26 +000067template<typename T> concept A::C10 = false;
Saar Razd7aae332019-07-10 21:25:49 +000068// expected-error@-1{{expected namespace name}}
69
70template<typename T> concept operator int = false;
71// expected-error@-1{{name defined in concept definition must be an identifier}}
Saar Raz5d98ba62019-10-15 15:24:26 +000072
73template<bool x> concept C11 = 2; // expected-error {{atomic constraint must be of type 'bool' (found 'int')}}
74template<bool x> concept C12 = 2 && x; // expected-error {{atomic constraint must be of type 'bool' (found 'int')}}
75template<bool x> concept C13 = x || 2 || x; // expected-error {{atomic constraint must be of type 'bool' (found 'int')}}
76template<bool x> concept C14 = 8ull && x || x; // expected-error {{atomic constraint must be of type 'bool' (found 'unsigned long long')}}
77template<typename T> concept C15 = sizeof(T); // expected-error {{atomic constraint must be of type 'bool'}}
78template<typename T> concept C16 = true && (0 && 0); // expected-error {{atomic constraint must be of type 'bool' (found 'int')}}
79// expected-warning@-1{{use of logical '&&' with constant operand}}
80// expected-note@-2{{use '&' for a bitwise operation}}
81// expected-note@-3{{remove constant to silence this warning}}
82template<typename T> concept C17 = T{};
83static_assert(!C17<bool>);
84template<typename T> concept C18 = (bool&&)true;
85static_assert(C18<int>);
86template<typename T> concept C19 = (const bool&)true;
87static_assert(C19<int>);
88template<typename T> concept C20 = (const bool)true;
89static_assert(C20<int>);
90template <bool c> concept C21 = integral_constant<bool, c>::value && true;
91static_assert(C21<true>);
92static_assert(!C21<false>);
93template <bool c> concept C22 = integral_constant<bool, c>::value;
94static_assert(C22<true>);
95static_assert(!C22<false>);
96
97template <bool word> concept C23 = integral_constant<bool, wor>::value;
98// expected-error@-1{{use of undeclared identifier 'wor'; did you mean 'word'?}}
99// expected-note@-2{{'word' declared here}}
100