Reduced from https://stackoverflow.com/q/78652860 This valid code: ``` consteval int f(int i) { return i; } constexpr int g(int i) { if consteval { return f(i); } else { return i; } } int main() { return g(1); } ``` Compiles at -O0, but is rejected at -O1: https://godbolt.org/z/bP9rEsqc8
Confirmed.
Started with r14-4140
Looks related to PR 110997 (and what was done in r14-6129-g1f1c432226cf3db) .
*** Bug 115902 has been marked as a duplicate of this bug. ***
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>: https://gcc.gnu.org/g:d5f1948640815a554d106542c2e91e4e117aa3bc commit r15-2369-gd5f1948640815a554d106542c2e91e4e117aa3bc Author: Jason Merrill <jason@redhat.com> Date: Sat Jul 27 16:40:02 2024 -0400 c++: if consteval and consteval propagation [PR115583] During speculative constant folding of an if consteval, we take the false branch, but the true branch is an immediate function context, so we don't want to to cp_fold_immediate it. So we could check IF_STMT_CONSTEVAL_P here. But beyond that, we don't want to do this inside a call, only when first parsing a function. PR c++/115583 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_conditional_expression): Don't cp_fold_immediate for if consteval. gcc/testsuite/ChangeLog: * g++.dg/cpp23/consteval-if13.C: New test.
The releases/gcc-14 branch has been updated by Jason Merrill <jason@gcc.gnu.org>: https://gcc.gnu.org/g:9662299593c0b028e5008def72744732da429e9f commit r14-10518-g9662299593c0b028e5008def72744732da429e9f Author: Jason Merrill <jason@redhat.com> Date: Sat Jul 27 16:40:02 2024 -0400 c++: if consteval and consteval propagation [PR115583] During speculative constant folding of an if consteval, we take the false branch, but the true branch is an immediate function context, so we don't want to to cp_fold_immediate it. So we could check IF_STMT_CONSTEVAL_P here. But beyond that, we don't want to do this inside a call, only when first parsing a function. PR c++/115583 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_conditional_expression): Don't cp_fold_immediate for if consteval. gcc/testsuite/ChangeLog: * g++.dg/cpp23/consteval-if13.C: New test. (cherry picked from commit d5f1948640815a554d106542c2e91e4e117aa3bc)
Fixed for 14.2/15
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>: https://gcc.gnu.org/g:580fe7979f3c873eae885568d2c17c9e110670b4 commit r15-2938-g580fe7979f3c873eae885568d2c17c9e110670b4 Author: Patrick Palka <ppalka@redhat.com> Date: Thu Aug 15 14:38:47 2024 -0400 c++: fix up cpp23/consteval-if3.C test [PR115583] Compiling with optimizations is needed to trigger the bug fixed by r15-2369. PR c++/115583 gcc/testsuite/ChangeLog: * g++.dg/cpp23/consteval-if13.C: Compile with -O.