diff mbox series

c++: Fix __is_*constructible/assignable for templates [PR102305]

Message ID 20210914084808.GU304296@tucnak
State New
Headers show
Series c++: Fix __is_*constructible/assignable for templates [PR102305] | expand

Commit Message

Jakub Jelinek Sept. 14, 2021, 8:48 a.m. UTC
Hi!

is_xible_helper returns error_mark_node (i.e. false from the traits)
for abstract classes by testing ABSTRACT_CLASS_TYPE_P (to) early.
Unfortunately, as the testcase shows, that doesn't work on class templates
that haven't been instantiated yet, ABSTRACT_CLASS_TYPE_P for them is false
until it is instantiated, which is done when the routine later constructs
a dummy object with that type.

The following patch fixes this by calling complete_type first, so that
ABSTRACT_CLASS_TYPE_P test will work properly, while keeping the handling
of arrays with unknown bounds, or incomplete types where it is done
currently.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2021-09-14  Jakub Jelinek  <jakub@redhat.com>

	PR c++/102305
	* method.c (is_xible_helper): Call complete_type on to.

	* g++.dg/cpp0x/pr102305.C: New test.


	Jakub

Comments

Jason Merrill Sept. 14, 2021, 2:52 p.m. UTC | #1
On 9/14/21 4:48 AM, Jakub Jelinek wrote:
> is_xible_helper returns error_mark_node (i.e. false from the traits)
> for abstract classes by testing ABSTRACT_CLASS_TYPE_P (to) early.
> Unfortunately, as the testcase shows, that doesn't work on class templates
> that haven't been instantiated yet, ABSTRACT_CLASS_TYPE_P for them is false
> until it is instantiated, which is done when the routine later constructs
> a dummy object with that type.
> 
> The following patch fixes this by calling complete_type first, so that
> ABSTRACT_CLASS_TYPE_P test will work properly, while keeping the handling
> of arrays with unknown bounds, or incomplete types where it is done
> currently.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

> 2021-09-14  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/102305
> 	* method.c (is_xible_helper): Call complete_type on to.
> 
> 	* g++.dg/cpp0x/pr102305.C: New test.
> 
> --- gcc/cp/method.c.jj	2021-08-12 09:34:16.465241411 +0200
> +++ gcc/cp/method.c	2021-09-13 12:29:05.398000237 +0200
> @@ -2081,6 +2081,7 @@ constructible_expr (tree to, tree from)
>   static tree
>   is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
>   {
> +  to = complete_type (to);
>     deferring_access_check_sentinel acs (dk_no_deferred);
>     if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
>         || (from && FUNC_OR_METHOD_TYPE_P (from)
> --- gcc/testsuite/g++.dg/cpp0x/pr102305.C.jj	2021-09-13 12:46:47.580332966 +0200
> +++ gcc/testsuite/g++.dg/cpp0x/pr102305.C	2021-09-13 12:49:00.072503394 +0200
> @@ -0,0 +1,39 @@
> +// PR c++/102305
> +// { dg-do compile { target c++11 } }
> +
> +namespace std
> +{
> +  template<typename _Tp, _Tp __v>
> +    struct integral_constant
> +    {
> +      static constexpr _Tp value = __v;
> +      typedef integral_constant<_Tp, __v> type;
> +    };
> +
> +  template<typename _Tp, _Tp __v>
> +    constexpr _Tp integral_constant<_Tp, __v>::value;
> +
> +  typedef integral_constant<bool, true> true_type;
> +  typedef integral_constant<bool, false> false_type;
> +
> +  template<bool __v>
> +    using bool_constant = integral_constant<bool, __v>;
> +
> +  template<typename _Tp, typename... _Args>
> +    struct is_constructible
> +    : public bool_constant<__is_constructible(_Tp, _Args...)>
> +    {
> +    };
> +}
> +
> +template<typename>
> +struct A {
> +  virtual ~A() = 0;
> +};
> +
> +struct B {
> +  virtual ~B() = 0;
> +};
> +
> +static_assert(!std::is_constructible<A<int> >::value, "");
> +static_assert(!std::is_constructible<B>::value, "");
> 
> 	Jakub
>
diff mbox series

Patch

--- gcc/cp/method.c.jj	2021-08-12 09:34:16.465241411 +0200
+++ gcc/cp/method.c	2021-09-13 12:29:05.398000237 +0200
@@ -2081,6 +2081,7 @@  constructible_expr (tree to, tree from)
 static tree
 is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
 {
+  to = complete_type (to);
   deferring_access_check_sentinel acs (dk_no_deferred);
   if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
       || (from && FUNC_OR_METHOD_TYPE_P (from)
--- gcc/testsuite/g++.dg/cpp0x/pr102305.C.jj	2021-09-13 12:46:47.580332966 +0200
+++ gcc/testsuite/g++.dg/cpp0x/pr102305.C	2021-09-13 12:49:00.072503394 +0200
@@ -0,0 +1,39 @@ 
+// PR c++/102305
+// { dg-do compile { target c++11 } }
+
+namespace std
+{
+  template<typename _Tp, _Tp __v>
+    struct integral_constant
+    {
+      static constexpr _Tp value = __v;
+      typedef integral_constant<_Tp, __v> type;
+    };
+
+  template<typename _Tp, _Tp __v>
+    constexpr _Tp integral_constant<_Tp, __v>::value;
+
+  typedef integral_constant<bool, true> true_type;
+  typedef integral_constant<bool, false> false_type;
+
+  template<bool __v>
+    using bool_constant = integral_constant<bool, __v>;
+
+  template<typename _Tp, typename... _Args>
+    struct is_constructible
+    : public bool_constant<__is_constructible(_Tp, _Args...)>
+    {
+    };
+}
+
+template<typename>
+struct A {
+  virtual ~A() = 0;
+};
+
+struct B {
+  virtual ~B() = 0;
+};
+
+static_assert(!std::is_constructible<A<int> >::value, "");
+static_assert(!std::is_constructible<B>::value, "");