From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id BEDFF3858295 for ; Thu, 30 Nov 2023 16:42:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BEDFF3858295 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BEDFF3858295 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701362563; cv=none; b=SbzpZ8BtEQNz4qYrvaxGHvdVfoONbzgdTT32qOzO+HdbFXaX1BFRCuufPvj1H2q3xo9O6NJPF3d934pSvFmopGnZeQBEy+lkfTMhQs/LCsI2jGQXF04LxKjZII+0Go3M7/q3utUQsFlfBOMemcEct35YqNH8SFPbMbYb7KmKiD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701362563; c=relaxed/simple; bh=yk7YPnK5q0mhRhuWOAhD+0WtbW2SRtwhF8mbwRpqlmQ=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=ErgGKPzEQl/JrnS4edug7xV+/EzVfLz4zGjZqEmiktS/03RFOBIstixpoec38Bv5nlxgD3I5adk/bTkxnC8Nz4YLw36cA87BtN7y//RqeygXhFkAZ3flrE2oVxU09tG45aASnXqGIPooYl3MnyQhBkuIwUnDbDlVZn2SAPrQh4c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701362560; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vFiulFttY89gTj1mq8OZa+O2Lktw/q3TVtlOscLZcnQ=; b=Mxp+OnfjXuu45F8VCy2Q2+Ak1VJ5grqSOXNFV+5kGglAnJE7OBem2aZFGLufCGU4ancRqw CYineS/68I/tfzeJD6GIZKvpI8W9NEzJF35ABX6DcurBkAjcuFF+mueYArgElapVd3xih7 nf85/pmvXYvtBXFNm603392rJj+RoI8= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-557-g5voWwF5MdKHyAl5tzhktQ-1; Thu, 30 Nov 2023 11:42:38 -0500 X-MC-Unique: g5voWwF5MdKHyAl5tzhktQ-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-423e1bc498fso15288731cf.1 for ; Thu, 30 Nov 2023 08:42:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701362558; x=1701967358; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vFiulFttY89gTj1mq8OZa+O2Lktw/q3TVtlOscLZcnQ=; b=gCLKDoDUhjoIlPq2P9nsz7+FtjUUtAJ1iIRoXKfwA2Z4Uwoig1/3yaUHL3GqBHNgyJ YykIP2IvuTaKsLIwcsgRiWSVmyZZXjCkiVwcnVSzfSPRcdWzJVJciY1qkAPgKha1yivZ gZrtXP2hIUUhEtwZIGnxjmkvxbywumwJT6viP1kMAtdo+HyzLFY6uzPUXYkIrTfSPC15 KQgCAkvxt4Sbn057s18RS4YW6eV107+SuVIi+7nFCv6eHosKbxGiIKzp5pE5ktpMHCcx BQ129Ufmar7NZkWJWqh5pWQJWXCEUdKA1+l2Zo2UxPskjOA5c7bU0W4CYb1kLgn2lK96 tB4w== X-Gm-Message-State: AOJu0Yz0OR2qIKrmdknT2Q8keRmMQAjg5GwbCMEz9bGk7mmdP9Cyx0g9 ZgY0URro+fPe7UgQRYBPmK4z70IJ8JOdeXveH3SJFUHplZkC4RpVlK/LRkczH4p5wjz+7IYX1YG mxmJzMybUSOqa/S2CwA== X-Received: by 2002:ac8:7fd5:0:b0:423:7b1e:e2ae with SMTP id b21-20020ac87fd5000000b004237b1ee2aemr29072296qtk.42.1701362558192; Thu, 30 Nov 2023 08:42:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IEJ6MLtyghhP6FHMPNkI3khSGBVbnrqTKmG8gnQOJvM2KkQUJTRh/F6zgKQYaIeuuGLm/5oEA== X-Received: by 2002:ac8:7fd5:0:b0:423:7b1e:e2ae with SMTP id b21-20020ac87fd5000000b004237b1ee2aemr29072269qtk.42.1701362557773; Thu, 30 Nov 2023 08:42:37 -0800 (PST) Received: from [192.168.1.145] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id b1-20020ac84f01000000b0041b016faf7esm634798qte.58.2023.11.30.08.42.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 30 Nov 2023 08:42:37 -0800 (PST) Message-ID: <271299fc-638a-4313-ad16-0e2cf0ec9708@redhat.com> Date: Thu, 30 Nov 2023 11:42:36 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] c++: wrong ambiguity in accessing static field [PR112744] To: Marek Polacek Cc: Patrick Palka , GCC Patches References: <20231129154512.428173-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 11/29/23 17:01, Marek Polacek wrote: > On Wed, Nov 29, 2023 at 03:28:44PM -0500, Jason Merrill wrote: >> On 11/29/23 12:43, Marek Polacek wrote: >>> On Wed, Nov 29, 2023 at 12:23:46PM -0500, Patrick Palka wrote: >>>> On Wed, 29 Nov 2023, Marek Polacek wrote: >>>> >>>>> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? >>>>> >>>>> Now that I'm posting this patch, I think you'll probably want me to use >>>>> ba_any unconditionally. That works too; g++.dg/tc1/dr52.C just needs >>>>> a trivial testsuite tweak: >>>>> 'C' is not an accessible base of 'X' >>>>> v. >>>>> 'C' is an inaccessible base of 'X' >>>>> We should probably unify those messages... >>>>> >>>>> -- >8 -- >>>>> Given >>>>> >>>>> struct A { constexpr static int a = 0; }; >>>>> struct B : A {}; >>>>> struct C : A {}; >>>>> struct D : B, C {}; >>>>> >>>>> we give the "'A' is an ambiguous base of 'D'" error for >>>>> >>>>> D{}.A::a; >>>>> >>>>> which seems wrong: 'a' is a static data member so there is only one copy >>>>> so it can be unambiguously referred to even if there are multiple A >>>>> objects. clang++/MSVC/icx agree. >>>>> >>>>> PR c++/112744 >>>>> >>>>> gcc/cp/ChangeLog: >>>>> >>>>> * typeck.cc (finish_class_member_access_expr): When accessing >>>>> a static data member, use ba_any for lookup_base. >>>>> >>>>> gcc/testsuite/ChangeLog: >>>>> >>>>> * g++.dg/lookup/scoped11.C: New test. >>>>> * g++.dg/lookup/scoped12.C: New test. >>>>> * g++.dg/lookup/scoped13.C: New test. >>>>> --- >>>>> gcc/cp/typeck.cc | 21 ++++++++++++++++++--- >>>>> gcc/testsuite/g++.dg/lookup/scoped11.C | 14 ++++++++++++++ >>>>> gcc/testsuite/g++.dg/lookup/scoped12.C | 14 ++++++++++++++ >>>>> gcc/testsuite/g++.dg/lookup/scoped13.C | 14 ++++++++++++++ >>>>> 4 files changed, 60 insertions(+), 3 deletions(-) >>>>> create mode 100644 gcc/testsuite/g++.dg/lookup/scoped11.C >>>>> create mode 100644 gcc/testsuite/g++.dg/lookup/scoped12.C >>>>> create mode 100644 gcc/testsuite/g++.dg/lookup/scoped13.C >>>>> >>>>> diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc >>>>> index e995fb6ddd7..c4de8bb2616 100644 >>>>> --- a/gcc/cp/typeck.cc >>>>> +++ b/gcc/cp/typeck.cc >>>>> @@ -3476,7 +3476,7 @@ finish_class_member_access_expr (cp_expr object, tree name, bool template_p, >>>>> name, scope); >>>>> return error_mark_node; >>>>> } >>>>> - >>>>> + >>>>> if (TREE_SIDE_EFFECTS (object)) >>>>> val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val); >>>>> return val; >>>>> @@ -3493,9 +3493,24 @@ finish_class_member_access_expr (cp_expr object, tree name, bool template_p, >>>>> return error_mark_node; >>>>> } >>>>> + /* NAME may refer to a static data member, in which case there is >>>>> + one copy of the data member that is shared by all the objects of >>>>> + the class. So NAME can be unambiguously referred to even if >>>>> + there are multiple indirect base classes containing NAME. */ >>>>> + const base_access ba = [scope, name] () >>>>> + { >>>>> + if (identifier_p (name)) >>>>> + { >>>>> + tree m = lookup_member (scope, name, /*protect=*/0, >>>>> + /*want_type=*/false, tf_none); >>>>> + if (!m || VAR_P (m)) >>>>> + return ba_any; >>>> >>>> I wonder if we want to return ba_check_bit instead of ba_any so that we >>>> still check access of the selected base? >>> >>> That would certainly make sense to me. I didn't do that because >>> I'd not seen ba_check_bit being used except as part of ba_check, >>> but that may not mean much. >>> >>> So either I can tweak the lambda to return ba_check_bit rather >>> than ba_any or use ba_check_bit unconditionally. Any opinions on that? >> >> The relevant passage seems to be >> https://eel.is/c++draft/class.access.base#6 >> after DR 52, which seems to have clarified that the pointer conversion only >> applies to non-static members. >> >>>> struct A { constexpr static int a = 0; }; >>>> struct D : private A {}; >>>> >>>> void f() { >>>> D{}.A::a; // #1 GCC (and Clang) currently rejects >>>> } >> >> I see that MSVC also rejects it, while EDG accepts. >> >> https://eel.is/c++draft/class.access.base#5.1 seems to say that a is >> accessible when named in A. >> >> https://eel.is/c++draft/expr.ref#7 also only constrains references to >> non-static members. >> >> But first we need to look up A in D, and A's injected-class-name looked up >> as a member of D is not accessible; it's private, and f() is not a friend, >> and we correctly complain about that. >> >> If we avoid the lookup of A in D with >> >> D{}.::A::a; >> >> clang accepts it, which is consistent with accepting the template version, >> and seems correct. >> >> So, I think ba_any is what we want here. > > Wow, that is not intuitive (to me at least). So I had it right but > only by accident. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > -- >8 -- > Given > > struct A { constexpr static int a = 0; }; > struct B : A {}; > struct C : A {}; > struct D : B, C {}; > > we give the "'A' is an ambiguous base of 'D'" error for > > D{}.A::a; > > which seems wrong: 'a' is a static data member so there is only one copy > so it can be unambiguously referred to even if there are multiple A > objects. clang++/MSVC/icx agree. > > The rationale for using ba_any is explained at > . I'd prefer not to cite the mailing list for rationales. To summarize: [class.access.base] requires conversion to a unique base subobject for non-static data members, but it does not require that the base be unique or accessible for static data members. > PR c++/112744 > > gcc/cp/ChangeLog: > > * typeck.cc (finish_class_member_access_expr): When accessing > a static data member, use ba_any for lookup_base. > > gcc/testsuite/ChangeLog: > > * g++.dg/lookup/scoped11.C: New test. > * g++.dg/lookup/scoped12.C: New test. > * g++.dg/lookup/scoped13.C: New test. > * g++.dg/lookup/scoped14.C: New test. > * g++.dg/lookup/scoped15.C: New test. > --- > gcc/cp/typeck.cc | 21 ++++++++++++++++++--- > gcc/testsuite/g++.dg/lookup/scoped11.C | 14 ++++++++++++++ > gcc/testsuite/g++.dg/lookup/scoped12.C | 14 ++++++++++++++ > gcc/testsuite/g++.dg/lookup/scoped13.C | 14 ++++++++++++++ > gcc/testsuite/g++.dg/lookup/scoped14.C | 14 ++++++++++++++ > gcc/testsuite/g++.dg/lookup/scoped15.C | 20 ++++++++++++++++++++ > 6 files changed, 94 insertions(+), 3 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/lookup/scoped11.C > create mode 100644 gcc/testsuite/g++.dg/lookup/scoped12.C > create mode 100644 gcc/testsuite/g++.dg/lookup/scoped13.C > create mode 100644 gcc/testsuite/g++.dg/lookup/scoped14.C > create mode 100644 gcc/testsuite/g++.dg/lookup/scoped15.C > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > index 0839d0a4167..bf8ffaa7e75 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -3467,7 +3467,7 @@ finish_class_member_access_expr (cp_expr object, tree name, bool template_p, > name, scope); > return error_mark_node; > } > - > + > if (TREE_SIDE_EFFECTS (object)) > val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val); > return val; > @@ -3484,9 +3484,24 @@ finish_class_member_access_expr (cp_expr object, tree name, bool template_p, > return error_mark_node; > } > > + /* NAME may refer to a static data member, in which case there is > + one copy of the data member that is shared by all the objects of > + the class. So NAME can be unambiguously referred to even if > + there are multiple indirect base classes containing NAME. */ > + const base_access ba = [scope, name] () > + { > + if (identifier_p (name)) > + { > + tree m = lookup_member (scope, name, /*protect=*/0, > + /*want_type=*/false, tf_none); > + if (!m || shared_member_p (m)) > + return ba_any; > + } > + return ba_check; > + } (); > + > /* Find the base of OBJECT_TYPE corresponding to SCOPE. */ > - access_path = lookup_base (object_type, scope, ba_check, > - NULL, complain); > + access_path = lookup_base (object_type, scope, ba, NULL, complain); > if (access_path == error_mark_node) > return error_mark_node; > if (!access_path) > diff --git a/gcc/testsuite/g++.dg/lookup/scoped11.C b/gcc/testsuite/g++.dg/lookup/scoped11.C > new file mode 100644 > index 00000000000..be743522fce > --- /dev/null > +++ b/gcc/testsuite/g++.dg/lookup/scoped11.C > @@ -0,0 +1,14 @@ > +// PR c++/112744 > +// { dg-do compile } > + > +struct A { const static int a = 0; }; > +struct B : A {}; > +struct C : A {}; > +struct D : B, C {}; > + > +int main() > +{ > + D d; > + (void) d.a; > + (void) d.A::a; > +} > diff --git a/gcc/testsuite/g++.dg/lookup/scoped12.C b/gcc/testsuite/g++.dg/lookup/scoped12.C > new file mode 100644 > index 00000000000..ffa145598fd > --- /dev/null > +++ b/gcc/testsuite/g++.dg/lookup/scoped12.C > @@ -0,0 +1,14 @@ > +// PR c++/112744 > +// { dg-do compile } > + > +class A { const static int a = 0; }; > +struct B : A {}; > +struct C : A {}; > +struct D : B, C {}; > + > +int main() > +{ > + D d; > + (void) d.a; // { dg-error "private" } > + (void) d.A::a; // { dg-error "private" } > +} > diff --git a/gcc/testsuite/g++.dg/lookup/scoped13.C b/gcc/testsuite/g++.dg/lookup/scoped13.C > new file mode 100644 > index 00000000000..970e1aa833e > --- /dev/null > +++ b/gcc/testsuite/g++.dg/lookup/scoped13.C > @@ -0,0 +1,14 @@ > +// PR c++/112744 > +// { dg-do compile } > + > +struct A { const static int a = 0; }; > +struct B : A {}; > +struct C : A {}; > +struct D : B, C {}; > + > +int main() > +{ > + D d; > + (void) d.x; // { dg-error ".struct D. has no member named .x." } > + (void) d.A::x; // { dg-error ".struct A. has no member named .x." } > +} > diff --git a/gcc/testsuite/g++.dg/lookup/scoped14.C b/gcc/testsuite/g++.dg/lookup/scoped14.C > new file mode 100644 > index 00000000000..141aa0d2b1a > --- /dev/null > +++ b/gcc/testsuite/g++.dg/lookup/scoped14.C > @@ -0,0 +1,14 @@ > +// PR c++/112744 > +// { dg-do compile { target c++11 } } > + > +struct A { int a = 0; }; > +struct B : A {}; > +struct C : A {}; > +struct D : B, C {}; > + > +int main() > +{ > + D d; > + (void) d.a; // { dg-error "request for member .a. is ambiguous" } > + (void) d.A::a; // { dg-error ".A. is an ambiguous base of .D." } > +} > diff --git a/gcc/testsuite/g++.dg/lookup/scoped15.C b/gcc/testsuite/g++.dg/lookup/scoped15.C > new file mode 100644 > index 00000000000..d450a41a617 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/lookup/scoped15.C > @@ -0,0 +1,20 @@ > +// PR c++/112744 > +// { dg-do compile { target c++11 } } > + > +struct A { constexpr static int a = 0; }; > +struct D : private A {}; > + > +// See > +// for rationale. The injected-class-name of A is private when named in D, but if A is named some other way, there is no requirement in [class.access.base] for static data members that it be an accessible base. OK with those comment adjustments. Jason