From patchwork Sat Dec 18 18:57:46 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 76093 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id DB15AB6F10 for ; Sun, 19 Dec 2010 05:57:56 +1100 (EST) Received: (qmail 13508 invoked by alias); 18 Dec 2010 18:57:53 -0000 Received: (qmail 13500 invoked by uid 22791); 18 Dec 2010 18:57:53 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-wy0-f175.google.com (HELO mail-wy0-f175.google.com) (74.125.82.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 18 Dec 2010 18:57:48 +0000 Received: by wyb40 with SMTP id 40so1691833wyb.20 for ; Sat, 18 Dec 2010 10:57:46 -0800 (PST) MIME-Version: 1.0 Received: by 10.216.169.209 with SMTP id n59mr1811219wel.51.1292698666210; Sat, 18 Dec 2010 10:57:46 -0800 (PST) Received: by 10.216.160.131 with HTTP; Sat, 18 Dec 2010 10:57:46 -0800 (PST) Date: Sat, 18 Dec 2010 18:57:46 +0000 Message-ID: Subject: [patch] fix c++/33558 - references cannot be mutable From: Jonathan Wakely To: gcc-patches Cc: Giovanni Funchal Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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 Jonathan Wakely PR c++/33558 * decl.c (grokdeclarator): Reject mutable reference members. testsuite/ChangeLog entry: 2010-12-18 Giovanni Funchal Jonathan Wakely PR c++/33558 * testsuite/g++.dg/other/pr33558.C: New. * testsuite/g++.dg/other/pr33558-2.C: New. 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 %", name); storage_class = sc_none; } + else if (TREE_CODE (type) == REFERENCE_TYPE) + { + permerror (input_location, "reference %qs cannot be declared " + "%", 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'" } */ +};