C++ PATCH for c++/91844 - Implement CWG 2352, Similar types and reference binding.

Message ID 20190922192346.GX14737@redhat.com
State New
Headers show
Series
  • C++ PATCH for c++/91844 - Implement CWG 2352, Similar types and reference binding.
Related show

Commit Message

Marek Polacek Sept. 22, 2019, 7:23 p.m.
I came across this DR while looking into P0388R4 and realized I need to implement
it before tackling P0388R4.  DR 2352 changes the definitions of reference-related
(so that it uses "similar type" instead of "same type") and of reference-compatible
(use a standard conversion sequence), and then P0388R4 further adjusts the
definition of what a "similar type" is.

We have same_type_p but unfortunately we didn't have similar_type_p so I added
that; I made use of comp_cv_qual_signature there.

That means that reference-related is now more broad, which means that we
will be binding more things directly, which has certain consequences, e.g.:

ref-bind1.C is an extended testcase from the DR.  We are no longer creating
a temporary, so -Wreturn-local-addr shouldn't trigger.

ref-bind2.C shows that some code is now ill-formed: [dcl.init.ref]p5.4.4
prohibits binding an rvalue reference to an lvalue if T1 is reference-related
to T2, which it now is.

ref-bind3.C shows that some code that has been invalid will become valid:
extending reference-related means avoiding creating a qualification conversion
when binding a parameter here, which changes the ranking.  In this case,
it evaded an ambiguity.  The comment explains it in more detail.

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

2019-09-22  Marek Polacek  <polacek@redhat.com>

	PR c++/91844 - Implement CWG 2352, Similar types and reference binding.
	* call.c (reference_related_p): Use similar_type_p instead of
	same_type_p.
	(reference_compatible_p): Update implementation to match CWG 2352.
	* cp-tree.h (similar_type_p): Declare.
	* typeck.c (similar_type_p): New.

	* g++.dg/cpp0x/pr33930.C: Add dg-error.
	* g++.dg/cpp0x/ref-bind1.C: New test.
	* g++.dg/cpp0x/ref-bind2.C: New test.
	* g++.dg/cpp0x/ref-bind3.C: New test.
	* g++.old-deja/g++.pt/spec35.C: Remove dg-error.

Comments

Jason Merrill Sept. 23, 2019, 2:17 p.m. | #1
On 9/22/19 3:23 PM, Marek Polacek wrote:
> +  /* Informally, two types are similar if, ignoring top-level cv-qualification:

> +     * they are the same type; or

> +     * they are both pointers, and the pointed-to types are similar; or

> +     * they are both pointers to member of the same class, and the types of

> +       the pointed-to members are similar; or

> +     * they are both arrays of the same size or both arrays of unknown bound,

> +       and the array element types are similar.  */


Yes.

> +  if ((TREE_CODE (type1) == POINTER_TYPE && TREE_CODE (type2) == POINTER_TYPE)

> +      || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))

> +      || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))

> +    {

> +      int result = comp_cv_qual_signature (TREE_TYPE (type1),

> +					   TREE_TYPE (type2));


Using comp_cv_qual_signature means that we only return true if one 
cv-qualification signature is a subset of the other, but the definition 
of 'similar' is such that we should completely ignore cv-quals.  int 
const ** and int *const* are similar even though neither can convert to 
the other.

Jason
Marek Polacek Sept. 23, 2019, 4:06 p.m. | #2
On Mon, Sep 23, 2019 at 10:17:00AM -0400, Jason Merrill wrote:
> On 9/22/19 3:23 PM, Marek Polacek wrote:

> > +  /* Informally, two types are similar if, ignoring top-level cv-qualification:

> > +     * they are the same type; or

> > +     * they are both pointers, and the pointed-to types are similar; or

> > +     * they are both pointers to member of the same class, and the types of

> > +       the pointed-to members are similar; or

> > +     * they are both arrays of the same size or both arrays of unknown bound,

> > +       and the array element types are similar.  */

> 

> Yes.

> 

> > +  if ((TREE_CODE (type1) == POINTER_TYPE && TREE_CODE (type2) == POINTER_TYPE)

> > +      || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))

> > +      || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))

