https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109277
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |ppalka at gcc dot gnu.org
--- Comment #9 from Patrick Palka <ppalka at gcc dot gnu.org> ---
AFAICT the trait instantiation is legitimate, and this appears to be UB, here's
a boiled down testcase:
#include <tuple>
#include <type_traits>
struct Object;
struct MaybeObject;
struct WasmArray;
template<class T, class U>
struct is_subtype {
static const bool value =
std::is_base_of<U, T>::value || (std::is_same<U, MaybeObject>::value &&
std::is_convertible<T, Object>::value);
};
template<class T>
struct TNode {
TNode();
template<class U, typename = std::enable_if_t<is_subtype<U, T>::value>>
TNode(const TNode<U>&); // #1
TNode(const TNode&); // #2
};
std::tuple<TNode<WasmArray>> node;
The instantiation of std::is_convertible<WasmArray, Object> happens when
synthesizing tuple<TNode<WasmArray>> defaulted move constructor, for which we
need to perform overload resolution of TNode's constructors with a TNode&&
argument, and when considering the template candidate #1 we need to instantiate
its default template argument which entails instantiation of is_convertible.
In GCC 12 is_convertible for an incomplete type would silently return false.
In GCC 13 the new built-in __is_convertible diagnoses this UB situation as a
hard error. Clang's __is_convertible behaves like GCC 12's is_convertible it
seems.
One fix is to define a move constructor for TNode, which causes GCC's perfect
candidate optimization (r11-7287-g187d0d5871b1fa) to kick in and avoid
considering the template candidate #1. Another fix is to use
std::conjunction/disjunction in is_subtype so that the condition properly
short-circuits (is_base_of<T, T> is true even for incomplete T).