https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128870

            Bug ID: 128870
           Summary: intermittent "taking address of an immediate function"
                    for a promoted std::optional constructor at -Os/-Oz
                    (rejects-valid)
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: filip.sajdak at gmail dot com
  Target Milestone: ---
              Host: aarch64-apple-darwin25
            Target: aarch64-apple-darwin25
             Build: aarch64-apple-darwin25

The following valid program is rejected intermittently when optimising for size
(-Os or -Oz). The same command line fails in some runs and compiles in others.
It never failed at -O0, -O1, -O2 or -O3.

$ cat red4.cpp
#include <optional>
#include <string>
struct Name {
  template<class T> requires std::constructible_from<std::string, T>
  consteval Name(T&& n) : s((T&&) n) {}
  std::string s;
};
struct Options { std::optional<Name> name; };
consteval int f(Options o) { return o.name ? 1 : 0; }
consteval int g() { return f({.name = "title"}); }
constexpr int i = g();
int main() { return i; }

$ g++-16 -std=c++23 -Os -c red4.cpp
In constructor 'constexpr std::optional<_Tp>::optional(_Up&&) [with _Up = const
char (&)[6]; _Tp = Name]':
cc1plus: error: taking address of an immediate function 'constexpr
std::optional<_Tp>::optional(_Up&&) [with _Up = const char (&)[6]; _Tp = Name]'
In file included from red4.cpp:1:
/opt/homebrew/Cellar/gcc/16.2.0/include/c++/16/optional:912:54: note:
'constexpr std::optional<_Tp>::optional(_Up&&) [with _Up = const char (&)[6];
_Tp = Name]' was promoted to an immediate function because its body contains an
immediate-escalating expression
  912 |         : _Base(std::in_place, std::forward<_Up>(__t)) { }
      |                                                      ^

The error has no source location of its own. With -fno-exceptions -fno-rtti
added, a failing run names std::_Optional_payload<Name, false, false,
false>::_Optional_payload(std::in_place_t, _Args&& ...) instead.

Failures out of 20 runs of the same command (g++-16 -std=<std> <level> -c
red4.cpp), GCC 16.2.0, aarch64-apple-darwin25:

            -O0   -O1   -O2   -O3   -Os    -Oz
  c++20     0     0     0     0     15     15
  c++23     0     0     0     0     16     16
  c++26     0     0     0     0     8      7

Every call in the program is in a constant-evaluated context, and the program
compiles at the other levels, so I believe the code is valid. The constructor
was promoted to an immediate function, and something at -Os/-Oz still refers to
its address. Since the outcome changes from run to run with identical input, it
probably depends on addresses or on an iteration order.

The shape is reduced from std::meta::data_member_options::_Name in <meta>. The
reduced file needs no reflection. I first met the error through
std::meta::data_member_spec(^^int, {.name = "title"}) at -Os, but only with the
out-of-tree build mentioned below; that <meta> form did not fail for me on
16.2.0.

Also seen once each (single runs, so they say little about the rate) on trunk
e632e263a5ca1fb5157db28def311bbc62b8f100 with an out-of-tree wasm32 back end at
-Os and -Oz. A native aarch64-unknown-linux-gnu build of the same trunk commit
did not fail in single runs at -O0 to -Oz; given the intermittency I would not
read that as "not affected".

Not checked: x86-64, GCC 15 and older, whether -fno-inline hides it.

Possibly related: PR 128386 (another case of a function wrongly treated as
immediate on 17).

g++-16 -v: Target: aarch64-apple-darwin25, Homebrew GCC 16.2.0,
--enable-checking=release.

This report was prepared with AI assistance. The reproduction and the table
above were run on my machine.

Reply via email to