| 1 |
// ensure that passing asserts do not affect constexpr functions |
| 2 |
// ensure that failing asserts generate an error in a constexpr function |
| 3 |
// { dg-do compile } |
| 4 |
// { dg-options "-std=c++2a -fcontracts" } |
| 5 |
|
| 6 |
constexpr int wfun(int a) { |
| 7 |
[[assert: a > 0]]; |
| 8 |
return a; |
| 9 |
} |
| 10 |
|
| 11 |
constexpr int ffun(int a) { |
| 12 |
[[assert: a > 0]]; |
| 13 |
return a; |
| 14 |
} |
| 15 |
|
| 16 |
template<typename T> |
| 17 |
constexpr int tfun(T a) { |
| 18 |
[[assert: a > 0]]; |
| 19 |
return a; |
| 20 |
} |
| 21 |
|
| 22 |
template<typename T> |
| 23 |
constexpr int wtfun(T a) { |
| 24 |
[[assert: a > 0]]; |
| 25 |
return a; |
| 26 |
} |
| 27 |
|
| 28 |
template<typename T> |
| 29 |
constexpr int ftfun(T a) { |
| 30 |
[[assert: a > 0]]; |
| 31 |
return a; |
| 32 |
} |
| 33 |
|
| 34 |
constexpr int explicitfn(int a) { |
| 35 |
[[assert ignore: a > 0]]; |
| 36 |
[[assert check_never_continue: a > 0]]; |
| 37 |
return a; |
| 38 |
} |
| 39 |
|
| 40 |
int main(int, char **) { |
| 41 |
constexpr int a = wfun(10); |
| 42 |
constexpr int b = ffun(-10); // { dg-message "in .constexpr. expansion" } |
| 43 |
// { dg-error "contract predicate" "" { target *-*-* } 12 } |
| 44 |
constexpr int c = wtfun(10); |
| 45 |
constexpr int d = ftfun(-10); // { dg-message "in .constexpr. expansion" } |
| 46 |
// { dg-error "contract predicate" "" { target *-*-* } 30 } |
| 47 |
constexpr int e = explicitfn(-10); // { dg-message "in .constexpr. expansion" } |
| 48 |
// { dg-error "contract predicate" "" { target *-*-* } 36 } |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|