From 899ac3b80040d7879082b5df11675cea88a43756 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Mon, 4 Dec 2017 17:52:07 -0500 Subject: [PATCH] PR c++/83273 - constexpr if allows non-constant condition * semantics.c (finish_if_stmt_cond): Use require_constant_expression rather than is_constant_expression. * constexpr.c (potential_constant_expression_1) [LAMBDA_EXPR]: Allow in C++17. From-SVN: r255390 --- gcc/cp/ChangeLog | 8 ++++++++ gcc/cp/constexpr.c | 8 ++++++++ gcc/cp/semantics.c | 2 +- gcc/testsuite/g++.dg/cpp1z/constexpr-if12.C | 2 +- gcc/testsuite/g++.dg/cpp1z/constexpr-if13.C | 10 ++++++++++ 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if13.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 5495dc13c69..8760a701677 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,11 @@ +2017-12-04 Jason Merrill + + PR c++/83273 - constexpr if allows non-constant condition + * semantics.c (finish_if_stmt_cond): Use require_constant_expression + rather than is_constant_expression. + * constexpr.c (potential_constant_expression_1) [LAMBDA_EXPR]: Allow + in C++17. + 2017-12-01 Jason Merrill Give #include hints for . diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index f0370cc2aff..6dfecfc1b14 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5524,6 +5524,14 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, return RECUR (STMT_EXPR_STMT (t), rval); case LAMBDA_EXPR: + if (cxx_dialect >= cxx17) + /* In C++17 lambdas can be constexpr, don't give up yet. */ + return true; + else if (flags & tf_error) + error_at (loc, "lambda-expression is not a constant expression " + "before C++17"); + return false; + case DYNAMIC_CAST_EXPR: case PSEUDO_DTOR_EXPR: case NEW_EXPR: diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index e2daab4339e..c6726324ae6 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -731,7 +731,7 @@ finish_if_stmt_cond (tree cond, tree if_stmt) { cond = maybe_convert_cond (cond); if (IF_STMT_CONSTEXPR_P (if_stmt) - && is_constant_expression (cond) + && require_constant_expression (cond) && !value_dependent_expression_p (cond)) { cond = instantiate_non_dependent_expr (cond); diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if12.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if12.C index 4e887f3b939..db984a64677 100644 --- a/gcc/testsuite/g++.dg/cpp1z/constexpr-if12.C +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if12.C @@ -7,7 +7,7 @@ struct T { template constexpr auto bf(T t) { - if constexpr(t.foo()) { + if constexpr(t.foo()) { // { dg-error "constant expression" } return false; } return true; diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if13.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if13.C new file mode 100644 index 00000000000..55dbfd902ee --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if13.C @@ -0,0 +1,10 @@ +// PR c++/83273 +// { dg-options -std=c++17 } + +int main() +{ + auto d = 42; + if constexpr (d > 0) { // { dg-error "constant expression" } + return d; + } +} -- 2.30.2