Patchwork fix c++/33558 - references cannot be mutable

login
register
mail settings
Submitter Jonathan Wakely
Date Dec. 18, 2010, 6:57 p.m.
Message ID <AANLkTimsrNGLzBoW8AaMWSj4ZrnnNAfK+zSizT7k9KLc@mail.gmail.com>
Download mbox | patch
Permalink /patch/76093/
State New
Headers show

Comments

Jonathan Wakely - Dec. 18, 2010, 6:57 p.m.
This is a slightly modified version of the patch attached to PR33558
which was posted to gcc-patches but apparently never reviewed.  I
don't know if the author of the original patch, Giovanni, has a
copyright assignment but I think the change is obvious and I hope is
sufficiently small to not need an assignment. I was in the process of
making the same change when I found his patch on the PR and copied the
wording of his diagnostic and testcase.

Sun CC and g++ both incorrectly accept mutable references, so it might
not be uncommon in the wild (I found a use in some production code
yesterday.)  This version of the patch allows mutable on reference
members when -fpermissive is used, giving old code a transition path.
I'll add a note to changes.html if this is approved.

tested x86_64-linux, OK for trunk?


cp/ChangeLog entry:

2010-12-18  Giovanni Funchal  <gafunchal@gmail.com>
            Jonathan Wakely  <jwakely.gcc@gmail.com>

        PR c++/33558
        * decl.c (grokdeclarator): Reject mutable reference members.

testsuite/ChangeLog entry:

2010-12-18  Giovanni Funchal  <gafunchal@gmail.com>
            Jonathan Wakely  <jwakely.gcc@gmail.com>

        PR c++/33558
        * testsuite/g++.dg/other/pr33558.C: New.
        * testsuite/g++.dg/other/pr33558-2.C: New.

Patch

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 167817)
+++ cp/decl.c	(working copy)
@@ -9210,6 +9210,12 @@  grokdeclarator (const cp_declarator *dec
 	  error ("const %qs cannot be declared %<mutable%>", name);
 	  storage_class = sc_none;
 	}
+      else if (TREE_CODE (type) == REFERENCE_TYPE)
+	{
+	  permerror (input_location, "reference %qs cannot be declared "
+	             "%<mutable%>", name);
+	  storage_class = sc_none;
+	}
     }
 
   /* If this is declaring a typedef name, return a TYPE_DECL.  */
Index: testsuite/g++.dg/other/pr33558.C
===================================================================
--- testsuite/g++.dg/other/pr33558.C	(revision 0)
+++ testsuite/g++.dg/other/pr33558.C	(revision 0)
@@ -0,0 +1,5 @@ 
+/* { dg-do compile } */
+
+class X {
+  mutable int &q; /* { dg-error "cannot be declared 'mutable'" } */
+};
Index: testsuite/g++.dg/other/pr33558-2.C
===================================================================
--- testsuite/g++.dg/other/pr33558-2.C	(revision 0)
+++ testsuite/g++.dg/other/pr33558-2.C	(revision 0)
@@ -0,0 +1,6 @@ 
+/* { dg-do compile } */
+/* { dg-options "-fpermissive" } */
+
+class X {
+  mutable int &q; /* { dg-warning "cannot be declared 'mutable'" } */
+};