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

Clang incorrectly instantiates conditional-explicit even when constraints are not satisfied #60710

Closed
ldionne opened this issue Feb 13, 2023 · 4 comments
Labels
c++23 clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts rejects-valid

Comments

@ldionne
Copy link
Member

ldionne commented Feb 13, 2023

In code like the following, Clang will instantiate the content of the conditional-explicit even when the constraint is not satisfied:

template <some-constraint T>
  explicit(some-expression-involving-T)
constructor(T const&) { }

As a result, if some-expression-involving-T is not well-formed, Clang will issue a hard error even if some-constraint is not satisfied, in which case one would have expected that the constructor is removed from the overload sets instead.

Full reproducer:

// clang++ -xc++ - -std=c++2b -fsyntax-only
#include <type_traits>
#include <utility>

template <class ...T>
struct tuple { };

template <class T>            struct IsPairLikeImpl : std::false_type { };
template <class T1, class T2> struct IsPairLikeImpl<tuple<T1, T2>> : std::true_type { };
template <class T>            concept IsPairLike = IsPairLikeImpl<T>::value;

template <class ...T>
constexpr auto f(tuple<T...> const&) {
    static_assert(sizeof...(T) == 2);
}

template <class T1, class T2>
struct pair {
    template <IsPairLike P>
    explicit(std::is_void_v<decltype(f(std::declval<P>()))>)
    pair(P const&) { }
};

template <class T>
concept can_construct_pair_from = requires (T t) {
    pair<int, int>(t);
};

static_assert(!can_construct_pair_from<tuple<int>>);
static_assert( can_construct_pair_from<tuple<int, int>>);
static_assert(!can_construct_pair_from<tuple<int, int, int>>);

int main() { }

Godbolt link: https://godbolt.org/z/7PjGqdjes

GCC and MSVC accept this code, Clang doesn't. This was discovered while trying to implement pair-like constructors for libc++.

@ldionne ldionne added clang:frontend Language frontend issues, e.g. anything involving "Sema" rejects-valid labels Feb 13, 2023
@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2023

@llvm/issue-subscribers-clang-frontend

@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2023

@llvm/issue-subscribers-c-2b

@JMazurkiewicz
Copy link
Contributor

This is possibly duplicate of #59827.

@EugeneZelenko EugeneZelenko added the concepts C++20 concepts label Feb 13, 2023
@LYP951018
Copy link
Contributor

It seems that this bug has been fixed by #70548 . The code works with clang trunk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++23 clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts rejects-valid
Projects
Status: Done
Development

No branches or pull requests

5 participants