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

            Bug ID: 116418
           Summary: [11/12/13/14/15 Regression] Nested statement
                    expressions with decltype auto in template break
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: valentin at tolmer dot fr
  Target Milestone: ---

$ cat source.cpp
template <typename>
void fn() {
  ({ decltype(auto) v = ({3;}); }); // breaks
  ({ auto v = ({3;}); });           // works
}
void fn2() {
  ({ decltype(auto) v = ({3;}); });  // works
}
$ g++ -std=c++20 source.cpp -c -O0
$ g++ -std=c++20 source.cpp -c -O1
source.cpp: In function 'void fn()':
source.cpp:3:30: error: statement-expression in a constant expression
[-Wtemplate-body]
    3 |   ({ decltype(auto) v = ({3;}); }); // breaks
      |                              ^
Compiler returned: 1

I tested it on godbolt, and it works on GCC 10.5, but not in 11.1, all the way
up to trunk. It's dependent on the optimizations, I've had some form of it
happen in our codebase in different places depending on the compiler version
(so upgrading GCC becomes a breaking change).
I tried to pinpoint the optimization that causes the diagnosis, but listing all
the flags for -O1 in https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
doesn't trigger the issue; adding -O1 does. Similarly, adding -O1 and disabling
all these flags triggers the issue.

Note that the templated function doesn't have to be templated to trigger the
bug.

Reply via email to