> > +    {

> > +      int result = comp_cv_qual_signature (TREE_TYPE (type1),

> > +					   TREE_TYPE (type2));

> 

> Using comp_cv_qual_signature means that we only return true if one

> cv-qualification signature is a subset of the other, but the definition of

> 'similar' is such that we should completely ignore cv-quals.  int const **

> and int *const* are similar even though neither can convert to the other.


Ah, true, what I actually want is comp_ptr_ttypes_const.  I didn't notice
because as you say, the conversion would fail with "discarding qualifiers".
But similar_type_p shouldn't give the wrong answer.

The handling of arrays will come in P0388R4.

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

2019-09-23  Marek Polacek  <polacek@redhat.com>

	PR c++/91844 - Implement CWG 2352, Similar types and reference binding.
	* call.c (reference_related_p): Use similar_type_p instead of
	same_type_p.
	(reference_compatible_p): Update implementation to match CWG 2352.
	* cp-tree.h (similar_type_p): Declare.
	* typeck.c (similar_type_p): New.

	* g++.dg/cpp0x/pr33930.C: Add dg-error.
	* g++.dg/cpp0x/ref-bind1.C: New test.
	* g++.dg/cpp0x/ref-bind2.C: New test.
	* g++.dg/cpp0x/ref-bind3.C: New test.
	* g++.old-deja/g++.pt/spec35.C: Remove dg-error.

diff --git gcc/cp/call.c gcc/cp/call.c
index 2dad699622e..28b3f332b5d 100644
--- gcc/cp/call.c
+++ gcc/cp/call.c
@@ -1530,9 +1530,8 @@ reference_related_p (tree t1, tree t2)
   /* [dcl.init.ref]
 
      Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
-     to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
-     of T2.  */
-  return (same_type_p (t1, t2)
+     to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2.  */
+  return (similar_type_p (t1, t2)
 	  || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
 	      && DERIVED_FROM_P (t1, t2)));
 }
@@ -1545,14 +1544,15 @@ reference_compatible_p (tree t1, tree t2)
   /* [dcl.init.ref]
 
      "cv1 T1" is reference compatible with "cv2 T2" if
-       * T1 is reference-related to T2 or
-       * T2 is "noexcept function" and T1 is "function", where the
-         function types are otherwise the same,
-     and cv1 is the same cv-qualification as, or greater cv-qualification
-     than, cv2.  */
-  return ((reference_related_p (t1, t2)
-	   || fnptr_conv_p (t1, t2))
-	  && at_least_as_qualified_p (t1, t2));
+     a prvalue of type "pointer to cv2 T2" can be converted to the type
+     "pointer to cv1 T1" via a standard conversion sequence.  */
+  tree ptype1 = build_pointer_type (t1);
+  tree ptype2 = build_pointer_type (t2);
+  conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
+					  /*c_cast_p=*/false, 0, tf_none);
+  if (!conv || conv->bad_p)
+    return false;
+  return true;
 }
 
 /* A reference of the indicated TYPE is being bound directly to the
diff --git gcc/cp/cp-tree.h gcc/cp/cp-tree.h
index 6d217fc27a2..9c0f3949c68 100644
--- gcc/cp/cp-tree.h
+++ gcc/cp/cp-tree.h
@@ -7361,6 +7361,7 @@ enum { ce_derived, ce_type, ce_normal, ce_exact };
 extern bool comp_except_specs			(const_tree, const_tree, int);
 extern bool comptypes				(tree, tree, int);
 extern bool same_type_ignoring_top_level_qualifiers_p (tree, tree);
+extern bool similar_type_p			(tree, tree);
 extern bool compparms				(const_tree, const_tree);
 extern int comp_cv_qualification		(const_tree, const_tree);
 extern int comp_cv_qualification		(int, int);
diff --git gcc/cp/typeck.c gcc/cp/typeck.c
index d85e5474df2..f427c4f4d3e 100644
--- gcc/cp/typeck.c
+++ gcc/cp/typeck.c
@@ -1530,6 +1530,33 @@ same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
   return same_type_p (type1, type2);
 }
 
+/* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual].  */
+
+bool
+similar_type_p (tree type1, tree type2)
+{
+  if (type1 == error_mark_node || type2 == error_mark_node)
+    return false;
+
+  /* Informally, two types are similar if, ignoring top-level cv-qualification:
+     * they are the same type; or
+     * they are both pointers, and the pointed-to types are similar; or
+     * they are both pointers to member of the same class, and the types of
+       the pointed-to members are similar; or
+     * they are both arrays of the same size or both arrays of unknown bound,
+       and the array element types are similar.  */
+
+  if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
+    return true;
+
+  /* FIXME This ought to handle ARRAY_TYPEs too.  */
+  if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
+      || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
+    return comp_ptr_ttypes_const (type1, type2);
+
+  return false;
+}
+
 /* Returns 1 if TYPE1 is at least as qualified as TYPE2.  */
 
 bool
