From 3075affdbcb3232fe549fbeed87bd94114c14758 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Sat, 8 Sep 2018 12:00:02 -0400 Subject: [PATCH] PR c++/86678 - constexpr function with non-constant after return. In this testcase, the call to f() can never be a constant expression, but that's not a problem because it isn't always reached by calls to g. We were wrongly rejecting this because potential_constant_expression_1 lacked the jump tracking that cxx_eval_constant_expression has. So this patch adds a simpler version of that tracking. * constexpr.c (potential_constant_expression_1): Add jump_target. (breaks): Check for BREAK_STMT. (continues): Check for CONTINUE_STMT. From-SVN: r264171 --- gcc/cp/ChangeLog | 7 ++ gcc/cp/constexpr.c | 66 +++++++++++++++---- .../g++.dg/cpp1y/constexpr-return4.C | 10 +++ 3 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-return4.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9ca0ef8c0fc..c0fa4660df9 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2018-09-08 Jason Merrill + + PR c++/86678 - constexpr function with non-constant after return. + * constexpr.c (potential_constant_expression_1): Add jump_target. + (breaks): Check for BREAK_STMT. + (continues): Check for CONTINUE_STMT. + 2018-09-08 Marek Polacek * cxx-pretty-print.c (cxx_pretty_printer::statement) is not a constant expression"); return false; @@ -6224,6 +6255,15 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, #undef RECUR } +bool +potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, + tsubst_flags_t flags) +{ + tree target = NULL_TREE; + return potential_constant_expression_1 (t, want_rval, strict, now, + flags, &target); +} + /* The main entry point to the above. */ bool diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-return4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-return4.C new file mode 100644 index 00000000000..8f29628148f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-return4.C @@ -0,0 +1,10 @@ +// PR c++/86678 +// { dg-do compile { target c++14 } } + +constexpr bool always_true() { return true; } +int f() { return 1; } +constexpr int g() { + if (always_true()) + return 0; + return f(); +} -- 2.30.2