Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++23][Modules] imported function with auto return type causes lambda does not satisfy 'move_constructible' #130080

Open
thbwd opened this issue Mar 6, 2025 · 6 comments
Labels
clang:modules C++20 modules and Clang Header Modules lambda C++11 lambda expressions

Comments

@thbwd
Copy link

thbwd commented Mar 6, 2025

A.ixx:

module;

#include <vector>
#include <ranges>

export module A;

export auto f(const std::vector<int> &vec) {
    return vec | std::views::transform([](int x) {
        return x + 1;
    });
}

/*void clang_bug_fix() {
    for (auto x : f(std::vector<int>()));
}*/

main.cpp:

#include <vector>

import A;

int main() {
    for (int ptr : f(std::vector<int>(5))) {
    }
}

Produces:

/opt/homebrew/Cellar/llvm/19.1.7_1/bin/clang++   -g -std=gnu++23 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -fcolor-diagnostics -MD -MT CMakeFiles/test.dir/main.cpp.o -MF CMakeFiles/test.dir/main.cpp.o.d @CMakeFiles/test.dir/main.cpp.o.modmap -o CMakeFiles/test.dir/main.cpp.o -c path/main.cpp
In module 'A' imported from path/main.cpp:3:
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:184:16: error: constraints not satisfied for class template 'transform_view' [with _View = std::ranges::ref_view<const std::vector<int>>, _Fn = (lambda at path/A.ixx:9:40)]
  184 |   friend class transform_view<_View, _Fn>::__iterator;
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:97:53: note: in instantiation of template class 'std::ranges::transform_view<std::ranges::ref_view<const std::vector<int>>, (lambda at path/A.ixx:9:40)>::__iterator<false>' requested here
   97 |   _LIBCPP_HIDE_FROM_ABI constexpr __iterator<false> begin() { return __iterator<false>{*this, ranges::begin(__base_)}; }
      |                                                     ^
path/main.cpp:6:18: note: in instantiation of member function 'std::ranges::transform_view<std::ranges::ref_view<const std::vector<int>>, (lambda at path/A.ixx:9:40)>::begin' requested here
    6 |     for (int ptr : f(std::vector<int>(5))) {
      |                  ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:68:30: note: because '(lambda at path/A.ixx:9:40)' does not satisfy 'move_constructible'
   68 | template <input_range _View, move_constructible _Fn>
      |                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/constructible.h:39:62: note: because 'convertible_to<(lambda at path/A.ixx:9:40), (lambda at path/A.ixx:9:40)>' evaluated to false
   39 | concept move_constructible = constructible_from<_Tp, _Tp> && convertible_to<_Tp, _Tp>;
      |                                                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/convertible_to.h:27:69: note: because 'static_cast<_To>(std::declval<_From>())' would be invalid: no matching conversion for static_cast from '(lambda at path/A.ixx:9:40)' to '(lambda at path/A.ixx:9:40)'
   27 | concept convertible_to = is_convertible_v<_From, _To> && requires { static_cast<_To>(std::declval<_From>()); };
      |                                                                     ^
In module 'A' imported from path/main.cpp:3:
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:187:16: error: constraints not satisfied for class template 'transform_view' [with _View = std::ranges::ref_view<const std::vector<int>>, _Fn = (lambda at path/A.ixx:9:40)]
  187 |   friend class transform_view<_View, _Fn>::__sentinel;
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:68:30: note: because '(lambda at path/A.ixx:9:40)' does not satisfy 'move_constructible'
   68 | template <input_range _View, move_constructible _Fn>
      |                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/constructible.h:39:62: note: because 'convertible_to<(lambda at path/A.ixx:9:40), (lambda at path/A.ixx:9:40)>' evaluated to false
   39 | concept move_constructible = constructible_from<_Tp, _Tp> && convertible_to<_Tp, _Tp>;
      |                                                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/convertible_to.h:27:69: note: because 'static_cast<_To>(std::declval<_From>())' would be invalid: no matching conversion for static_cast from '(lambda at path/A.ixx:9:40)' to '(lambda at path/A.ixx:9:40)'
   27 | concept convertible_to = is_convertible_v<_From, _To> && requires { static_cast<_To>(std::declval<_From>()); };
      |                                                                     ^
2 errors generated.

Even though the lambda is move constructible. The bug disappears when uncommenting the function clang_bug_fix.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.30)
project(test)

