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.133.124]) by sourceware.org (Postfix) with ESMTPS id 356BC385840E for ; Mon, 14 Mar 2022 17:24:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 356BC385840E Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-596-hAbt_RrWNxyLWNPt6cITaQ-1; Mon, 14 Mar 2022 13:24:55 -0400 X-MC-Unique: hAbt_RrWNxyLWNPt6cITaQ-1 Received: by mail-qt1-f200.google.com with SMTP id y23-20020ac85257000000b002e06697f2ebso12083372qtn.16 for ; Mon, 14 Mar 2022 10:24:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=M5JHJi7P+foGzXOsS+l4gdWA7jZ9Djx0qMZ2KsWlOIM=; b=FQXIkKBlsK0n8fnBmJUq1h2bcgDZr88AZ+y6v4cGPLk5F0/f/s7sKrfF1E9a1C2tp+ y+Y2ZgGZuR+jBHtJDhcE9967AVPyhHEixfzC43jz7tqTEevpLQVUBry0506jvjv+GzYv MVVwPvOcR+BHk/6pHZqLZGJfh8s/CrUTJJ+dNCsU2MHckycr2mDhhWG4m0nch2QxXAT6 sFkdbroyuLatIggqGEgRdQnKiMxZmpq38bxaxUFg07n4Joq12jG7N7TSdX3hYpPd+keG 6YxZj1CMVhy9Z0g8GxoUf3Qyeo0yXeDLfkXRc4Z9wKyrFS8bcut/qI1qx0W+734E9IZA PkiA== X-Gm-Message-State: AOAM530dbqFM323S2nNS/YoVieZhR9iOprBtuhAdofpIc+Qi/U1+WPQS mEDIbNsxtB7WWVtV8+e9Z0cWISRl6fp7KVxtMTKy3w6dvAHWZTWmL9RzXCbwYrpO1QEt2X1bXLk km574GNncabxX1aoWHOv6K8VCqxGslYPTfBwGTQ8HWe+ozkBshLChLSCP6u8jwqPOp6E= X-Received: by 2002:a05:620a:4042:b0:67b:39c2:107d with SMTP id i2-20020a05620a404200b0067b39c2107dmr15639326qko.698.1647278694875; Mon, 14 Mar 2022 10:24:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxi4hyGXEFgrUBZnphbofk2aubV2fOJKhAWgLUWNA6yrneZkjsLIarWPTOwZspzYjz7r4B0UQ== X-Received: by 2002:a05:620a:4042:b0:67b:39c2:107d with SMTP id i2-20020a05620a404200b0067b39c2107dmr15639310qko.698.1647278694537; Mon, 14 Mar 2022 10:24:54 -0700 (PDT) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id l19-20020a05622a051300b002dff3437923sm11517683qtx.11.2022.03.14.10.24.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Mar 2022 10:24:54 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: extraneous access error with ambiguous lookup [PR103177] Date: Mon, 14 Mar 2022 13:24:51 -0400 Message-Id: <20220314172451.1341913-1-ppalka@redhat.com> X-Mailer: git-send-email 2.35.1.455.g1a4874565f MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-14.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Mar 2022 17:24:59 -0000 When a lookup is ambiguous, lookup_member still attempts to check access of the first member found before diagnosing the ambiguity and erroring out, which may cause us to issue an extraneous access error in case of ambiguous lookup as in the testcase below (for B1::foo). This patch fixes this by swapping the order of the ambiguity and access checks within lookup_member. In passing, since the only possible error that could happen during lookup_field_r is an ambiguity error, we might as well hardcode that in lookup_member instead and remove the unneeded lookup_field_info::errstr. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/103177 gcc/cp/ChangeLog: * search.cc (lookup_field_info::errstr): Remove this data member. (lookup_field_r): Don't set errstr. (lookup_member): Check ambiguity before checking access. Simplify accordingly after errstr removal. Exit early upon error or empty result. gcc/testsuite/ChangeLog: * g++.dg/lookup/ambig6.C: New test. --- gcc/cp/search.cc | 37 ++++++++++++---------------- gcc/testsuite/g++.dg/lookup/ambig6.C | 18 ++++++++++++++ 2 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 gcc/testsuite/g++.dg/lookup/ambig6.C diff --git a/gcc/cp/search.cc b/gcc/cp/search.cc index 2b8210408fb..85e3e7cb487 100644 --- a/gcc/cp/search.cc +++ b/gcc/cp/search.cc @@ -941,8 +941,6 @@ struct lookup_field_info { tree ambiguous; /* If nonzero, we are looking for types, not data members. */ int want_type; - /* If something went wrong, a message indicating what. */ - const char *errstr; }; /* True for a class member means that it is shared between all objects @@ -1055,7 +1053,6 @@ lookup_field_r (tree binfo, void *data) /* Add the new value. */ lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous); TREE_TYPE (lfi->ambiguous) = error_mark_node; - lfi->errstr = G_("request for member %qD is ambiguous"); } } else @@ -1127,8 +1124,6 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type, checks. Whereas rval is only set if a proper (not hidden) non-function member is found. */ - const char *errstr = 0; - if (name == error_mark_node || xbasetype == NULL_TREE || xbasetype == error_mark_node) @@ -1172,7 +1167,6 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type, rval_binfo = lfi.rval_binfo; if (rval_binfo) type = BINFO_TYPE (rval_binfo); - errstr = lfi.errstr; /* If we are not interested in ambiguities, don't report them; just return NULL_TREE. */ @@ -1187,6 +1181,19 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type, protect = 0; } + if (protect == 1 && lfi.ambiguous) + { + if (complain & tf_error) + { + error ("request for member %qD is ambiguous", name); + print_candidates (lfi.ambiguous); + } + return error_mark_node; + } + + if (!rval) + return NULL_TREE; + /* [class.access] In the case of overloaded function names, access control is @@ -1206,8 +1213,7 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type, only the first call to "f" is valid. However, if the function is static, we can check. */ - if (rval && protect - && !really_overloaded_fn (rval)) + if (protect && !really_overloaded_fn (rval)) { tree decl = is_overloaded_fn (rval) ? get_first_fn (rval) : rval; decl = strip_using_decl (decl); @@ -1216,21 +1222,10 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type, && !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) && !perform_or_defer_access_check (basetype_path, decl, decl, complain, afi)) - rval = error_mark_node; - } - - if (errstr && protect) - { - if (complain & tf_error) - { - error (errstr, name, type); - if (lfi.ambiguous) - print_candidates (lfi.ambiguous); - } - rval = error_mark_node; + return error_mark_node; } - if (rval && is_overloaded_fn (rval) + if (is_overloaded_fn (rval) /* Don't use a BASELINK for class-scope deduction guides since they're not actually member functions. */ && !dguide_name_p (name)) diff --git a/gcc/testsuite/g++.dg/lookup/ambig6.C b/gcc/testsuite/g++.dg/lookup/ambig6.C new file mode 100644 index 00000000000..45e8effba43 --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/ambig6.C @@ -0,0 +1,18 @@ +// PR c++/103177 + +struct B1 { +private: + static int foo(); +}; + +struct B2 { + int foo(); +}; + +struct D : B1, B2 { }; + +void f() { + D d; + d.foo(); // { dg-error "ambiguous" } + // { dg-bogus "private" "" { target *-*-* } .-1 } +} -- 2.35.1.455.g1a4874565f