c++: Fix ICE with static_cast when converting from int[] [PR93862]

Message ID 20200224204147.415565-1-polacek@redhat.com
State New
Headers show
Series
  • c++: Fix ICE with static_cast when converting from int[] [PR93862]
Related show

Commit Message

Marek Polacek Feb. 24, 2020, 8:41 p.m.
This ICEs since my patch for P0388, which allowed conversions to arrays
of unknown bound, but not the reverse, so these two static_casts are
ill-formed.

[expr.static.cast]/3 says that "cv1 T1" and "cv2 T2" have to be
reference-compatible and the comment in build_static_cast_1 says it too
but then we actually use reference_related_p...  Fixed thus.

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

2020-02-24  Marek Polacek  <polacek@redhat.com>

	PR c++/93862 - ICE with static_cast when converting from int[].
	* call.c (reference_compatible_p): No longer static.
	* cp-tree.h (reference_compatible_p): Declare.
	* typeck.c (build_static_cast_1): Use reference_compatible_p instead
	if reference_related_p.

	* g++.dg/cpp0x/rv-cast7.C: New test.
---
 gcc/cp/call.c                         | 3 +--
 gcc/cp/cp-tree.h                      | 1 +
 gcc/cp/typeck.c                       | 2 +-
 gcc/testsuite/g++.dg/cpp0x/rv-cast7.C | 6 ++++++
 4 files changed, 9 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/rv-cast7.C


base-commit: a4dbb9b25a60143c699de55cd6226cebeb3b3c3e
-- 
Marek Polacek • Red Hat, Inc. • 300 A St, Boston, MA

Comments

Marek Polacek Feb. 24, 2020, 8:44 p.m. | #1
On Mon, Feb 24, 2020 at 03:41:47PM -0500, Marek Polacek wrote:
> This ICEs since my patch for P0388, which allowed conversions to arrays

> of unknown bound, but not the reverse, so these two static_casts are

> ill-formed.

> 

> [expr.static.cast]/3 says that "cv1 T1" and "cv2 T2" have to be

> reference-compatible and the comment in build_static_cast_1 says it too

> but then we actually use reference_related_p...  Fixed thus.

> 

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

> 

> 2020-02-24  Marek Polacek  <polacek@redhat.com>

> 

> 	PR c++/93862 - ICE with static_cast when converting from int[].

> 	* call.c (reference_compatible_p): No longer static.

> 	* cp-tree.h (reference_compatible_p): Declare.

> 	* typeck.c (build_static_cast_1): Use reference_compatible_p instead

> 	if reference_related_p.


(Consider this typo fixed.)

Marek
Jason Merrill Feb. 26, 2020, 4:57 a.m. | #2
On 2/24/20 3:41 PM, Marek Polacek wrote:
> This ICEs since my patch for P0388, which allowed conversions to arrays

> of unknown bound, but not the reverse, so these two static_casts are

> ill-formed.

> 

> [expr.static.cast]/3 says that "cv1 T1" and "cv2 T2" have to be

> reference-compatible and the comment in build_static_cast_1 says it too

> but then we actually use reference_related_p...  Fixed thus.

> 

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


OK.

> 

> 2020-02-24  Marek Polacek  <polacek@redhat.com>

> 

> 	PR c++/93862 - ICE with static_cast when converting from int[].

> 	* call.c (reference_compatible_p): No longer static.

> 	* cp-tree.h (reference_compatible_p): Declare.

> 	* typeck.c (build_static_cast_1): Use reference_compatible_p instead

> 	if reference_related_p.

> 

> 	* g++.dg/cpp0x/rv-cast7.C: New test.

> ---

>   gcc/cp/call.c                         | 3 +--

>   gcc/cp/cp-tree.h                      | 1 +

>   gcc/cp/typeck.c                       | 2 +-

>   gcc/testsuite/g++.dg/cpp0x/rv-cast7.C | 6 ++++++

>   4 files changed, 9 insertions(+), 3 deletions(-)

>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/rv-cast7.C

> 

> diff --git a/gcc/cp/call.c b/gcc/cp/call.c

> index 7a7f07c9630..e07ee85c06e 100644

> --- a/gcc/cp/call.c

> +++ b/gcc/cp/call.c

> @@ -204,7 +204,6 @@ static struct z_candidate *add_candidate

>   	 conversion **, tree, tree, int, struct rejection_reason *, int);

>   static tree source_type (conversion *);

>   static void add_warning (struct z_candidate *, struct z_candidate *);

> -static bool reference_compatible_p (tree, tree);

>   static conversion *direct_reference_binding (tree, conversion *);

>   static bool promoted_arithmetic_type_p (tree);

>   static conversion *conditional_conversion (tree, tree, tsubst_flags_t);

> @@ -1554,7 +1553,7 @@ reference_related_p (tree t1, tree t2)

>   

>   /* Returns nonzero if T1 is reference-compatible with T2.  */