set(CMAKE_CXX_STANDARD 23)

add_executable(test main.cpp)

target_sources(test
        PUBLIC
        FILE_SET all_my_modules TYPE CXX_MODULES FILES
        A.ixx
)
@frederick-vs-ja frederick-vs-ja added clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules and removed new issue clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 6, 2025

@llvm/issue-subscribers-clang-modules

Author: None (thbwd)

A.ixx: ```c++ module;

#include <vector>
#include <ranges>

export module A;

export auto f(const std::vector<int> &vec) {
return vec | std::views::transform([](int x) {
return x + 1;
});
}

/void clang_bug_fix() {
for (auto x : f(std::vector<int>()));
}
/

main.cpp:
```c++
#include &lt;vector&gt;

import A;

int main() {
    for (int ptr : f(std::vector&lt;int&gt;(5))) {
    }
}

Produces:

/opt/homebrew/Cellar/llvm/19.1.7_1/bin/clang++   -g -std=gnu++23 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -fcolor-diagnostics -MD -MT CMakeFiles/test.dir/main.cpp.o -MF CMakeFiles/test.dir/main.cpp.o.d @<!-- -->CMakeFiles/test.dir/main.cpp.o.modmap -o CMakeFiles/test.dir/main.cpp.o -c path/main.cpp
In module 'A' imported from path/main.cpp:3:
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:184:16: error: constraints not satisfied for class template 'transform_view' [with _View = std::ranges::ref_view&lt;const std::vector&lt;int&gt;&gt;, _Fn = (lambda at path/A.ixx:9:40)]
  184 |   friend class transform_view&lt;_View, _Fn&gt;::__iterator;
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:97:53: note: in instantiation of template class 'std::ranges::transform_view&lt;std::ranges::ref_view&lt;const std::vector&lt;int&gt;&gt;, (lambda at path/A.ixx:9:40)&gt;::__iterator&lt;false&gt;' requested here
   97 |   _LIBCPP_HIDE_FROM_ABI constexpr __iterator&lt;false&gt; begin() { return __iterator&lt;false&gt;{*this, ranges::begin(__base_)}; }
      |                                                     ^
