Bug 115902 - [14/15 Regression] Can't call immediate function within "if consteval" when optimizing
Summary: [14/15 Regression] Can't call immediate function within "if consteval" when o...
Status: RESOLVED DUPLICATE of bug 115583
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.1.1
: P3 normal
Target Milestone: 14.2
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2024-07-13 07:02 UTC by David Mazières
Modified: 2024-07-14 23:17 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Mazières 2024-07-13 07:02:54 UTC
When optimizing, g++ seems to try calling immediate functions inside "if consteval" and then reject what should be valid code.  Here is a simple example (which I place in the public domain):

```
// g++ -std=c++23 -c -O2 consteval.cc

consteval int
plusone(int n)
{
  return n + 1;
}

constexpr int
maybeplusone(int n)
{
  if consteval {
    return plusone(n);
  }
  else {
    return n;
  }
}

int
not_consteval()
{
  return maybeplusone(1);
}
```

It gives the following error:

```
$ g++ -std=c++23 -c -O2 consteval.cc
consteval.cc: In function 'int not_consteval()':
consteval.cc:23:22:   in 'constexpr' expansion of 'maybeplusone(1)'
consteval.cc:13:19: error: call to consteval function 'plusone(n)' is not a constant expression
   13 |     return plusone(n);
      |            ~~~~~~~^~~
consteval.cc:13:20: error: 'n' is not a constant expression
   13 |     return plusone(n);
      |                    ^
$ 
```

The code compiles fine without optimization, and also works with clang 18.
Comment 1 Drea Pinski 2024-07-13 08:03:49 UTC
Most likely introduced by the change which was done for https://wg21.link/p2564 (r14-6129-g1f1c432226cf3db399b2a2a627e3c5720b02b1d6  PR 107687 ).
Comment 2 Jonathan Wakely 2024-07-14 08:07:58 UTC
It was introduced by r14-4140:  c++: Move consteval folding to cp_fold_r
Comment 3 David Mazières 2024-07-14 23:14:22 UTC
This might be the same as Bug 115583.
Comment 4 Drea Pinski 2024-07-14 23:17:42 UTC
Yes it is a dup.

*** This bug has been marked as a duplicate of bug 115583 ***