<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/126435>126435</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Lambda with by-copy capture returned from function in module has copy constructor deleted
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          es1024
      </td>
    </tr>
</table>

<pre>
    Reproducer (https://godbolt.org/z/79q6Gsdas):
```
// module.cppm
export module mod;
export auto make_lambda(int x) {
  return [x] { return x; };
}

// test.cc
import mod;
void test() {
  auto f = make_lambda(0);
  auto g = f;
}
```
yields
```
test.cc:4:8: error: no matching constructor for initialization of '(lambda at /app/module.cppm:3:10)'
    4 |   auto g = f;
      |        ^   ~
1 error generated.
```

A similar example where we instead create a std::function triggers an assertion (https://godbolt.org/z/bhjqr45bE):
```
// module.cppm
export module mod;
export auto make_lambda(int x) {
  return [x] { return x; };
}

// test.cc
#include <functional>
import mod;
void test() {
 std::function<int(void)> f = make_lambda(0);
}
```
```
/opt/compiler-explorer/clang-assertions-trunk/bin/clang++ --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot   -fcolor-diagnostics -fno-crash-diagnostics -stdlib=libc++ -g -std=gnu++20 -MD -MT CMakeFiles/test_modules.dir/test.cc.o -MF CMakeFiles/test_modules.dir/test.cc.o.d @CMakeFiles/test_modules.dir/test.cc.o.modmap -o CMakeFiles/test_modules.dir/test.cc.o -c /app/test.cc
clang++: /root/llvm-project/clang/include/clang/AST/DeclCXX.h:871: void clang::CXXRecordDecl::setImplicitCopyConstructorIsDeleted(): Assertion `(data().DefaultedCopyConstructorIsDeleted || needsOverloadResolutionForCopyConstructor()) && "Copy constructor should not be deleted"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -stdlib=libc++ -g -std=gnu++20 -MD -MT CMakeFiles/test_modules.dir/test.cc.o -MF CMakeFiles/test_modules.dir/test.cc.o.d @CMakeFiles/test_modules.dir/test.cc.o.modmap -o CMakeFiles/test_modules.dir/test.cc.o -c /app/test.cc
1.      <eof> parser at end of file
2.      /opt/compiler-explorer/clang-assertions-trunk-20250209/bin/../include/c++/v1/__functional/function.h:952:30: instantiating function definition 'std::function<int ()>::function<(lambda at /app/module.cppm:3:10), void>'
3.      /opt/compiler-explorer/clang-assertions-trunk-20250209/bin/../include/c++/v1/__functional/function.h:378:34: instantiating function definition 'std::__function::__value_func<int ()>::__value_func<(lambda at /app/module.cppm:3:10), 0>'
4.      /opt/compiler-explorer/clang-assertions-trunk-20250209/bin/../include/c++/v1/__functional/function.h:358:25: instantiating function definition 'std::__function::__value_func<int ()>::__value_func<(lambda at /app/module.cppm:3:10), std::allocator<(lambda at /app/module.cppm:3:10)>>'
5.      /opt/compiler-explorer/clang-assertions-trunk-20250209/bin/../include/c++/v1/__functional/function.h:274:34: instantiating function definition 'std::__function::__func<(lambda at /app/module.cppm:3:10), std::allocator<(lambda at /app/module.cppm:3:10)>, int ()>::__func'
 #0 0x0000000003e72268 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3e72268)
 #1 0x0000000003e6ff24 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3e6ff24)
 #2 0x0000000003dbc3f8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x000076b807842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x000076b8078969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x000076b807842476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x000076b8078287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x000076b80782871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x000076b807839e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x00000000068bdbd5 clang::Sema::DeclareImplicitCopyConstructor(clang::CXXRecordDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x68bdbd5)
#10 0x0000000006b5a40f void llvm::function_ref<void ()>::callback_fn<clang::Sema::LookupConstructors(clang::CXXRecordDecl*)::'lambda'()>(long) SemaLookup.cpp:0:0
#11 0x0000000007cc5691 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7cc5691)
#12 0x0000000006b5a743 clang::Sema::LookupConstructors(clang::CXXRecordDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6b5a743)
#13 0x0000000006b2b082 TryConstructorInitialization(clang::Sema&, clang::InitializedEntity const&, clang::InitializationKind const&, llvm::MutableArrayRef<clang::Expr*>, clang::QualType, clang::QualType, clang::InitializationSequence&, bool, bool) SemaInit.cpp:0:0
#14 0x0000000006b33fad clang::InitializationSequence::InitializeFrom(clang::Sema&, clang::InitializedEntity const&, clang::InitializationKind const&, llvm::MutableArrayRef<clang::Expr*>, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6b33fad)
#15 0x00000000068696ec clang::Sema::BuildMemberInitializer(clang::ValueDecl*, clang::Expr*, clang::SourceLocation) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x68696ec)
#16 0x000000000707a618 clang::Sema::InstantiateMemInitializers(clang::CXXConstructorDecl*, clang::CXXConstructorDecl const*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x707a618)
#17 0x00000000070806c2 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x70806c2)
#18 0x000000000707e65e clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x707e65e)
#19 0x00000000070809a6 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x70809a6)
#20 0x000000000707e65e clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x707e65e)
#21 0x00000000070809a6 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x70809a6)
#22 0x000000000707e65e clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x707e65e)
#23 0x00000000070809a6 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x70809a6)
#24 0x000000000707e65e clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x707e65e)
#25 0x00000000065f1fed clang::Sema::ActOnEndOfTranslationUnitFragment(clang::Sema::TUFragmentKind) (.part.0) Sema.cpp:0:0
#26 0x00000000065f27e2 clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x65f27e2)
#27 0x000000000645d0eb clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x645d0eb)
#28 0x000000000644f4aa clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x644f4aa)
#29 0x0000000004807ea8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4807ea8)
#30 0x0000000004ace8f5 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ace8f5)
#31 0x0000000004a522be clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4a522be)
#32 0x0000000004bbd20e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4bbd20e)
#33 0x0000000000d4c6cf cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd4c6cf)
#34 0x0000000000d4414a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#35 0x000000000484e489 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#36 0x0000000003dbc8a4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3dbc8a4)
#37 0x000000000484ea7f clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#38 0x0000000004811bfd clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4811bfd)
#39 0x0000000004812c7e clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4812c7e)
#40 0x000000000481ac35 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x481ac35)
#41 0x0000000000d494c3 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd494c3)
#42 0x0000000000c11f74 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc11f74)
#43 0x000076b807829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#44 0x000076b807829e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#45 0x0000000000d43bf5 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd43bf5)
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsW19z2zYS_zTwC0YaEvyrBz_IstVLL574YreXNw0ILiXUJMEAoCP34T77DUBS_GM5sTp2m1wv045pEFj89reL3QUIU6X4tgQ4R8EFCi7PaK13Qp6Dch3inyUifTz_CJUUac1AYkTindaVQt4SkTUi661IE5HruZBbRNa_I7KOFp_Dn1RKFSIL081ZotBp_3PaUbgQaZ3DnFVVgZwl7CshddtofiDvom-mtRa4oPewyWmRpBSRmJca7xFZYBSZjhhL0LUsMQou9ii4NM1d0x55FxhFl41I8-AsexwalJ4zhpwlLzoMTc8HwVP7GpF4OJNFk2HkXU4wOVbfQaet7ZSNJh4w8cghT9WkscPjLX3kLWPkLTFIKaR5KA0Lmu14ucVMlErLmmkhcSYk5iXXnOb8d6q5KLHIMCIRInGDDlONEVnTqkJkPaTeW3rIW7oWOoksdIx9jKIVPqYEtv-at81jcIUx_g9ylm6DE2-hBEk1pPOp4Z3lEite8JxKDHtaVDngLzuQgL8A5qXSQFPMJFANmGKlU-M83jKrS2Z10pJvtyAVpiWmSoG0rS9wyGT322fpB8nVj-WQiHi8ZHmdAkbeqqOB5si7erm7PqEReStemk5mg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