public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Fix unnecessary error when top-level cv-qualifiers is dropped [PR101783]
@ 2021-08-10  5:36 nick huang
  2021-08-28 11:54 ` [PATCH] c++: Suppress error when cv-qualified reference is introduced by typedef [PR101783] nick huang
  0 siblings, 1 reply; 10+ messages in thread
From: nick huang @ 2021-08-10  5:36 UTC (permalink / raw)
  To: gcc-patches

The root cause of this bug is that it considers reference with cv-qualifiers
as an error by generating value for variable "bad_quals". However, this is
not correct for case of typedef. Here I quote spec:
"Cv-qualified references are ill-formed except when the cv-qualifiers
are introduced through the use of a typedef-name ([dcl.typedef],
[temp.param]) or decltype-specifier ([dcl.type.decltype]),
in which case the cv-qualifiers are ignored."

2021-08-09  Qingzhe Huang  <nickhuang99@hotmail.com>

PR c++/101387

gcc/cp/ChangeLog:
        PR c++/101387
        * tree.c (cp_build_qualified_type_real): Excluding typedef from error

gcc/testsuite/ChangeLog:
        PR c++/101387
        * g++.dg/parse/pr101783.C: New test.

diff --git gcc/cp/tree.c gcc/cp/tree.c
index 10b818d1370..a4ae3eee6d0 100644
--- gcc/cp/tree.c
+++ gcc/cp/tree.c
@@ -1361,11 +1361,18 @@ cp_build_qualified_type_real (tree type,
   /* A reference or method type shall not be cv-qualified.
      [dcl.ref], [dcl.fct].  This used to be an error, but as of DR 295
      (in CD1) we always ignore extra cv-quals on functions.  */
+  /* Cv-qualified references are ill-formed except when the cv-qualifiers
+     are introduced through the use of a typedef-name ([dcl.typedef],
+     [temp.param]) or decltype-specifier ([dcl.type.decltype]),
+     in which case the cv-qualifiers are ignored.  */
   if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
       && (TYPE_REF_P (type)
          || FUNC_OR_METHOD_TYPE_P (type)))
     {
-      if (TYPE_REF_P (type))
+      if (TYPE_REF_P (type)
+       //A reference from typedef is not an error, but ignored.
+       && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type)))
        bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
       type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
     }

diff --git gcc/testsuite/g++.dg/parse/pr101783.C gcc/testsuite/g++.dg/parse/pr101783.C
new file mode 100644
index 00000000000..8f8f8b1faeb
--- /dev/null
+++ gcc/testsuite/g++.dg/parse/pr101783.C
@@ -0,0 +1,10 @@
+template<class T> struct A{
+        typedef T& Type;
+};
+template<class T>
+void f(const typename A<T>::Type){}
+template<>
+void f<int>(const typename A<int>::Type){}
+struct B{};
+template<>
+void f<B>(const A<B>::Type){}

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-10-05 19:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CAP6LXBsdU3xULuu8B3cEDGbcpHUXxF4roCC-zboWmtpGoPdVvw@mail.gmail.com>
2021-09-27  1:31 ` [PATCH] c++: Suppress error when cv-qualified reference is introduced by typedef [PR101783] nick huang
2021-09-28 19:51   ` Jason Merrill
2021-09-30 18:24     ` nick huang
2021-10-01 13:29       ` Jason Merrill
2021-10-01 15:10         ` Nick Huang
2021-10-01 15:45           ` Jason Merrill
2021-10-01 19:52             ` Nick Huang
2021-10-05 19:40               ` Jason Merrill
2021-08-10  5:36 [PATCH] c++: Fix unnecessary error when top-level cv-qualifiers is dropped [PR101783] nick huang
2021-08-28 11:54 ` [PATCH] c++: Suppress error when cv-qualified reference is introduced by typedef [PR101783] nick huang
2021-09-24 20:33   ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).