diff --git gcc/testsuite/g++.dg/cpp0x/pr33930.C gcc/testsuite/g++.dg/cpp0x/pr33930.C
index 8d9312c019b..ba5b4b943c8 100644
--- gcc/testsuite/g++.dg/cpp0x/pr33930.C
+++ gcc/testsuite/g++.dg/cpp0x/pr33930.C
@@ -6,5 +6,5 @@ int& foo( type&& ggg );
 
 void bar( int* someptr )
 {
-  int& x = foo( someptr );
+  int& x = foo( someptr ); // { dg-error "cannot bind non-const lvalue reference" }
 }
diff --git gcc/testsuite/g++.dg/cpp0x/ref-bind1.C gcc/testsuite/g++.dg/cpp0x/ref-bind1.C
new file mode 100644
index 00000000000..af6140a1f44
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/ref-bind1.C
@@ -0,0 +1,44 @@
+// PR c++/91844 - Implement CWG 2352, Similar types and reference binding.
+// { dg-do compile { target c++11 } }
+
+// These should bind directly to ptr, so no -Wreturn-local-addr warnings.
+int *ptr;
+
+const int *const &
+fn1 ()
+{
+  return ptr;
+}
+
+int **const ptr2 = nullptr;
+const int *const *const &
+fn2 ()
+{
+  return ptr2;
+}
+
+int (*ptr3)[10];
+using T = const int (*const)[10];
+
+T&
+fn3 ()
+{
+  return ptr3;
+}
+
+int (**ptr4)[5] = nullptr;
+using T2 = const int (*const *const)[5];
+
+T2&
+fn4 ()
+{
+  return ptr4;
+}
+
+const int **ptr5 = nullptr;
+
+const int *const *const &
+fn5 ()
+{
+  return ptr5;
+}
diff --git gcc/testsuite/g++.dg/cpp0x/ref-bind2.C gcc/testsuite/g++.dg/cpp0x/ref-bind2.C
new file mode 100644
index 00000000000..967c59e06fd
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/ref-bind2.C
@@ -0,0 +1,15 @@
+// PR c++/91844 - Implement CWG 2352, Similar types and reference binding.
+// { dg-do compile { target c++11 } }
+
+// "const int *" and "int *" are reference-related, and 5.4.4.
+// says that in that case, if the reference is an rvalue reference,
+// the initializer expression shall not be an lvalue.
+
+int &f (const int *&&);
+
+void
+fn (int *p)
+{
+  const int *&&r = p; // { dg-error "cannot bind rvalue reference" }
+  f (p); // { dg-error "cannot bind rvalue reference" }
+}
diff --git gcc/testsuite/g++.dg/cpp0x/ref-bind3.C gcc/testsuite/g++.dg/cpp0x/ref-bind3.C
new file mode 100644
index 00000000000..16e1bfe6ccc
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/ref-bind3.C
@@ -0,0 +1,22 @@
+// PR c++/91844 - Implement CWG 2352, Similar types and reference binding.
+// { dg-do compile { target c++11 } }
+
+template<typename T> int f (const T *const &); // (1)
+template<typename T> int f (T *const &); // (2)
+template<typename T> int f (T *); // (3)
+
+/* Before CWG 2352, (2) was a better match than (1), but (2) and (3) were
+   equally good, so there was an ambiguity.  (2) was better than (1) because
+   (1) required a qualification conversion whereas (2) didn't.  But with this
+   CWG, (1) no longer requires a qualification conversion, because the types
+   "const int* const" and "int *" are now considered reference-related and we
+   bind directly, and (1) is more specialized than (2).  And (1) is also a
+   better match than (3).  */
+
+void
+g (int *p, const int *q, const int *const r)
+{
+  f (p); // calls (1)
+  f (q); // calls (1)
+  f (r); // calls (1)
+}
diff --git gcc/testsuite/g++.old-deja/g++.pt/spec35.C gcc/testsuite/g++.old-deja/g++.pt/spec35.C
index 581bb8ed537..93e953df7e7 100644
--- gcc/testsuite/g++.old-deja/g++.pt/spec35.C
+++ gcc/testsuite/g++.old-deja/g++.pt/spec35.C
@@ -14,9 +14,9 @@ template <typename T> int Foo (T &);  // { dg-message "note" } candidate
 template <typename T> int Qux (T);    // { dg-message "note" } 
 template <typename T> int Qux (T const &);  // { dg-message "note" } candidate
 