path/main.cpp:6:18: note: in instantiation of member function 'std::ranges::transform_view&lt;std::ranges::ref_view&lt;const std::vector&lt;int&gt;&gt;, (lambda at path/A.ixx:9:40)&gt;::begin' requested here
    6 |     for (int ptr : f(std::vector&lt;int&gt;(5))) {
      |                  ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:68:30: note: because '(lambda at path/A.ixx:9:40)' does not satisfy 'move_constructible'
   68 | template &lt;input_range _View, move_constructible _Fn&gt;
      |                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/constructible.h:39:62: note: because 'convertible_to&lt;(lambda at path/A.ixx:9:40), (lambda at path/A.ixx:9:40)&gt;' evaluated to false
   39 | concept move_constructible = constructible_from&lt;_Tp, _Tp&gt; &amp;&amp; convertible_to&lt;_Tp, _Tp&gt;;
      |                                                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/convertible_to.h:27:69: note: because 'static_cast&lt;_To&gt;(std::declval&lt;_From&gt;())' would be invalid: no matching conversion for static_cast from '(lambda at path/A.ixx:9:40)' to '(lambda at path/A.ixx:9:40)'
   27 | concept convertible_to = is_convertible_v&lt;_From, _To&gt; &amp;&amp; requires { static_cast&lt;_To&gt;(std::declval&lt;_From&gt;()); };
      |                                                                     ^
In module 'A' imported from path/main.cpp:3:
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:187:16: error: constraints not satisfied for class template 'transform_view' [with _View = std::ranges::ref_view&lt;const std::vector&lt;int&gt;&gt;, _Fn = (lambda at path/A.ixx:9:40)]
  187 |   friend class transform_view&lt;_View, _Fn&gt;::__sentinel;
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:68:30: note: because '(lambda at path/A.ixx:9:40)' does not satisfy 'move_constructible'
   68 | template &lt;input_range _View, move_constructible _Fn&gt;
      |                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/constructible.h:39:62: note: because 'convertible_to&lt;(lambda at path/A.ixx:9:40), (lambda at path/A.ixx:9:40)&gt;' evaluated to false
   39 | concept move_constructible = constructible_from&lt;_Tp, _Tp&gt; &amp;&amp; convertible_to&lt;_Tp, _Tp&gt;;
      |                                                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/convertible_to.h:27:69: note: because 'static_cast&lt;_To&gt;(std::declval&lt;_From&gt;())' would be invalid: no matching conversion for static_cast from '(lambda at path/A.ixx:9:40)' to '(lambda at path/A.ixx:9:40)'
   27 | concept convertible_to = is_convertible_v&lt;_From, _To&gt; &amp;&amp; requires { static_cast&lt;_To&gt;(std::declval&lt;_From&gt;()); };
      |                                                                     ^
2 errors generated.

Even though the lambda is move constructible. The bug disappears when uncommenting the function clang_bug_fix.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.30)
project(test)

set(CMAKE_CXX_STANDARD 23)

add_executable(test main.cpp)

target_sources(test
        PUBLIC
        FILE_SET all_my_modules TYPE CXX_MODULES FILES
        A.ixx
)

@llvmbot
Copy link
Member

llvmbot commented Mar 6, 2025

@llvm/issue-subscribers-clang-frontend

Author: None (thbwd)

A.ixx: ```c++ module;

#include <vector>
#include <ranges>

export module A;

export auto f(const std::vector<int> &vec) {
return vec | std::views::transform([](int x) {
return x + 1;
});
}

/void clang_bug_fix() {
for (auto x : f(std::vector<int>()));
}
/

main.cpp:
```c++
#include &lt;vector&gt;

import A;

int main() {
    for (int ptr : f(std::vector&lt;int&gt;(5))) {
    }
}

Produces:

/opt/homebrew/Cellar/llvm/19.1.7_1/bin/clang++   -g -std=gnu++23 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -fcolor-diagnostics -MD -MT CMakeFiles/test.dir/main.cpp.o -MF CMakeFiles/test.dir/main.cpp.o.d @<!-- -->CMakeFiles/test.dir/main.cpp.o.modmap -o CMakeFiles/test.dir/main.cpp.o -c path/main.cpp
In module 'A' imported from path/main.cpp:3:
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:184:16: error: constraints not satisfied for class template 'transform_view' [with _View = std::ranges::ref_view&lt;const std::vector&lt;int&gt;&gt;, _Fn = (lambda at path/A.ixx:9:40)]
  184 |   friend class transform_view&lt;_View, _Fn&gt;::__iterator;
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:97:53: note: in instantiation of template class 'std::ranges::transform_view&lt;std::ranges::ref_view&lt;const std::vector&lt;int&gt;&gt;, (lambda at path/A.ixx:9:40)&gt;::__iterator&lt;false&gt;' requested here
   97 |   _LIBCPP_HIDE_FROM_ABI constexpr __iterator&lt;false&gt; begin() { return __iterator&lt;false&gt;{*this, ranges::begin(__base_)}; }
      |                                                     ^
