Skip to content

libc++ makes wrong assumptions in std::pair constructor #65620

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

Closed
grisumbras opened this issue Sep 7, 2023 · 9 comments · Fixed by llvm/llvm-project-release-prs#702
Closed
Assignees
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Comments

@grisumbras
Copy link

With latest libc++ this code doesn't compile in clang with -std=c++23.

#include <type_traits>
#include <utility>

struct kvp {
  kvp() = default;
  explicit kvp(std::pair<int, int> const& p);
};

namespace std {

template <>
struct tuple_size<kvp> : std::integral_constant<std::size_t, 2> {};

template <>
struct tuple_element<0, kvp> {
  using type = int;
};

template <>
struct tuple_element<1, kvp> {
  using type = int;
};

} // namespace std


static_assert(std::is_nothrow_move_constructible<kvp>::value);

As far as I can tell, when checking wether kvp is nothrow move constructible, the compiler tries instantiating this std::pair constructor( https://github.com/llvm/llvm-project/blob/main/libcxx/include/__utility/pair.h#L286-L291 ).
That in turn tries to compile this code (https://github.com/llvm/llvm-project/blob/main/libcxx/include/__utility/pair.h#L279-L282), which assumes that std::tuple_size<T>::value == 2 means that std::get<0>(T) and std::get<1>(T) will work.

For context, in the original code this type had get overloads in its own namespace, but not in namespace std.

@ldionne
Copy link
Member

ldionne commented Sep 15, 2023

From #65907 (comment) by @huixie90 , the fix seems pretty simple. @huixie90 Do you want to take this?

@ldionne
Copy link
Member

ldionne commented Sep 15, 2023

I think this is an obvious candidate for cherry-picking back to LLVM 17. We will miss 17.0.0, but we should still cherry-pick to release/17.x in order to make 17.0.1. Thanks for reporting this issue (and sorry for introducing it)!

huixie90 added a commit to huixie90/llvm-project that referenced this issue Sep 18, 2023
Fixes llvm#65620

GitHub Issue:
llvm#65620

The helper function `__pair_like_explicit_wknd` is only SFINAE-ed with `tuple_size<remove_cvref_t<_PairLike>>::value == 2`, but its function body assumes `std::get` being valid
https://github.com/llvm/llvm-project/blame/main/libcxx/include/__utility/pair.h#L280
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this issue Sep 19, 2023
…on (llvm#66585)

The helper function `__pair_like_explicit_wknd` is only SFINAE-ed with
`tuple_size<remove_cvref_t<_PairLike>>::value == 2`, but its function
body assumes `std::get` being valid.

Fixes llvm#65620
@huixie90
Copy link
Contributor

/cherry-pick 054f9c5

@huixie90 huixie90 added this to the LLVM 17.0.X Release milestone Sep 20, 2023
@huixie90
Copy link
Contributor

/cherry-pick 054f9c5

@llvmbot
Copy link
Member

llvmbot commented Sep 20, 2023

/branch llvm/llvm-project-release-prs/issue65620

1 similar comment
@llvmbot
Copy link
Member

llvmbot commented Sep 20, 2023

/branch llvm/llvm-project-release-prs/issue65620

@llvmbot
Copy link
Member

llvmbot commented Sep 20, 2023

/pull-request llvm/llvm-project-release-prs#702

@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Sep 28, 2023
@tru tru moved this from Needs Triage to Needs Review in LLVM Release Status Sep 28, 2023
@tru
Copy link
Collaborator

tru commented Sep 29, 2023

/cherry-pick 054f9c5

(trying to get the CI to behave)

@llvmbot
Copy link
Member

llvmbot commented Sep 29, 2023

/branch llvm/llvm-project-release-prs/issue65620

tru pushed a commit that referenced this issue Oct 3, 2023
…on (#66585)

The helper function `__pair_like_explicit_wknd` is only SFINAE-ed with
`tuple_size<remove_cvref_t<_PairLike>>::value == 2`, but its function
body assumes `std::get` being valid.

Fixes #65620

(cherry picked from commit 054f9c5)
@tru tru moved this from Needs Review to Done in LLVM Release Status Oct 4, 2023
zahiraam pushed a commit to tahonermann/llvm-project that referenced this issue Oct 24, 2023
…on (llvm#66585)

The helper function `__pair_like_explicit_wknd` is only SFINAE-ed with
`tuple_size<remove_cvref_t<_PairLike>>::value == 2`, but its function
body assumes `std::get` being valid.

Fixes llvm#65620
zahiraam pushed a commit to tahonermann/llvm-project that referenced this issue Oct 24, 2023
…on (llvm#66585)

The helper function `__pair_like_explicit_wknd` is only SFINAE-ed with
`tuple_size<remove_cvref_t<_PairLike>>::value == 2`, but its function
body assumes `std::get` being valid.

Fixes llvm#65620
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
Development

Successfully merging a pull request may close this issue.

6 participants