-template <typename T> int Bar (T const *const &); // { dg-message "note" } 
-template <typename T> int Bar (T *const &);       // { dg-message "note" } candidate
-template <typename T> int Bar (T *);              // { dg-message "note" } candidate
+template <typename T> int Bar (T const *const &);
+template <typename T> int Bar (T *const &);
+template <typename T> int Bar (T *);
 
 template <typename T> int Baz (T *const &);       // { dg-message "note" } 
 template <typename T> int Baz (T *);              // { dg-message "note" } candidate
@@ -24,7 +24,7 @@ template <typename T> int Baz (T *);              // { dg-message "note" } candi
 int Baz (int const *ptr, int *ptr2)
 {
   Baz (ptr2);   // { dg-error "ambiguous" } 
-  Bar (ptr2);   // { dg-error "ambiguous" } 
+  Bar (ptr2);
   Foo (ptr2);   // { dg-error "ambiguous" } 
   Qux (ptr2);   // { dg-error "ambiguous" } 
   return 0;
Jason Merrill Sept. 23, 2019, 5:27 p.m. | #3
On 9/23/19 12:06 PM, Marek Polacek wrote:
> On Mon, Sep 23, 2019 at 10:17:00AM -0400, Jason Merrill wrote:

>> On 9/22/19 3:23 PM, Marek Polacek wrote:

>>> +  /* Informally, two types are similar if, ignoring top-level cv-qualification:

>>> +     * they are the same type; or

>>> +     * they are both pointers, and the pointed-to types are similar; or

>>> +     * they are both pointers to member of the same class, and the types of

>>> +       the pointed-to members are similar; or

>>> +     * they are both arrays of the same size or both arrays of unknown bound,

>>> +       and the array element types are similar.  */

>>

>> Yes.

>>

>>> +  if ((TREE_CODE (type1) == POINTER_TYPE && TREE_CODE (type2) == POINTER_TYPE)

>>> +      || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))

>>> +      || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))

>>> +    {

>>> +      int result = comp_cv_qual_signature (TREE_TYPE (type1),

>>> +					   TREE_TYPE (type2));

>>

>> Using comp_cv_qual_signature means that we only return true if one

>> cv-qualification signature is a subset of the other, but the definition of

>> 'similar' is such that we should completely ignore cv-quals.  int const **

>> and int *const* are similar even though neither can convert to the other.

> 

> Ah, true, what I actually want is comp_ptr_ttypes_const.  I didn't notice

> because as you say, the conversion would fail with "discarding qualifiers".

> But similar_type_p shouldn't give the wrong answer.

> 

> The handling of arrays will come in P0388R4.


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


OK.

Jason

Patch

diff --git gcc/cp/call.c gcc/cp/call.c
index 2dad699622e..28b3f332b5d 100644
--- gcc/cp/call.c
+++ gcc/cp/call.c
@@ -1530,9 +1530,8 @@  reference_related_p (tree t1, tree t2)
   /* [dcl.init.ref]
 
      Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
-     to "cv2 T2" if T1 is the same type as T2, or T1 is a base class
-     of T2.  */
-  return (same_type_p (t1, t2)
+     to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2.  */
+  return (similar_type_p (t1, t2)
 	  || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
 	      && DERIVED_FROM_P (t1, t2)));
 }
