Bug 115583 - [14 Regression] C++23: Call to consteval function in `if consteval` immediate function context rejected at -O1 since r14-4140
Summary: [14 Regression] C++23: Call to consteval function in `if consteval` immediate...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 15.0
: P2 normal
Target Milestone: 14.2
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
: 115902 (view as bug list)
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2024-06-21 16:41 UTC by Mital Ashok
Modified: 2024-08-15 18:39 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work: 13.3.0
Known to fail: 14.1.0, 15.0
Last reconfirmed: 2024-06-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mital Ashok 2024-06-21 16:41:17 UTC
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
Comment 1 Drea Pinski 2024-06-21 16:47:39 UTC
Confirmed.
Comment 2 Patrick Palka 2024-06-21 20:32:46 UTC
Started with r14-4140
Comment 3 Drea Pinski 2024-06-21 20:36:22 UTC
Looks related to PR 110997 (and what was done in r14-6129-g1f1c432226cf3db) .
Comment 4 Drea Pinski 2024-07-14 23:17:42 UTC
*** Bug 115902 has been marked as a duplicate of this bug. ***
Comment 5 GCC Commits 2024-07-28 21:39:17 UTC
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.
Comment 6 GCC Commits 2024-07-29 13:14:33 UTC
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)
Comment 7 Jason Merrill 2024-07-29 13:52:21 UTC
Fixed for 14.2/15
Comment 8 GCC Commits 2024-08-15 18:39:25 UTC
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.