path/main.cpp:6:18: note: in instantiation of member function 'std::ranges::transform_view&lt;std::ranges::ref_view&lt;const std::vector&lt;int&gt;&gt;, (lambda at path/A.ixx:9:40)&gt;::begin' requested here
    6 |     for (int ptr : f(std::vector&lt;int&gt;(5))) {
      |                  ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:68:30: note: because '(lambda at path/A.ixx:9:40)' does not satisfy 'move_constructible'
   68 | template &lt;input_range _View, move_constructible _Fn&gt;
      |                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/constructible.h:39:62: note: because 'convertible_to&lt;(lambda at path/A.ixx:9:40), (lambda at path/A.ixx:9:40)&gt;' evaluated to false
   39 | concept move_constructible = constructible_from&lt;_Tp, _Tp&gt; &amp;&amp; convertible_to&lt;_Tp, _Tp&gt;;
      |                                                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/convertible_to.h:27:69: note: because 'static_cast&lt;_To&gt;(std::declval&lt;_From&gt;())' would be invalid: no matching conversion for static_cast from '(lambda at path/A.ixx:9:40)' to '(lambda at path/A.ixx:9:40)'
   27 | concept convertible_to = is_convertible_v&lt;_From, _To&gt; &amp;&amp; requires { static_cast&lt;_To&gt;(std::declval&lt;_From&gt;()); };
      |                                                                     ^
In module 'A' imported from path/main.cpp:3:
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:187:16: error: constraints not satisfied for class template 'transform_view' [with _View = std::ranges::ref_view&lt;const std::vector&lt;int&gt;&gt;, _Fn = (lambda at path/A.ixx:9:40)]
  187 |   friend class transform_view&lt;_View, _Fn&gt;::__sentinel;
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__ranges/transform_view.h:68:30: note: because '(lambda at path/A.ixx:9:40)' does not satisfy 'move_constructible'
   68 | template &lt;input_range _View, move_constructible _Fn&gt;
      |                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/constructible.h:39:62: note: because 'convertible_to&lt;(lambda at path/A.ixx:9:40), (lambda at path/A.ixx:9:40)&gt;' evaluated to false
   39 | concept move_constructible = constructible_from&lt;_Tp, _Tp&gt; &amp;&amp; convertible_to&lt;_Tp, _Tp&gt;;
      |                                                              ^
/opt/homebrew/Cellar/llvm/19.1.7_1/bin/../include/c++/v1/__concepts/convertible_to.h:27:69: note: because 'static_cast&lt;_To&gt;(std::declval&lt;_From&gt;())' would be invalid: no matching conversion for static_cast from '(lambda at path/A.ixx:9:40)' to '(lambda at path/A.ixx:9:40)'
   27 | concept convertible_to = is_convertible_v&lt;_From, _To&gt; &amp;&amp; requires { static_cast&lt;_To&gt;(std::declval&lt;_From&gt;()); };
      |                                                                     ^
2 errors generated.

Even though the lambda is move constructible. The bug disappears when uncommenting the function clang_bug_fix.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.30)
project(test)

set(CMAKE_CXX_STANDARD 23)

add_executable(test main.cpp)

target_sources(test
        PUBLIC
        FILE_SET all_my_modules TYPE CXX_MODULES FILES
        A.ixx
)

@frederick-vs-ja
Copy link
Contributor

Is this reproducible when replacing the lambda with an equivalent hand-write function object?

@thbwd
Copy link
Author

thbwd commented Mar 6, 2025

I didn't specifically test a separate function but just converting the lambda to a function pointer with the unary + makes the bug go away:

export auto f(const std::vector<int> &vec) {
    return vec | std::views::transform(+[](int x) {
        return x + 1;
    });
}

@thbwd
Copy link
Author

thbwd commented Mar 6, 2025

Ah, I think I misunderstood. I haven't tried writing an equivalent "functor" so far.

@frederick-vs-ja frederick-vs-ja added the lambda C++11 lambda expressions label Mar 6, 2025
@ChuanqiXu9
Copy link
Member

I am not sure if this relates to #116087

Can you verify by adding an inline to f?

And also -fmodules-reduced-bmi may be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:modules C++20 modules and Clang Header Modules lambda C++11 lambda expressions
Projects
None yet
Development

No branches or pull requests

4 participants