@@ -1545,14 +1544,15 @@  reference_compatible_p (tree t1, tree t2)
   /* [dcl.init.ref]
 
      "cv1 T1" is reference compatible with "cv2 T2" if
-       * T1 is reference-related to T2 or
-       * T2 is "noexcept function" and T1 is "function", where the
-         function types are otherwise the same,
-     and cv1 is the same cv-qualification as, or greater cv-qualification
-     than, cv2.  */
-  return ((reference_related_p (t1, t2)
-	   || fnptr_conv_p (t1, t2))
-	  && at_least_as_qualified_p (t1, t2));
+     a prvalue of type "pointer to cv2 T2" can be converted to the type
+     "pointer to cv1 T1" via a standard conversion sequence.  */
+  tree ptype1 = build_pointer_type (t1);
+  tree ptype2 = build_pointer_type (t2);
+  conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
+					  /*c_cast_p=*/false, 0, tf_none);
+  if (!conv || conv->bad_p)
+    return false;
+  return true;
 }
 
 /* A reference of the indicated TYPE is being bound directly to the
diff --git gcc/cp/cp-tree.h gcc/cp/cp-tree.h
index 6d217fc27a2..9c0f3949c68 100644
--- gcc/cp/cp-tree.h
+++ gcc/cp/cp-tree.h
@@ -7361,6 +7361,7 @@  enum { ce_derived, ce_type, ce_normal, ce_exact };
 extern bool comp_except_specs			(const_tree, const_tree, int);
 extern bool comptypes				(tree, tree, int);
 extern bool same_type_ignoring_top_level_qualifiers_p (tree, tree);
+extern bool similar_type_p			(tree, tree);
 extern bool compparms				(const_tree, const_tree);
 extern int comp_cv_qualification		(const_tree, const_tree);
 extern int comp_cv_qualification		(int, int);
diff --git gcc/cp/typeck.c gcc/cp/typeck.c
index d85e5474df2..2c99efb0b03 100644
--- gcc/cp/typeck.c
+++ gcc/cp/typeck.c
@@ -1530,6 +1530,38 @@  same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
   return same_type_p (type1, type2);
 }
 
+/* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual].  */
+
+bool
+similar_type_p (tree type1, tree type2)
+{
+  if (type1 == error_mark_node || type2 == error_mark_node)
+    return false;
+
+  /* Informally, two types are similar if, ignoring top-level cv-qualification:
+     * they are the same type; or
+     * they are both pointers, and the pointed-to types are similar; or
+     * they are both pointers to member of the same class, and the types of
+       the pointed-to members are similar; or
+     * they are both arrays of the same size or both arrays of unknown bound,
+       and the array element types are similar.  */
+
+  if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
+    return true;
+
+  if ((TREE_CODE (type1) == POINTER_TYPE && TREE_CODE (type2) == POINTER_TYPE)
+      || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
+      || (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
+    {
+      int result = comp_cv_qual_signature (TREE_TYPE (type1),
+					   TREE_TYPE (type2));
+      if (result != 0)
+	return true;
+    }
+
+  return false;
+}
+
 /* Returns 1 if TYPE1 is at least as qualified as TYPE2.  */
 
 bool
diff --git gcc/testsuite/g++.dg/cpp0x/pr33930.C gcc/testsuite/g++.dg/cpp0x/pr33930.C
index 8d9312c019b..ba5b4b943c8 100644
--- gcc/testsuite/g++.dg/cpp0x/pr33930.C
+++ gcc/testsuite/g++.dg/cpp0x/pr33930.C
@@ -6,5 +6,5 @@  int& foo( type&& ggg );
 
 void bar( int* someptr )
 {
-  int& x = foo( someptr );
+  int& x = foo( someptr ); // { dg-error "cannot bind non-const lvalue reference" }
 }
diff --git gcc/testsuite/g++.dg/cpp0x/ref-bind1.C gcc/testsuite/g++.dg/cpp0x/ref-bind1.C
new file mode 100644
index 00000000000..841ec1dc7a0
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/ref-bind1.C
@@ -0,0 +1,36 @@ 
+// PR c++/91844 - Implement CWG 2352, Similar types and reference binding.
+// { dg-do compile { target c++11 } }
+
+// These should bind directly to ptr, so no -Wreturn-local-addr warnings.
+int *ptr;
+
+const int *const &
+fn1 ()
+{
+  return ptr;
+}
+
+int **const ptr2 = nullptr;
+const int *const *const &
+fn2 ()
+{
+  return ptr2;
+}
+
+int (*ptr3)[10];
+using T = const int (*const)[10];
+
+T&
+fn3 ()
+{
+  return ptr3;
+}
+
+int (**ptr4)[5] = nullptr;
+using T2 = const int (*const *const)[5];
+
+T2&
+fn4 ()
+{
+  return ptr4;
+}
diff --git gcc/testsuite/g++.dg/cpp0x/ref-bind2.C gcc/testsuite/g++.dg/cpp0x/ref-bind2.C
new file mode 100644
index 00000000000..967c59e06fd
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/ref-bind2.C
@@ -0,0 +1,15 @@ 
+// PR c++/91844 - Implement CWG 2352, Similar types and reference binding.
+// { dg-do compile { target c++11 } }
+
+// "const int *" and "int *" are reference-related, and 5.4.4.
+// says that in that case, if the reference is an rvalue reference,
+// the initializer expression shall not be an lvalue.
+
+int &f (const int *&&);
+
+void
+fn (int *p)
+{
+  const int *&&r = p; // { dg-error "cannot bind rvalue reference" }
+  f (p); // { dg-error "cannot bind rvalue reference" }
+}
diff --git gcc/testsuite/g++.dg/cpp0x/ref-bind3.C gcc/testsuite/g++.dg/cpp0x/ref-bind3.C
new file mode 100644
index 00000000000..16e1bfe6ccc
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/ref-bind3.C
@@ -0,0 +1,22 @@ 
+// PR c++/91844 - Implement CWG 2352, Similar types and reference binding.
+// { dg-do compile { target c++11 } }
+
+template<typename T> int f (const T *const &); // (1)
+template<typename T> int f (T *const &); // (2)
+template<typename T> int f (T *); // (3)
+
+/* Before CWG 2352, (2) was a better match than (1), but (2) and (3) were
+   equally good, so there was an ambiguity.  (2) was better than (1) because
+   (1) required a qualification conversion whereas (2) didn't.  But with this
+   CWG, (1) no longer requires a qualification conversion, because the types
+   "const int* const" and "int *" are now considered reference-related and we
+   bind directly, and (1) is more specialized than (2).  And (1) is also a
+   better match than (3).  */
+
+void
+g (int *p, const int *q, const int *const r)
+{
+  f (p); // calls (1)
+  f (q); // calls (1)
+  f (r); // calls (1)
+}
diff --git gcc/testsuite/g++.old-deja/g++.pt/spec35.C gcc/testsuite/g++.old-deja/g++.pt/spec35.C
index 581bb8ed537..93e953df7e7 100644
--- gcc/testsuite/g++.old-deja/g++.pt/spec35.C
+++ gcc/testsuite/g++.old-deja/g++.pt/spec35.C
@@ -14,9 +14,9 @@  template <typename T> int Foo (T &);  // { dg-message "note" } candidate
 template <typename T> int Qux (T);    // { dg-message "note" } 
 template <typename T> int Qux (T const &);  // { dg-message "note" } candidate
 
-template <typename T> int Bar (T const *const &); // { dg-message "note" } 
-template <typename T> int Bar (T *const &);       // { dg-message "note" } candidate
-template <typename T> int Bar (T *);              // { dg-message "note" } candidate
+template <typename T> int Bar (T const *const &);
+template <typename T> int Bar (T *const &);
+template <typename T> int Bar (T *);
 
 template <typename T> int Baz (T *const &);       // { dg-message "note" } 
 template <typename T> int Baz (T *);              // { dg-message "note" } candidate
@@ -24,7 +24,7 @@  template <typename T> int Baz (T *);              // { dg-message "note" } candi
 int Baz (int const *ptr, int *ptr2)
 {
   Baz (ptr2);   // { dg-error "ambiguous" } 
-  Bar (ptr2);   // { dg-error "ambiguous" } 
+  Bar (ptr2);
   Foo (ptr2);   // { dg-error "ambiguous" } 
   Qux (ptr2);   // { dg-error "ambiguous" } 
   return 0;