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

    <tr>
        <th>Summary</th>
        <td>
            [libc++] std::variant invokes move constructor of non-movable alternative in copy assignment
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

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

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

<pre>
    Link to godbolt example: https://godbolt.org/z/r4as6sejq

```
struct CopyOnly {
    explicit CopyOnly(const std::string& str) {}
    CopyOnly(const CopyOnly&) = default;
    CopyOnly(CopyOnly&&) = delete;
    CopyOnly& operator=(const CopyOnly&) = default;
    CopyOnly& operator=(CopyOnly&&) = delete;

    std::string str;
};

int main() {
 std::variant<std::string, CopyOnly> v;
    std::variant<std::string, CopyOnly> v2;
    v2 = v; // Compile error

    return 0;
}
```

Compilation error:

```
In file included from <source>:1:
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:709:9: error: call to deleted constructor of 'std::__variant_detail::__alt<1, CopyOnly>::__value_type' (aka 'CopyOnly')
      : __value(_VSTD::forward<_Args>(__args)...) {}
        ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:877:9: note: in instantiation of function template specialization 'std::__variant_detail::__alt<1, CopyOnly>::__alt<CopyOnly>' requested here
 __alt<_Ip, _Tp>(in_place, _VSTD::forward<_Args>(__args)...);
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:990:25: note: in instantiation of function template specialization 'std::__variant_detail::__ctor<std::__variant_detail::__traits<std::string, CopyOnly>>::__construct_alt<1UL, CopyOnly, CopyOnly>' requested here
 auto& __res = this->__construct_alt(__access::__base::__get_alt<_Ip>(*this),
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:1003:7: note: in instantiation of member function 'std::__variant_detail::__assignment<std::__variant_detail::__traits<std::string, CopyOnly>>::__assign_alt(__alt<1UL, CopyOnly> &, const CopyOnly &)::(anonymous struct)::operator()' requested here
      struct {
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:1029:19: note: in instantiation of function template specialization 'std::__variant_detail::__assignment<std::__variant_detail::__traits<std::string, CopyOnly>>::__assign_alt<1UL, CopyOnly, const CopyOnly &>' requested here
 this->__assign_alt(
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:1108:13: note: in instantiation of function template specialization 'std::__variant_detail::__assignment<std::__variant_detail::__traits<std::string, CopyOnly>>::__generic_assign<const std::__variant_detail::__copy_assignment<std::__variant_detail::__traits<std::string, CopyOnly>, std::__variant_detail::_Trait::_Available> &>' requested here
 this->__generic_assign(__that);
 ^
/opt/compiler-explorer/clang-14.0.0/bin/../include/c++/v1/variant:1119:28: note: in instantiation of member function 'std::__variant_detail::__copy_assignment<std::__variant_detail::__traits<std::string, CopyOnly>, std::__variant_detail::_Trait::_Available>::operator=' requested here
class _LIBCPP_TEMPLATE_VIS __impl
 ^
<source>:8:5: note: 'CopyOnly' has been explicitly marked deleted here
 CopyOnly(CopyOnly&&) = delete;
    ^
1 error generated.
Compiler returned: 1
```

Compilation error does not occur in libstdc++. I'd expect this to compile without issue, invoking the copy constructor/assignment of the non-movable alternative where needed.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzcWFFv2zYQ_jX0yyGGRNmS_eAHxbaAABlWoFlfBUo622woUiUpp-nDfvtASrZsx13abgmGFWosSLzj8fu-O_LEjOFbibgg01syXY1Ya3dKL552aMyoUNXz4p7LR7AKtqoqlLCAX1ndCCRRCjtrG0OilNCM0KwfMFZ6S2j2jdBMT5iJDX7-QoLUXXHQX0FqrG5LC0vVPP8uxTOQ5JYEKQAAfm0EL_nwjtBZqaSxYGzlJoucMZdbQmMwVhM699bJqnfwwm54EPvB0Q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