From patchwork Wed Jan 4 22:07:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 711135 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3tv4g819pYz9sQw for ; Thu, 5 Jan 2017 09:07:31 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="hYglKSz7"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=hwLxenglbJGslhoeQ PIOijJHt87JrWDbEHZSiJZNk+19AajJjh53Ik2WxypEphqEmMhXHUZ4lJuYXTZjz RqP2btfNpEJm2K/3bCGsOR383YD9vKmHIHSHl4NpGnXlCEJQT/xf+4X6UCb5j4P4 tnUfvZfG2wmJGKcoVYUP5RFWw0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:reply-to:references:mime-version :content-type:in-reply-to; s=default; bh=NQ7PqLZUMXxbCyelIFjLrBx NtLw=; b=hYglKSz7+c1Fmk6LEEFcPT1hFWzBhf6isH7VPxLPkFeP6MH6c2RWozD KYa8n//EXy8ns6ElUWpN+FQNd6sdnB8GEhJfmrE2+3vz35TN5ajW9ArUz+uFiMNe ckDBnzQn6D9SVIgcwBAyW4wINgSEvXvGGxVlwElr9jQoC7yoPe2M= Received: (qmail 10679 invoked by alias); 4 Jan 2017 22:07:23 -0000 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 Received: (qmail 10654 invoked by uid 89); 4 Jan 2017 22:07:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.1 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=78890, cxx11, H*MI:sk:Ot1m3GQ, H*MI:39_q X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 04 Jan 2017 22:07:20 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 49C1BC0567B1; Wed, 4 Jan 2017 22:07:20 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-116-54.ams2.redhat.com [10.36.116.54]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v04M7ITp023364 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 4 Jan 2017 17:07:19 -0500 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id v04M7F4J020405; Wed, 4 Jan 2017 23:07:15 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id v04M7EXl020300; Wed, 4 Jan 2017 23:07:14 +0100 Date: Wed, 4 Jan 2017 23:07:14 +0100 From: Jakub Jelinek To: Jason Merrill Cc: Paolo Carlini , gcc-patches List Subject: Re: [C++ PATCH] Reject invalid reference type non-static data members in unions (PR c++/78890) Message-ID: <20170104220713.GT21933@tucnak> Reply-To: Jakub Jelinek References: <20170102193252.GI21933@tucnak> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.7.1 (2016-10-04) X-IsSubscribed: yes On Wed, Jan 04, 2017 at 04:24:55PM -0500, Jason Merrill wrote: > On Mon, Jan 2, 2017 at 2:32 PM, Jakub Jelinek wrote: > > if (TREE_CODE (type) == REFERENCE_TYPE) > > { > > - error ("in C++98 %q+D may not have reference type %qT " > > - "because it is a member of a union", x, type); > > - continue; > > + if (cxx_dialect < cxx11) > > + { > > + error ("in C++98 %q+D may not have reference type %qT " > > + "because it is a member of a union", x, type); > > + continue; > > + } > > I don't think we need the old error anymore; in C++98 mode we will > have already complained about a static data member reference, so we > can drop the above and just keep the below. > > > + else if (TREE_CODE (x) == FIELD_DECL) > > + { > > + error ("non-static data member %q+D in a union may not " > > + "have reference type %qT", x, type); > > + continue; > > + } Like this? Ok for trunk if bootstrap/regtest passes (just started them; make check-c++-all didn't show regressions)? 2017-01-04 Jakub Jelinek PR c++/78890 * class.c (check_field_decls): Diagnose REFERENCE_TYPE fields in unions even for C++11 and later. * g++.dg/init/ref14.C: Expect error even in C++11 and later. * g++.dg/init/union1.C: Likewise. * g++.dg/cpp0x/union6.C: Expect errors. * g++.dg/cpp0x/union8.C: New test. * g++.dg/cpp0x/pr78890-1.C: New test. * g++.dg/cpp0x/pr78890-2.C: New test. Jakub --- gcc/cp/class.c.jj 2017-01-03 08:12:27.154511815 +0100 +++ gcc/cp/class.c 2017-01-04 22:41:32.669279309 +0100 @@ -3759,25 +3759,27 @@ check_field_decls (tree t, tree *access_ /* When this goes into scope, it will be a non-local reference. */ DECL_NONLOCAL (x) = 1; - if (TREE_CODE (t) == UNION_TYPE - && cxx_dialect < cxx11) + if (TREE_CODE (t) == UNION_TYPE) { /* [class.union] (C++98) If a union contains a static data member, or a member of reference type, the program is ill-formed. - In C++11 this limitation doesn't exist anymore. */ - if (VAR_P (x)) + In C++11 [class.union] says: + If a union contains a non-static data member of reference type + the program is ill-formed. */ + if (VAR_P (x) && cxx_dialect < cxx11) { error ("in C++98 %q+D may not be static because it is " "a member of a union", x); continue; } - if (TREE_CODE (type) == REFERENCE_TYPE) + if (TREE_CODE (type) == REFERENCE_TYPE + && TREE_CODE (x) == FIELD_DECL) { - error ("in C++98 %q+D may not have reference type %qT " - "because it is a member of a union", x, type); + error ("non-static data member %q+D in a union may not " + "have reference type %qT", x, type); continue; } } --- gcc/testsuite/g++.dg/init/ref14.C.jj 2017-01-03 08:12:26.878515378 +0100 +++ gcc/testsuite/g++.dg/init/ref14.C 2017-01-04 22:40:35.789996635 +0100 @@ -4,7 +4,7 @@ union A { - int &i; // { dg-error "may not have reference type" "" { target { ! c++11 } } } + int &i; // { dg-error "may not have reference type" } }; void foo() --- gcc/testsuite/g++.dg/init/union1.C.jj 2017-01-03 08:12:26.862515585 +0100 +++ gcc/testsuite/g++.dg/init/union1.C 2017-01-04 22:40:35.789996635 +0100 @@ -1,5 +1,5 @@ // PR c++/14401 union U { - int& i; // { dg-error "reference type" "" { target { ! c++11 } } } + int& i; // { dg-error "reference type" } }; --- gcc/testsuite/g++.dg/cpp0x/pr78890-2.C.jj 2017-01-04 22:40:35.821996232 +0100 +++ gcc/testsuite/g++.dg/cpp0x/pr78890-2.C 2017-01-04 22:40:35.821996232 +0100 @@ -0,0 +1,44 @@ +// PR c++/78890 +// { dg-do compile { target c++11 } } + +template +int +foo () +{ + union { + int a; + int &b = a; // { dg-error "may not have reference type" } + }; + a = 1; + auto c = b + 1; + return c; +} + +template +T +bar () +{ + union { + T a; + T &b = a; // { dg-error "may not have reference type" } + }; + a = 1; + auto c = b + 1; + return c; +} + +template +T baz() +{ + union { + T a; + U b = a; // { dg-error "may not have reference type" } + }; + a = 1; + auto c = b + 1; + return c; +} + +int a = foo (); +int b = bar (); +int c = baz (); --- gcc/testsuite/g++.dg/cpp0x/union8.C.jj 2017-01-04 22:40:35.820996244 +0100 +++ gcc/testsuite/g++.dg/cpp0x/union8.C 2017-01-04 22:40:35.820996244 +0100 @@ -0,0 +1,26 @@ +// PR c++/78890 +// { dg-do compile { target c++11 } } + +union Test1 { + static int kConstant; +}; + +union Test2 { + static const int kConstant; +}; + +const int Test2::kConstant = 10; + +int k; + +union Test3 { + static int& kRef; +}; + +int& Test3::kRef = k; + +union Test4 { + static const int& kRef; +}; + +const int& Test4::kRef = 10; --- gcc/testsuite/g++.dg/cpp0x/union6.C.jj 2017-01-03 08:12:26.926514759 +0100 +++ gcc/testsuite/g++.dg/cpp0x/union6.C 2017-01-04 22:40:35.820996244 +0100 @@ -12,9 +12,9 @@ union Test2 { const int Test2::kConstant = 10; union Test3 { - int& kConstant; + int& kConstant; // { dg-error "may not have reference type" } }; union Test4 { - const int& kConstant = 10; + const int& kConstant = 10; // { dg-error "may not have reference type" } }; --- gcc/testsuite/g++.dg/cpp0x/pr78890-1.C.jj 2017-01-04 22:40:35.820996244 +0100 +++ gcc/testsuite/g++.dg/cpp0x/pr78890-1.C 2017-01-04 22:40:35.820996244 +0100 @@ -0,0 +1,13 @@ +// PR c++/78890 +// { dg-do compile { target c++11 } } + +int +main() +{ + union { + int a; + int &b = a; // { dg-error "may not have reference type" } + }; + a = 1; + auto c = b + 1; +}