>   

> -static bool

> +bool

>   reference_compatible_p (tree t1, tree t2)

>   {

>     /* [dcl.init.ref]

> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h

> index c1aafa1de3a..140d5d88b61 100644

> --- a/gcc/cp/cp-tree.h

> +++ b/gcc/cp/cp-tree.h

> @@ -6357,6 +6357,7 @@ extern tree make_temporary_var_for_ref_to_temp	(tree, tree);

>   extern bool type_has_extended_temps		(tree);

>   extern tree strip_top_quals			(tree);

>   extern bool reference_related_p			(tree, tree);

> +extern bool reference_compatible_p		(tree, tree);

>   extern int remaining_arguments			(tree);

>   extern tree perform_implicit_conversion		(tree, tree, tsubst_flags_t);

>   extern tree perform_implicit_conversion_flags	(tree, tree, tsubst_flags_t, int);

> diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c

> index c0c98dad980..103a1a439ec 100644

> --- a/gcc/cp/typeck.c

> +++ b/gcc/cp/typeck.c

> @@ -7383,7 +7383,7 @@ build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,

>     if (TYPE_REF_P (type)

>         && TYPE_REF_IS_RVALUE (type)

>         && (clk = real_lvalue_p (expr))

> -      && reference_related_p (TREE_TYPE (type), intype)

> +      && reference_compatible_p (TREE_TYPE (type), intype)

>         && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))

>       {

>         if (processing_template_decl)

> diff --git a/gcc/testsuite/g++.dg/cpp0x/rv-cast7.C b/gcc/testsuite/g++.dg/cpp0x/rv-cast7.C

> new file mode 100644

> index 00000000000..0eea24f9e85

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/cpp0x/rv-cast7.C

> @@ -0,0 +1,6 @@

> +// PR c++/93862 - ICE with static_cast when converting from int[].

> +// { dg-do compile { target c++11 } }

> +

> +int(&&intu_rvref)[]{1,2,3,4};

> +int(&int4_lvref)[4] = static_cast<int(&)[4]>(intu_rvref); // { dg-error "invalid .static_cast." }

> +int(&&int4_rvref)[4] = static_cast<int(&&)[4]>(intu_rvref); // { dg-error "invalid .static_cast." }

> 

> base-commit: a4dbb9b25a60143c699de55cd6226cebeb3b3c3e

>

Patch

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 7a7f07c9630..e07ee85c06e 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -204,7 +204,6 @@  static struct z_candidate *add_candidate
 	 conversion **, tree, tree, int, struct rejection_reason *, int);
 static tree source_type (conversion *);
 static void add_warning (struct z_candidate *, struct z_candidate *);
-static bool reference_compatible_p (tree, tree);
 static conversion *direct_reference_binding (tree, conversion *);
 static bool promoted_arithmetic_type_p (tree);
 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
@@ -1554,7 +1553,7 @@  reference_related_p (tree t1, tree t2)
 
 /* Returns nonzero if T1 is reference-compatible with T2.  */
 
-static bool
+bool
 reference_compatible_p (tree t1, tree t2)
 {
   /* [dcl.init.ref]
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index c1aafa1de3a..140d5d88b61 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6357,6 +6357,7 @@  extern tree make_temporary_var_for_ref_to_temp	(tree, tree);
 extern bool type_has_extended_temps		(tree);
 extern tree strip_top_quals			(tree);
 extern bool reference_related_p			(tree, tree);
+extern bool reference_compatible_p		(tree, tree);
 extern int remaining_arguments			(tree);
 extern tree perform_implicit_conversion		(tree, tree, tsubst_flags_t);
 extern tree perform_implicit_conversion_flags	(tree, tree, tsubst_flags_t, int);
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index c0c98dad980..103a1a439ec 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -7383,7 +7383,7 @@  build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
   if (TYPE_REF_P (type)
       && TYPE_REF_IS_RVALUE (type)
       && (clk = real_lvalue_p (expr))
-      && reference_related_p (TREE_TYPE (type), intype)
+      && reference_compatible_p (TREE_TYPE (type), intype)
       && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
     {
       if (processing_template_decl)
diff --git a/gcc/testsuite/g++.dg/cpp0x/rv-cast7.C b/gcc/testsuite/g++.dg/cpp0x/rv-cast7.C
new file mode 100644
index 00000000000..0eea24f9e85
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/rv-cast7.C
@@ -0,0 +1,6 @@ 
+// PR c++/93862 - ICE with static_cast when converting from int[].
+// { dg-do compile { target c++11 } }
+
+int(&&intu_rvref)[]{1,2,3,4};
+int(&int4_lvref)[4] = static_cast<int(&)[4]>(intu_rvref); // { dg-error "invalid .static_cast." }
+int(&&int4_rvref)[4] = static_cast<int(&&)[4]>(intu_rvref); // { dg-error "invalid .static_cast." }