From patchwork Mon Oct 23 23:51:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1854062 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=KA8tmkM2; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SDsS070Ylz202k for ; Tue, 24 Oct 2023 10:52:16 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 06AA83858031 for ; Mon, 23 Oct 2023 23:52:15 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 386F73858D37 for ; Mon, 23 Oct 2023 23:52:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 386F73858D37 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 386F73858D37 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=1698105123; cv=none; b=IxWjJsbFvTFe17fsneQUJogBmIoNKrGaXeKSmQrIOqkEzq6zKPArCKrJ6enYD5ZoWJ2fRm2PwF3ORaIViK1000htrhAtwO8IIoU9HwDB4oeIbJy7m4wSgWgX47tv7WHL3C788mi4UVMp9vIH3ySfVlAjqKsnqeGiOOnJtFvji50= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698105123; c=relaxed/simple; bh=+kKs13cVbtkoUrbHqprzhUSxx8BGDG8+woa12aRR4/c=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PV1fnPegawf8FkSAGP8dvoLn395jS9cVPwCuHNTsE0S54+q4HXBJAhi7SVT/71p0kl7JFiml8ddhUAreGZjNexRJP7Rm71icUMStHSpK2HHR1Z5Z7ZjmkRk0bDu7HUCzxvxF8QJ4kt8w7FytDzbSP+AkwqHqPJytZT6OpAKlPwo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698105120; 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; bh=g+On5CfDEdJK8H0D6uosCTmp1rJos3AYhq+mpL60GqQ=; b=KA8tmkM2GUsUTOW3GtLbZEv+RsRaf4RM1p9QvF1Ez90FxvxKgTFhpbrFsETncZfei2emj8 M+XBXX9ZdISuOgs8/SFhe91Nmlsi3532wZo78//nXW/G5UKz2ciy7A9Y6bgxW/oY6t0xfp l5R9vWK8NMS2xwGZEvzeDyf19A8G5DI= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-695-aflfVdYUOS-bBWwBcbvbew-1; Mon, 23 Oct 2023 19:51:59 -0400 X-MC-Unique: aflfVdYUOS-bBWwBcbvbew-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-66d12cd3591so45286866d6.3 for ; Mon, 23 Oct 2023 16:51:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698105118; x=1698709918; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=g+On5CfDEdJK8H0D6uosCTmp1rJos3AYhq+mpL60GqQ=; b=tgyRPPnxt28hX5tI2Lym9Gfj84wZrVw9BESj9x6Qkt9+G8jy3wJn2pDOJPThYCkteO THmQAfddfTk17CFBesmRLfBLkxXag9hPTEFJMAttS6m96FpGzvbuh4e/pPS7l123jvrp 3dMKNjpHkDmbAdcsk/uIArtRxtFq86HDq9fqUeVgUV+DjqVph84P0xA7x+yTco9SHS07 tYhRnAU/0HfAWWtOjAngIYn5fIsFdLAN8kcKoiyMAkiI6oa36u6t2WrT94sNDH7E0juo uo1ClUY+VaADOWGOFj2Au2MiAtcGINwKp2qTV/PSSbYvSBYmas6dwbmgbXb6NmTG9+lb rytA== X-Gm-Message-State: AOJu0Yy5Zz8C+Lz1ZN1JWtgk19f2t2HULHZm+dOhGV113P6v6Pr/T512 kcQn0sRPhzEjUUKBvuExfDTZlRHWl1rq9oDj+1C0Z9mfeUBoNA1FTB+3Cvtif5jELudOqWHocZ9 mW9VUtZKF7GJ+Zp27Z1s1FuLhUdj2qvIhk/KkoIDZSYyOFJ1pnP/L9MazUeath2PQ6IKVvprlw1 Q= X-Received: by 2002:ad4:5f87:0:b0:66d:1eb6:96c9 with SMTP id jp7-20020ad45f87000000b0066d1eb696c9mr15773551qvb.60.1698105117965; Mon, 23 Oct 2023 16:51:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEf5068K1448tUbpdwxIUBr6mXu4QxgHI2qB4y+H1/w+dhprnjIrTyttWaoT1NrE+n+qgA+1Q== X-Received: by 2002:ad4:5f87:0:b0:66d:1eb6:96c9 with SMTP id jp7-20020ad45f87000000b0066d1eb696c9mr15773529qvb.60.1698105117514; Mon, 23 Oct 2023 16:51:57 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id dn26-20020a05622a471a00b0040331a24f16sm3097548qtb.3.2023.10.23.16.51.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 16:51:57 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH v2 1/3] c++: sort candidates according to viability Date: Mon, 23 Oct 2023 19:51:52 -0400 Message-ID: <20231023235154.2971561-1-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.424.gceadf0f3cf MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org The second patch in this series is new and ensures that the candidates list isn't mysteriously missing some candidates when noting other candidates due to deletedness. -- >8 -- This patch: * changes splice_viable to move the non-viable candidates to the end of the list instead of removing them outright * makes tourney move the best candidate to the front of the candidate list * adjusts print_z_candidates to preserve our behavior of printing only viable candidates when diagnosing ambiguity * adds a parameter to print_z_candidates to control this default behavior (the follow-up patch will want to print all candidates when diagnosing deletedness) Thus after this patch we have access to the entire candidate list through the best viable candidate. This change also happens to fix diagnostics for the below testcase where we currently neglect to note the third candidate, since the presence of the two unordered non-strictly viable candidates causes splice_viable to prematurely get rid of the non-viable third candidate. gcc/cp/ChangeLog: * call.cc: Include "tristate.h". (splice_viable): Sort the candidate list according to viability. Don't remove non-viable candidates from the list. (print_z_candidates): Add defaulted only_viable_p parameter. By default only print non-viable candidates if there is no viable candidate. (tourney): Make 'candidates' parameter a reference. Ignore non-viable candidates. Move the true champ to the front of the candidates list, and update 'candidates' to point to the front. gcc/testsuite/ChangeLog: * g++.dg/overload/error5.C: New test. --- gcc/cp/call.cc | 163 +++++++++++++++---------- gcc/testsuite/g++.dg/overload/error5.C | 12 ++ 2 files changed, 113 insertions(+), 62 deletions(-) create mode 100644 gcc/testsuite/g++.dg/overload/error5.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 2eb54b5b6ed..89d422f7220 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -43,6 +43,7 @@ along with GCC; see the file COPYING3. If not see #include "attribs.h" #include "decl.h" #include "gcc-rich-location.h" +#include "tristate.h" /* The various kinds of conversion. */ @@ -160,7 +161,7 @@ static struct obstack conversion_obstack; static bool conversion_obstack_initialized; struct rejection_reason; -static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t); +static struct z_candidate * tourney (struct z_candidate *&, tsubst_flags_t); static int equal_functions (tree, tree); static int joust (struct z_candidate *, struct z_candidate *, bool, tsubst_flags_t); @@ -176,7 +177,8 @@ static void op_error (const op_location_t &, enum tree_code, enum tree_code, static struct z_candidate *build_user_type_conversion_1 (tree, tree, int, tsubst_flags_t); static void print_z_candidate (location_t, const char *, struct z_candidate *); -static void print_z_candidates (location_t, struct z_candidate *); +static void print_z_candidates (location_t, struct z_candidate *, + tristate = tristate::unknown ()); static tree build_this (tree); static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *); static bool any_strictly_viable (struct z_candidate *); @@ -3700,68 +3702,60 @@ add_template_conv_candidate (struct z_candidate **candidates, tree tmpl, } /* The CANDS are the set of candidates that were considered for - overload resolution. Return the set of viable candidates, or CANDS - if none are viable. If any of the candidates were viable, set + overload resolution. Sort CANDS so that the strictly viable + candidates appear first, followed by non-strictly viable candidates, + followed by unviable candidates. Returns the first candidate + in this sorted list. If any of the candidates were viable, set *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be - considered viable only if it is strictly viable. */ + considered viable only if it is strictly viable when setting + *ANY_VIABLE_P. */ static struct z_candidate* splice_viable (struct z_candidate *cands, bool strict_p, bool *any_viable_p) { - struct z_candidate *viable; - struct z_candidate **last_viable; - struct z_candidate **cand; - bool found_strictly_viable = false; + z_candidate *strictly_viable = nullptr; + z_candidate **strictly_viable_tail = &strictly_viable; + + z_candidate *non_strictly_viable = nullptr; + z_candidate **non_strictly_viable_tail = &non_strictly_viable; + + z_candidate *unviable = nullptr; + z_candidate **unviable_tail = &unviable; /* Be strict inside templates, since build_over_call won't actually do the conversions to get pedwarns. */ if (processing_template_decl) strict_p = true; - viable = NULL; - last_viable = &viable; - *any_viable_p = false; - - cand = &cands; - while (*cand) + for (z_candidate *cand = cands; cand; cand = cand->next) { - struct z_candidate *c = *cand; if (!strict_p - && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL)) - { - /* Be strict in the presence of a viable candidate. Also if - there are template candidates, so that we get deduction errors - for them instead of silently preferring a bad conversion. */ - strict_p = true; - if (viable && !found_strictly_viable) - { - /* Put any spliced near matches back onto the main list so - that we see them if there is no strict match. */ - *any_viable_p = false; - *last_viable = cands; - cands = viable; - viable = NULL; - last_viable = &viable; - } - } + && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL)) + /* Be strict in the presence of a viable candidate. Also if + there are template candidates, so that we get deduction errors + for them instead of silently preferring a bad conversion. */ + strict_p = true; - if (strict_p ? c->viable == 1 : c->viable) - { - *last_viable = c; - *cand = c->next; - c->next = NULL; - last_viable = &c->next; - *any_viable_p = true; - if (c->viable == 1) - found_strictly_viable = true; - } - else - cand = &c->next; + /* Move this candidate to the appropriate list according to + its viability. */ + auto& tail = (cand->viable == 1 ? strictly_viable_tail + : cand->viable == -1 ? non_strictly_viable_tail + : unviable_tail); + *tail = cand; + tail = &cand->next; } - return viable ? viable : cands; + *any_viable_p = (strictly_viable != nullptr + || (!strict_p && non_strictly_viable != nullptr)); + + /* Combine the lists. */ + *unviable_tail = nullptr; + *non_strictly_viable_tail = unviable; + *strictly_viable_tail = non_strictly_viable; + + return strictly_viable; } static bool @@ -3995,8 +3989,13 @@ print_z_candidate (location_t loc, const char *msgstr, } } +/* Print information about each overload candidate in CANDIDATES, + which is assumed to have gone through splice_viable and tourney + (if splice_viable succeeded). */ + static void -print_z_candidates (location_t loc, struct z_candidate *candidates) +print_z_candidates (location_t loc, struct z_candidate *candidates, + tristate only_viable_p /* = tristate::unknown () */) { struct z_candidate *cand1; struct z_candidate **cand2; @@ -4041,8 +4040,19 @@ print_z_candidates (location_t loc, struct z_candidate *candidates) } } + /* Unless otherwise specified, if there's a (strictly) viable candidate then + we assume we're being called as part of diagnosing ambiguity, in which case + we want to print only viable candidates since unviable candidates couldn't + have contributed to the ambiguity. */ + if (only_viable_p.is_unknown ()) + only_viable_p = candidates->viable == 1; + for (; candidates; candidates = candidates->next) - print_z_candidate (loc, N_("candidate:"), candidates); + { + if (only_viable_p.is_true () && candidates->viable != 1) + break; + print_z_candidate (loc, N_("candidate:"), candidates); + } } /* USER_SEQ is a user-defined conversion sequence, beginning with a @@ -13169,38 +13179,50 @@ tweak: /* Given a list of candidates for overloading, find the best one, if any. This algorithm has a worst case of O(2n) (winner is last), and a best case of O(n/2) (totally ambiguous); much better than a sorting - algorithm. */ + algorithm. The candidates list is assumed to be sorted according + to viability (via splice_viable). */ static struct z_candidate * -tourney (struct z_candidate *candidates, tsubst_flags_t complain) +tourney (struct z_candidate *&candidates, tsubst_flags_t complain) { struct z_candidate *champ = candidates, *challenger; int fate; struct z_candidate *champ_compared_to_predecessor = nullptr; + struct z_candidate *champ_predecessor = nullptr; + struct z_candidate *challenger_predecessor = champ; /* Walk through the list once, comparing each current champ to the next candidate, knocking out a candidate or two with each comparison. */ - for (challenger = champ->next; challenger; ) + for (challenger = champ->next; challenger && challenger->viable; ) { fate = joust (champ, challenger, 0, complain); if (fate == 1) - challenger = challenger->next; + { + challenger_predecessor = challenger; + challenger = challenger->next; + } else { if (fate == 0) { + champ_predecessor = challenger; champ = challenger->next; - if (champ == 0) - return NULL; + if (!champ || !champ->viable) + { + champ = nullptr; + break; + } champ_compared_to_predecessor = nullptr; } else { champ_compared_to_predecessor = champ; + champ_predecessor = challenger_predecessor; champ = challenger; } + challenger_predecessor = champ; challenger = champ->next; } } @@ -13208,15 +13230,32 @@ tourney (struct z_candidate *candidates, tsubst_flags_t complain) /* Make sure the champ is better than all the candidates it hasn't yet been compared to. */ - for (challenger = candidates; - challenger != champ; - challenger = challenger->next) + if (champ) + for (challenger = candidates; + challenger != champ; + challenger = challenger->next) + { + if (challenger == champ_compared_to_predecessor) + continue; + fate = joust (champ, challenger, 0, complain); + if (fate != 1) + { + champ = nullptr; + break; + } + } + + if (!champ) + return nullptr; + + /* Move the champ to the front of the candidate list. */ + + if (champ != candidates) { - if (challenger == champ_compared_to_predecessor) - continue; - fate = joust (champ, challenger, 0, complain); - if (fate != 1) - return NULL; + gcc_checking_assert (champ_predecessor->next == champ); + champ_predecessor->next = champ->next ? champ->next->next : nullptr; + champ->next = candidates; + candidates = champ; } return champ; diff --git a/gcc/testsuite/g++.dg/overload/error5.C b/gcc/testsuite/g++.dg/overload/error5.C new file mode 100644 index 00000000000..6a2f3b5ba35 --- /dev/null +++ b/gcc/testsuite/g++.dg/overload/error5.C @@ -0,0 +1,12 @@ +// Verify we note all three candidates when diagnosing overload +// resolution failure. The presence of the first two (ambiguous) +// non-strictly viable candidates used to make us prune the third +// and not note it. + +void f(int, int*); // { dg-message "candidate" } +void f(int*, int); // { dg-message "candidate" } +void f(int, int, int); // { dg-message "candidate" } + +int main() { + f(1, 2); // { dg-error "no match|invalid conversion" } +} From patchwork Mon Oct 23 23:51:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1854063 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Js6ct7C6; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SDsS44NRBz202k for ; Tue, 24 Oct 2023 10:52:20 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A3E9F3858D37 for ; Mon, 23 Oct 2023 23:52:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 AAE6B3858C74 for ; Mon, 23 Oct 2023 23:52:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AAE6B3858C74 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 AAE6B3858C74 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=1698105124; cv=none; b=QNnQsj4F2BwkURMeshM5cY8JDV9Zjd2os91B2Lk6+W0zLHVgG6npmKqvDuOGdqNuhEXovy7qRTNAKlUi0yDFppwS1da52jq0TCSJSyay2Ak5nCPcftJ7IwwwJrozI28KeeL09vSjzrl2jJFciNpqbC+HSwT4RXVhNd855/qBuHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698105124; c=relaxed/simple; bh=gmmHQoQlL5LieuiYGR8xa1Q7fpMIgbsnX0kAK2iARrU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Rlvtl0wuqmsJX4V+YB1vmAjQ60JsNio/OJWyuKG9OpBji/VsnO59a4lZZNtqq2eSsgkjEAWTyUBUH/XEwagkvjkSVCmMDTJIHVScUZe1gZyQu6FBNd0UmvQdeBVOBCKJsRvXOkXfZbjugAe5be+dl+51s55JXlDjQwXfxIeNrLs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698105121; 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=cNKtPO/oiGihDbwsKboDVDaTLITlDNqNEvxogumrzYs=; b=Js6ct7C6wLoKsJLW8NzHaILZyM3waiacaB1Fv9hC9ZLhVjlA+q197BDvcVOHXjwG4ugtSs jG0FDGZfopbiOxgQlIcICV0VMuPRZIAYGOAVgLJjh51UveaitTv+DglqgVaTxaqHxWP1Il sMp/GOAvkPYS46eNrUSvlML/YQ61xlw= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-197-vRmlDvOUO429b7Y5SsPnBQ-1; Mon, 23 Oct 2023 19:52:00 -0400 X-MC-Unique: vRmlDvOUO429b7Y5SsPnBQ-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-66d7b90c8ecso54592416d6.3 for ; Mon, 23 Oct 2023 16:52:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698105119; x=1698709919; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cNKtPO/oiGihDbwsKboDVDaTLITlDNqNEvxogumrzYs=; b=aAHAwyVT0OPBe1R9T1dKCHp4Bkkt/VNBO0+pjdjkqzXyQ31tyW7tmAwgnC+FaaZkKK 5lfqD5Q0KGdXC+fQ82XICflMVfw/D3mCeDak4fZBXIRm5Xwh9QumBf03l/QI4m+s9li0 0l+iW3l7QilE59CF114Oxp+MBiNBVFmZ37wYSGliUUVXBpSivv8PI6N3yOSmXxFFkzC4 H9pge17+msoIbXNbjVcmTBEan+6xBTBF8rUjk+I/EEmmlavbPAqH+D1kB1TkTXH22gYa MyVoUfaKJhzrJwCh/1+C4oFbIlJSMnIZxRGyXpMZ6LJgjNZjtTEDP37NA/WPMhY2UpwP CAkg== X-Gm-Message-State: AOJu0YwLsXxZIn7pj6+fatVA8isI5mQ5yK4CW5xjG5Tct7YhQewp84fE 5BVmj97daAh4hsqIrbkYTx/HeJCvCqsH9BwJKoky5ls9GmCSZDGdxNXSdTTJjsqqB8moxCB0f0f dkCq+7EMOZAj8TZ5X01s185lN1nUIYOgT+8Lk1WGQcmflCBUG9NEgwNPxnYrfdFl2SnITzFIAAZ s= X-Received: by 2002:a05:6214:e82:b0:66d:25cb:43ba with SMTP id hf2-20020a0562140e8200b0066d25cb43bamr15839427qvb.20.1698105118989; Mon, 23 Oct 2023 16:51:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEfFVQM2MWEyKum8f8EXaK613EVakxWmZntjZ++YRFbpMS1DDXJZKE0Via/0MDwAFe2r3ZBeQ== X-Received: by 2002:a05:6214:e82:b0:66d:25cb:43ba with SMTP id hf2-20020a0562140e8200b0066d25cb43bamr15839404qvb.20.1698105118571; Mon, 23 Oct 2023 16:51:58 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id dn26-20020a05622a471a00b0040331a24f16sm3097548qtb.3.2023.10.23.16.51.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 16:51:58 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH v2 2/3] c++: remember candidates that we ignored Date: Mon, 23 Oct 2023 19:51:53 -0400 Message-ID: <20231023235154.2971561-2-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.424.gceadf0f3cf In-Reply-To: <20231023235154.2971561-1-ppalka@redhat.com> References: <20231023235154.2971561-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org During overload resolution, we sometimes outright ignore a function from the overload set and leave no trace of it in the candidates list, for example when we find a perfect non-template candidate we discard all function templates, or when the callee is a template-id we discard all non-template functions. We should still however make note of these unviable functions when diagnosing overload resolution failure, but that's not possible if they're not present in the returned candidates list. To that end, this patch reworks add_candidates to add such ignored functions to the list. The new rr_ignored rejection reason is somewhat of a catch-all; we could perhaps split it up into more specific rejection reasons, but I leave that as future work. gcc/cp/ChangeLog: * call.cc (enum rejection_reason_code): Add rr_ignored. (add_ignored_candidate): Define. (ignored_candidate_p): Define. (add_template_candidate_real): Do add_ignored_candidate instead of returning NULL. (splice_viable): Put ignored (unviable) candidates last. (print_z_candidate): Handle ignored candidates. (build_new_function_call): Refine shortcut that calls cp_build_function_call_vec now that non-templates can appear in the candidate list for a template-id call. (add_candidates): Replace 'bad_fns' overload with 'bad_cands' candidate list. When not considering a candidate, add it to the list as an ignored candidate. Add all 'bad_cands' to the overload set as well. gcc/testsuite/ChangeLog: * g++.dg/diagnostic/param-type-mismatch-2.C: Rename template function test_7 that accidentally (perhaps) shares the same name as its non-template callee. * g++.dg/overload/error6.C: New test. --- gcc/cp/call.cc | 149 +++++++++++++----- .../g++.dg/diagnostic/param-type-mismatch-2.C | 20 +-- gcc/testsuite/g++.dg/overload/error6.C | 9 ++ 3 files changed, 132 insertions(+), 46 deletions(-) create mode 100644 gcc/testsuite/g++.dg/overload/error6.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 89d422f7220..3212d5268e0 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -441,7 +441,8 @@ enum rejection_reason_code { rr_template_unification, rr_invalid_copy, rr_inherited_ctor, - rr_constraint_failure + rr_constraint_failure, + rr_ignored, }; struct conversion_info { @@ -2224,6 +2225,34 @@ add_candidate (struct z_candidate **candidates, return cand; } +/* FN is a function from the overload set that we outright didn't even consider + (for some reason); add it to the list as an unviable "ignored" candidate. */ + +static z_candidate * +add_ignored_candidate (z_candidate **candidates, tree fn) +{ + /* No need to dynamically allocate these. */ + static const rejection_reason reason_ignored = { rr_ignored, {} }; + + struct z_candidate *cand = (struct z_candidate *) + conversion_obstack_alloc (sizeof (struct z_candidate)); + + cand->fn = fn; + cand->reason = const_cast (&reason_ignored); + cand->next = *candidates; + *candidates = cand; + + return cand; +} + +/* True iff CAND is a candidate added by add_ignored_candidate. */ + +static bool +ignored_candidate_p (const z_candidate *cand) +{ + return cand->reason && cand->reason->code == rr_ignored; +} + /* Return the number of remaining arguments in the parameter list beginning with ARG. */ @@ -3471,7 +3500,7 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl, } if (len < skip_without_in_chrg) - return NULL; + return add_ignored_candidate (candidates, tmpl); if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg), @@ -3609,7 +3638,7 @@ add_template_candidate_real (struct z_candidate **candidates, tree tmpl, if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR)) == LOOKUP_ONLYCONVERTING) && DECL_NONCONVERTING_P (fn)) - return NULL; + return add_ignored_candidate (candidates, fn); if (DECL_CONSTRUCTOR_P (fn) && nargs == 2) { @@ -3724,6 +3753,9 @@ splice_viable (struct z_candidate *cands, z_candidate *unviable = nullptr; z_candidate **unviable_tail = &unviable; + z_candidate *unviable_ignored = nullptr; + z_candidate **unviable_ignored_tail = &unviable_ignored; + /* Be strict inside templates, since build_over_call won't actually do the conversions to get pedwarns. */ if (processing_template_decl) @@ -3742,6 +3774,7 @@ splice_viable (struct z_candidate *cands, its viability. */ auto& tail = (cand->viable == 1 ? strictly_viable_tail : cand->viable == -1 ? non_strictly_viable_tail + : ignored_candidate_p (cand) ? unviable_ignored_tail : unviable_tail); *tail = cand; tail = &cand->next; @@ -3751,7 +3784,8 @@ splice_viable (struct z_candidate *cands, || (!strict_p && non_strictly_viable != nullptr)); /* Combine the lists. */ - *unviable_tail = nullptr; + *unviable_ignored_tail = nullptr; + *unviable_tail = unviable_ignored; *non_strictly_viable_tail = unviable; *strictly_viable_tail = non_strictly_viable; @@ -3901,6 +3935,8 @@ print_z_candidate (location_t loc, const char *msgstr, inform (cloc, "%s%qT (conversion)", msg, fn); else if (candidate->viable == -1) inform (cloc, "%s%#qD (near match)", msg, fn); + else if (ignored_candidate_p (candidate)) + inform (cloc, "%s%#qD (ignored)", msg, fn); else if (DECL_DELETED_FN (fn)) inform (cloc, "%s%#qD (deleted)", msg, fn); else if (candidate->reversed ()) @@ -3980,6 +4016,8 @@ print_z_candidate (location_t loc, const char *msgstr, "initialization from an expression of the same or derived " "type"); break; + case rr_ignored: + break; case rr_none: default: /* This candidate didn't have any issues or we failed to @@ -5023,7 +5061,12 @@ build_new_function_call (tree fn, vec **args, // If there is a single (non-viable) function candidate, // let the error be diagnosed by cp_build_function_call_vec. if (!any_viable_p && candidates && ! candidates->next - && (TREE_CODE (candidates->fn) == FUNCTION_DECL)) + && TREE_CODE (candidates->fn) == FUNCTION_DECL + /* A template-id callee consisting of a single (ignored) + non-template candidate needs to be diagnosed the + ordinary way. */ + && (TREE_CODE (fn) != TEMPLATE_ID_EXPR + || candidates->template_decl)) return cp_build_function_call_vec (candidates->fn, args, complain); // Otherwise, emit notes for non-viable candidates. @@ -6509,6 +6552,10 @@ add_candidates (tree fns, tree first_arg, const vec *args, else /*if (flags & LOOKUP_DEFAULTED)*/ which = non_templates; + /* Template candidates that we'll potentially ignore if the + perfect candidate optimization succeeds. */ + z_candidate *ignored_template_cands = nullptr; + /* During overload resolution, we first consider each function under the assumption that we'll eventually find a strictly viable candidate. This allows us to circumvent our defacto behavior when checking @@ -6519,20 +6566,29 @@ add_candidates (tree fns, tree first_arg, const vec *args, This trick is important for pruning member function overloads according to their const/ref-qualifiers (since all 'this' conversions are at worst bad) without breaking -fpermissive. */ - tree bad_fns = NULL_TREE; + z_candidate *bad_cands = nullptr; bool shortcut_bad_convs = true; again: for (tree fn : lkp_range (fns)) { - if (check_converting && DECL_NONCONVERTING_P (fn)) - continue; - if (check_list_ctor && !is_list_ctor (fn)) - continue; if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL) - continue; + { + if (template_only) + add_ignored_candidate (candidates, fn); + continue; + } if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL) - continue; + { + add_ignored_candidate (&ignored_template_cands, fn); + continue; + } + if ((check_converting && DECL_NONCONVERTING_P (fn)) + || (check_list_ctor && !is_list_ctor (fn))) + { + add_ignored_candidate (candidates, fn); + continue; + } tree fn_first_arg = NULL_TREE; const vec *fn_args = args; @@ -6589,22 +6645,19 @@ add_candidates (tree fns, tree first_arg, const vec *args, } if (TREE_CODE (fn) == TEMPLATE_DECL) - { - if (!add_template_candidate (candidates, - fn, - ctype, - explicit_targs, - fn_first_arg, - fn_args, - return_type, - access_path, - conversion_path, - flags, - strict, - shortcut_bad_convs, - complain)) - continue; - } + add_template_candidate (candidates, + fn, + ctype, + explicit_targs, + fn_first_arg, + fn_args, + return_type, + access_path, + conversion_path, + flags, + strict, + shortcut_bad_convs, + complain); else { add_function_candidate (candidates, @@ -6632,13 +6685,14 @@ add_candidates (tree fns, tree first_arg, const vec *args, { /* This candidate has been tentatively marked non-strictly viable, and we didn't compute all argument conversions for it (having - stopped at the first bad conversion). Add the function to BAD_FNS - to fully reconsider later if we don't find any strictly viable - candidates. */ + stopped at the first bad conversion). Move the candidate to + BAD_CANDS to fully reconsider later if we don't find any strictly + viable candidates. */ if (complain & (tf_error | tf_conv)) { - bad_fns = lookup_add (fn, bad_fns); - *candidates = (*candidates)->next; + *candidates = cand->next; + cand->next = bad_cands; + bad_cands = cand; } else /* But if we're in a SFINAE context, just mark this candidate as @@ -6652,21 +6706,44 @@ add_candidates (tree fns, tree first_arg, const vec *args, if (which == non_templates && !seen_perfect) { which = templates; + ignored_template_cands = nullptr; goto again; } else if (which == templates && !seen_strictly_viable && shortcut_bad_convs - && bad_fns) + && bad_cands) { /* None of the candidates are strictly viable, so consider again those - functions in BAD_FNS, this time without shortcutting bad conversions + functions in BAD_CANDS, this time without shortcutting bad conversions so that all their argument conversions are computed. */ which = either; - fns = bad_fns; + fns = NULL_TREE; + for (z_candidate *cand = bad_cands; cand; cand = cand->next) + { + tree fn = cand->fn; + if (cand->template_decl) + fn = TI_TEMPLATE (cand->template_decl); + fns = ovl_make (fn, fns); + } shortcut_bad_convs = false; + bad_cands = nullptr; goto again; } + + if (complain & tf_error) + { + /* Remember any omitted candidates if we need to print candidates + as part of overload resolution failure diagnostics. */ + for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands }) + { + z_candidate **omitted_cands_tail = &omitted_cands; + while (*omitted_cands_tail) + omitted_cands_tail = &(*omitted_cands_tail)->next; + *omitted_cands_tail = *candidates; + *candidates = omitted_cands; + } + } } /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first, diff --git a/gcc/testsuite/g++.dg/diagnostic/param-type-mismatch-2.C b/gcc/testsuite/g++.dg/diagnostic/param-type-mismatch-2.C index de7570a6efa..50c25cd49b7 100644 --- a/gcc/testsuite/g++.dg/diagnostic/param-type-mismatch-2.C +++ b/gcc/testsuite/g++.dg/diagnostic/param-type-mismatch-2.C @@ -129,22 +129,22 @@ int test_6 (int first, const char *second, float third, s6 *ptr) /* Template function. */ template -int test_7 (int one, T two, float three); // { dg-line test_7_decl } +int callee_7 (int one, T two, float three); // { dg-line callee_7_decl } int test_7 (int first, const char *second, float third) { - return test_7 (first, second, third); // { dg-line test_7_usage } - // { dg-message "cannot convert 'const char\\*' to 'const char\\*\\*'" "" { target *-*-* } test_7_usage } + return callee_7 (first, second, third); // { dg-line callee_7_usage } + // { dg-message "cannot convert 'const char\\*' to 'const char\\*\\*'" "" { target *-*-* } callee_7_usage } /* { dg-begin-multiline-output "" } - return test_7 (first, second, third); - ^~~~~~ - | - const char* + return callee_7 (first, second, third); + ^~~~~~ + | + const char* { dg-end-multiline-output "" } */ - // { dg-message "initializing argument 2 of 'int test_7\\(int, T, float\\) .with T = const char\\*\\*.'" "" { target *-*-* } test_7_decl } + // { dg-message "initializing argument 2 of 'int callee_7\\(int, T, float\\) .with T = const char\\*\\*.'" "" { target *-*-* } callee_7_decl } /* { dg-begin-multiline-output "" } - int test_7 (int one, T two, float three); - ~~^~~ + int callee_7 (int one, T two, float three); + ~~^~~ { dg-end-multiline-output "" } */ } diff --git a/gcc/testsuite/g++.dg/overload/error6.C b/gcc/testsuite/g++.dg/overload/error6.C new file mode 100644 index 00000000000..86a12eaa8de --- /dev/null +++ b/gcc/testsuite/g++.dg/overload/error6.C @@ -0,0 +1,9 @@ +// Verify we note even non-template candidates when diagnosing +// overload resolution failure for a template-id. + +template void f(T); // { dg-message "candidate" } +void f(int); // { dg-message {candidate: 'void f\(int\)' \(ignored\)} } + +int main() { + f(0, 0); // { dg-error "no match" } +} From patchwork Mon Oct 23 23:51:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1854064 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=UE727gQJ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SDsSg2xrkz202k for ; Tue, 24 Oct 2023 10:52:51 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 628A1385783F for ; Mon, 23 Oct 2023 23:52:49 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 E29EB3858C2A for ; Mon, 23 Oct 2023 23:52:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E29EB3858C2A 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 E29EB3858C2A 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=1698105124; cv=none; b=FvT+2X6J5Cle0igLCSwFh7jnClP9J7PD86W5hUIpCrLQjpVfPihI/uI5f0BDZD92qt/VWM4Pa6+uPSYtqiENuh3cYIvpF/sNXDgQt5+M+Z5/Ioev9z597OWt1Hdx+l7bMKye6q53X5RZldbQ54tl6i+pglnFqKH022Nm2kVG7Fw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698105124; c=relaxed/simple; bh=341VtsUASDPxgkQNw2VHyQUybqGMgeztW5xbcYYa1wo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Vp1s4r7iT2kD3UokLSGDfEo94TGJSmGQNh+PNXK2viCyHiIiSJ54cncF0zpCh7sQd4xK9OadL1P1/YYKttBJ8Zlbvu1OfkIuPWYj1pgBDT8QE4oR+oLDJFAj+nCYHtvgmDuM3i/KHzqd3zTLC14+uauRnC+7FWQKg4m9trlVe04= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698105122; 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=T97WVqOBWGXuSRNMpCL9tctTaykTAX0IwGc6FHyCu34=; b=UE727gQJU3NaoozUFGcjrkkjJqCc/uA5vqYHwIla3sUvoVtp+VzztHf/mzQw5QxYWje/nv O2Shy6WZOOdiJH04MfCU82Mtixf2Cwck8LC4v1/8XMwODGVEw0jcFoIyRe30C7ddpTigpT mfpCJHlf3YLOYVWI6/9/vJcTANSf5Oo= 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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-183--O5GIhkfPxSTvXFde-1qdg-1; Mon, 23 Oct 2023 19:52:01 -0400 X-MC-Unique: -O5GIhkfPxSTvXFde-1qdg-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-41e172143c3so40061161cf.1 for ; Mon, 23 Oct 2023 16:52:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698105120; x=1698709920; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T97WVqOBWGXuSRNMpCL9tctTaykTAX0IwGc6FHyCu34=; b=iGo1FY7D1AmWuH2SxVXyxHpXOeiT3RCuUqS2PHzfc1ZkkIA+maOP4A0ZWMK7JhPp9l hHKjJUFo6BKFDyheZ85lDQapHL6I1AV6O3/3VBIu6yiPnnVPWrP8e3wBKPzY+F/BhtqA 2eRIYsc3J3/YjofhUbvgQJyrC1aIkbv5m51wxkGvhtQN4MSHDAgH2CM2Dm2GpN+bqCXj G17G/lFxmJ+5tZzrbk4n+LN1LOPFVc5GVJ5TOANjX2JuhzDuWhycnnOi+2osl+kE89IS XXoEQps+4Zi/XftHHBKpwA54PjYa+sFWcPnB0Z/uCCtZ6GziWB1uOa0QxEC0k7o+3ArY j+qA== X-Gm-Message-State: AOJu0Ywxx6XmMAouPXJmanuTxL5GlYueB9/IUy8HjuPIDoFR5D7wX4xX oAOluz6jMGCAxe/h/77t9y/qH2K9zX3nDHO7vE9aLtixr0TjICF/Nm7PhYqm53m+5jdntV2VAiL tOInil8f1G+/RunDRapt5O6JJdHmIVipjwZQMI/blyshAVgPFXgIA3v2BXUVzRUl8DaHNvORyC/ g= X-Received: by 2002:a05:622a:1a05:b0:417:953c:ff57 with SMTP id f5-20020a05622a1a0500b00417953cff57mr15066495qtb.14.1698105120405; Mon, 23 Oct 2023 16:52:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHecYuKrTXqWe/zbn1ZFmvq60nWpCNch1V8FHMu0FznoPnDdPESPuF9TfXV9/oiFKICom+0fQ== X-Received: by 2002:a05:622a:1a05:b0:417:953c:ff57 with SMTP id f5-20020a05622a1a0500b00417953cff57mr15066482qtb.14.1698105120076; Mon, 23 Oct 2023 16:52:00 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id dn26-20020a05622a471a00b0040331a24f16sm3097548qtb.3.2023.10.23.16.51.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 16:51:59 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH v2 3/3] c++: note other candidates when diagnosing deletedness Date: Mon, 23 Oct 2023 19:51:54 -0400 Message-ID: <20231023235154.2971561-3-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.424.gceadf0f3cf In-Reply-To: <20231023235154.2971561-1-ppalka@redhat.com> References: <20231023235154.2971561-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org With the previous two patches in place, we can now extend our deletedness diagnostic to note the other considered candidates, e.g.: deleted16.C: In function 'int main()': deleted16.C:10:4: error: use of deleted function 'void f(int)' 10 | f(0); | ~^~~ deleted16.C:5:6: note: declared here 5 | void f(int) = delete; | ^ deleted16.C:5:6: note: candidate: 'void f(int)' (deleted) deleted16.C:6:6: note: candidate: 'void f(...)' 6 | void f(...); | ^ deleted16.C:7:6: note: candidate: 'void f(int, int)' 7 | void f(int, int); | ^ deleted16.C:7:6: note: candidate expects 2 arguments, 1 provided For now, these these notes are disabled when a deleted special member function is selected because it introduces a lot of new "cannot bind reference" errors in the testsuite when noting non-viable candidates, e.g. in cpp0x/initlist-opt1.C we would need to expect an error when noting unviability of A(A&&). (It'd be nice if we could downgrade such errors into notes when noting candidates...) gcc/cp/ChangeLog: * call.cc (build_over_call): Call print_z_candidates when diagnosing deletedness. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/deleted16.C: New test. --- gcc/cp/call.cc | 10 +++++++++- gcc/testsuite/g++.dg/cpp0x/deleted16.C | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/deleted16.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 3212d5268e0..1313d6516bd 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -9932,7 +9932,15 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) if (DECL_DELETED_FN (fn)) { if (complain & tf_error) - mark_used (fn); + { + mark_used (fn); + /* Note the other candidates we considered unless we selected a + special member function since the mismatch reasons for other + candidates are usually uninteresting, e.g. rvalue vs lvalue + reference binding . */ + if (cand->next && !special_memfn_p (fn)) + print_z_candidates (input_location, cand, /*only_viable_p=*/false); + } return error_mark_node; } diff --git a/gcc/testsuite/g++.dg/cpp0x/deleted16.C b/gcc/testsuite/g++.dg/cpp0x/deleted16.C new file mode 100644 index 00000000000..55acbfd9188 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/deleted16.C @@ -0,0 +1,24 @@ +// Verify we note other candidates when a deleted function is +// selected by overload resolution. +// { dg-do compile { target c++11 } } + +void f(int) = delete; // { dg-message "declared here|candidate" } +void f(...); // { dg-message "candidate" } +void f(int, int); // { dg-message "candidate" } + +// An example where the perfect candidate optimization causes us +// to ignore function templates. +void g(int) = delete; // { dg-message "declared here|candidate" } +template void g(T); // { dg-message "candidate" } + +// An example where we have a strictly viable candidate and +// an incompletely considered bad candidate. +template void h(T, T) = delete; // { dg-message "declared here|candidate" } +void h(int*, int) = delete; // { dg-message "candidate" } + +int main() { + f(0); // { dg-error "deleted" } + g(0); // { dg-error "deleted" } + h(1, 1); // { dg-error "deleted" } + // { dg-error "invalid conversion" "" { target *-*-* } .-1 } when noting 2nd cand +}