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

            Bug ID: 129148
           Summary: [modules] Bad file data importing template whose body
                    uses a constrained auto placeholder with a
                    lambda-containing concept
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gccbugzilla at thepirate42 dot org
  Target Milestone: ---

Compiler Explorer: https://godbolt.org/z/bnMKT6K6T

Importing a module fails with "Bad file data" when an exported function
template contains a constrained auto variable whose concept contains a
lambda, and the initializer is not dependent.

// m.cppm
export module m;
template<typename T> concept C = requires { []{}; };
export template<typename> void f() { C auto x = 0; }

// example.cpp
import m;
int main(){ f<int>(); }

$ g++ -std=c++20 -fmodules -c -x c++ m.cppm -o m.o
$ g++ -std=c++20 -fmodules -c example.cpp -o example.o
In module imported at example.cpp:1:1:
m: In function 'int main()':
m: error: failed to read compiled module cluster 1: Bad file data
m: note: compiled module file is 'gcm.cache/m.gcm'
example.cpp:2:13: fatal error: failed to load binding '::f@m'
    2 | int main(){ f<int>(); }
      |             ^
compilation terminated.

As you can see in Compiler Explorer, Clang 23.1.0 accepts the code, and the
program compiles and links, while gcc trunk doesn't. The output above is from a
local gcc 16.2.1

Variations:
- fails: the lambda is in sizeof([]{}) > 0 instead of a requires-expression
- fails: f is an abbreviated function template, void f(auto)
- fails: f is a member function template of an exported class
- works: the initializer is dependent (C auto x = T{};)
- works: f is a non-template function
- works: f is never instantiated in the importer
- works: the concept is used as template<C T>, in static_assert(C<int>), or as
a return type (C auto f())
- works: the lambda is replaced by a named function template
- works: the concept has no lambda

LLM WRITTEN PART (I've read it all and it seems useful context, but I wouldn't
have been able to write it myself):

-fdump-lang-module-all on m.cppm shows that the lambda in the concept
definition is keyed to C and streamed in C's cluster, but f's cluster
contains:

   Writing TU-local entity:-17 type_decl:'::._anon_1'

and the summary reports "1 TU-local entities".

Per CWG2988, and as implemented for PR116568 (r15-7203), lambdas in
concept definitions are not TU-local, so the code should be accepted.

PR125356 is a similar failure mode (an undiagnosed TU-local lambda
reached from an exported template produces "Bad file data"), but there
the lambda really is TU-local.
  • [Bug c++/129148] New: [mod... gccbugzilla at thepirate42 dot org via Gcc-bugs

Reply via email to