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

            Bug ID: 126039
           Summary: [contracts] Parser failure / invalid const qualifier
                    error on variadic fold expressions inside generic
                    lambda preconditions
           Product: gcc
           Version: 16.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zuhaitz.zechhub at gmail dot com
  Target Milestone: ---
              Host: x86_64-redhat-linux
            Target: x86_64-redhat-linux
             Build: x86_64-redhat-linux

Created attachment 64898
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64898&action=edit
Preprocessed source.

When a C++26 contract contains a C++17 fold expression operating on a C++20
generic lambda forwarding reference pack (auto&&...), the compiler front-end
fails to parse the expression layout. The contract template synthesis engine
erroneously attempts to apply a top-level const qualifier to the deduced
reference types, corrupting the AST parse tree for the fold expansion.

EXACT GCC VERSION:
gcc version 16.1.1 20260515 (Red Hat 16.1.1-2) (GCC)

SYSTEM TYPE / TARGET:
Target: x86_64-redhat-linux

COMPLETE COMMAND LINE TRIGGERING THE BUG:
g++ -std=c++26 -fcontracts -save-temps bug_variadic_fold.cpp

COMPILER OUTPUT:
bug_variadic_fold.cpp: In lambda function:
bug_variadic_fold.cpp:5:14: error: ‘const’ qualifiers cannot be applied to
‘auto:1&&’ [-Wtemplate-body]
    5 |          pre((args >= 0 && ...))
      |               ^~~~
bug_variadic_fold.cpp:5:19: error: binary expression in operand of
fold-expression [-Wtemplate-body]
    5 |          pre((args >= 0 && ...))
      |               ~~~~~^~~~

MINIMAL STANDALONE TESTCASE (bug_variadic_fold.cpp):
#include <contracts>

int main() {
    auto check_all_positive = [](auto&&... args)
        pre((args >= 0 && ...))
    {};
    check_all_positive(1, 2, 3);
}

Reply via email to