From patchwork Mon Oct 14 19:28:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1997079 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=GwedRBO/; 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 4XS6kT4wJmz1xvK for ; Tue, 15 Oct 2024 06:29:52 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ECC36385AC28 for ; Mon, 14 Oct 2024 19:29: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 ESMTP id 03D043858C98 for ; Mon, 14 Oct 2024 19:29:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 03D043858C98 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 03D043858C98 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=1728934167; cv=none; b=eC8XXM2sugcMsLJj1PXkV3Xcya2rzYdhbaAXOz2P58/zcZiiA4GQ9NfK+ImWylsY6f5NQXp9NEyc68iPDRQBfJXJz66SYmH79Pr/P4xp8rK7G1lBCOtlwPLIWA/I8d6ICFzihhT8393oueOYiEG8dmFDL0o5hxjcyB730p6CE5s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728934167; c=relaxed/simple; bh=C3k7NTbe2pbDFhEyq7sf6AHuw0UrSXMs+60Jpa8Elqo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=seEKWQtvDJdMKhVsIUEsK5CPpQBNRv8Kq5p1MWj+8SGtUo/HWMTkYfZoYBacITSfyNnjJIHOywRPpE4c/Cz2g9G2GTc8Es/ildOmq8stTGF66VqtB1nZLzBAI3XidXLzC3csIVovAYQ2olf/SE0Xz/VZmWFO4y/FG+s6lzCQxmc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728934164; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=W+RJAH7S1rHg3UCVqwRlICSOGoiP2QEBMZS5/JLM8is=; b=GwedRBO/qJkxiYLuMLaFwQMjPqZhax56slboTSBuyAidbFqFSr84URSAWb1BH5NtGU9RAE 6mr/lpViy/mXjJpRbzqjR3a9YUnySlJU3Myrxvnd85AiC6VdMoTiZwlz4LTyLIQHQu0dkE 643S18hD1kKbelAJKIuAsDxGFzqINao= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-389-Fp5TZrUGOV6c1wan7GW3xQ-1; Mon, 14 Oct 2024 15:29:21 -0400 X-MC-Unique: Fp5TZrUGOV6c1wan7GW3xQ-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6cbe4cb4252so97896546d6.2 for ; Mon, 14 Oct 2024 12:29:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728934160; x=1729538960; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=W+RJAH7S1rHg3UCVqwRlICSOGoiP2QEBMZS5/JLM8is=; b=bVyzEOkKxRUxUvepScgfCOtnuhiqjoZCZV4WGvzTF81/uau8hEAP6ICZ3S9QG3xMXA IFSRUAGorWbhWWwDnoDFCJ8+Rj491Hrz/0MvB68DCBRmfM4cD5iOfx9ljJx9lgZn2Vua fbEJDf8epx8WbL0ZK3IDTMMoTqFYqj2PZQVdG2WzbPnXJsGz3wVAkr3C3ngjFzYm1Xqg 7CEgNwpjrbIas8F/3HGX3ILN8arzoEuFfACr4/CsbxvF+yFcUkHxwWYN5n5Pc/iWWdy0 ictAjSOI3O7If8wuw9tdjFJLRrUka5AxG+INKbrFDWXiKeH2EJ67BayYH2w4FT8T3HH8 2o9w== X-Gm-Message-State: AOJu0Yz/ie0OapWXAj5ZRnAzKcTt+lNFJLXSmbQ96+ImpHCYeRLNNAXA v8+oCSMn1z85K+78DZPQHWpdiVuEmCxb+vaMuGo0JGEltn1JY4Zo/RoWKxisl8Y6yac4vlfU80h dxD7qaT2rRgeft9w0uUBYY/jHit0tjkM4yy2sO7aIrqjTvQspIWUEbx/7XjIABH5wU6AVOhH58U N68h//RsZHx47c/vYE7lOEQsUPYIOY33FPMd4= X-Received: by 2002:a05:6214:3201:b0:6cb:90ba:edb2 with SMTP id 6a1803df08f44-6cbf0040720mr212118676d6.41.1728934159850; Mon, 14 Oct 2024 12:29:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGSOGvMCo5iLoDAiJFWOA0cj+IOI6YvpMuu8RlSMUB7mqJyHlehc9U0HFgk8iQn7XtIQ1uWjQ== X-Received: by 2002:a05:6214:3201:b0:6cb:90ba:edb2 with SMTP id 6a1803df08f44-6cbf0040720mr212118246d6.41.1728934159299; Mon, 14 Oct 2024 12:29:19 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (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 6a1803df08f44-6cbe85a5b7fsm48931086d6.2.2024.10.14.12.29.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2024 12:29:18 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: address deduction and concepts [CWG2918] Date: Mon, 14 Oct 2024 15:28:18 -0400 Message-ID: <20241014192916.4119199-1-jason@redhat.com> X-Mailer: git-send-email 2.47.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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_H3, 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 Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- CWG2918 changes deduction from an overload set for the case where multiple candidates succeed and have the same type; previously this made the overload set a non-deduced context, now it succeeds since the result is consistent between the candidates. This is needed for cases of overloading based on requirements, where we want to choose the most constrained overload. I also needed to adjust resolve_address_of_overloaded_function accordingly; we already handled the comparison for template candidates in most_specialized_instantiation, but need to also do the comparison for non-template candidates such as member functions of a class template. CWG 2918 (proposed) gcc/cp/ChangeLog: * cp-tree.h (most_constrained_function): Declare.. * class.cc (resolve_address_of_overloaded_function): Call it. * pt.cc (get_template_for_ordering): Handle list from resolve_address_of_overloaded_function. (most_constrained_function): No longer static. (resolve_overloaded_unification): Always compare type rather than decl. gcc/testsuite/ChangeLog: * g++.dg/DRs/dr2918.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/class.cc | 13 ++++++++++--- gcc/cp/pt.cc | 18 ++++++++++++++---- gcc/testsuite/g++.dg/DRs/dr2918.C | 11 +++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/DRs/dr2918.C base-commit: fa04a1713b54bdc4c7b686c88117c9c46043ec73 diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index dc153a97dc4..94ee550bd9c 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7593,6 +7593,7 @@ extern int template_args_equal (tree, tree); extern tree maybe_process_partial_specialization (tree); extern tree most_specialized_instantiation (tree); extern tree most_specialized_partial_spec (tree, tsubst_flags_t, bool = false); +extern tree most_constrained_function (tree); extern void print_candidates (tree); extern void instantiate_pending_templates (int); extern tree tsubst_default_argument (tree, int, tree, tree, diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc index 646072d4f20..8c39bb4a76b 100644 --- a/gcc/cp/class.cc +++ b/gcc/cp/class.cc @@ -8896,11 +8896,18 @@ resolve_address_of_overloaded_function (tree target_type, tree match = most_specialized_instantiation (matches); if (match != error_mark_node) - matches = tree_cons (TREE_PURPOSE (match), - NULL_TREE, - NULL_TREE); + { + matches = match; + TREE_CHAIN (match) = NULL_TREE; + } } } + else if (flag_concepts && TREE_CHAIN (matches)) + if (tree match = most_constrained_function (matches)) + { + matches = match; + TREE_CHAIN (match) = NULL_TREE; + } /* Now we should have exactly one function in MATCHES. */ if (matches == NULL_TREE) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 03a1144765b..c0a37a51cba 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -2071,6 +2071,10 @@ get_template_for_ordering (tree list) { gcc_assert (TREE_CODE (list) == TREE_LIST); tree f = TREE_VALUE (list); + if (f == NULL_TREE) + /* Also handle a list from resolve_address_of_overloaded_function with the + function in TREE_PURPOSE. */ + f = TREE_PURPOSE (list); if (tree ti = DECL_TEMPLATE_INFO (f)) return TI_TEMPLATE (ti); return f; @@ -2084,7 +2088,7 @@ get_template_for_ordering (tree list) Note that we don't compare constraints on the functions themselves, but rather those of their templates. */ -static tree +tree most_constrained_function (tree candidates) { // Try to find the best candidate in a first pass. @@ -23865,11 +23869,14 @@ resolve_overloaded_unification (tree tparms, tree fn = *iter; if (flag_noexcept_type) maybe_instantiate_noexcept (fn, tf_none); - if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn), + if (TREE_CODE (fn) == FUNCTION_DECL && !constraints_satisfied_p (fn)) + continue; + tree elem = TREE_TYPE (fn); + if (try_one_overload (tparms, targs, tempargs, parm, elem, strict, sub_strict, addr_p, explain_p) - && (!goodfn || !decls_match (goodfn, fn))) + && (!goodfn || !same_type_p (goodfn, elem))) { - goodfn = fn; + goodfn = elem; ++good; } } @@ -23879,6 +23886,9 @@ resolve_overloaded_unification (tree tparms, overloaded functions does not contain function templates and at most one of a set of overloaded functions provides a unique match. + CWG2918 allows multiple functions to match if they all have the same type, + so that we can choose the most constrained later. + So if we found multiple possibilities, we return success but don't deduce anything. */ diff --git a/gcc/testsuite/g++.dg/DRs/dr2918.C b/gcc/testsuite/g++.dg/DRs/dr2918.C new file mode 100644 index 00000000000..982ced71f1f --- /dev/null +++ b/gcc/testsuite/g++.dg/DRs/dr2918.C @@ -0,0 +1,11 @@ +// CWG 2918 makes this OK +// { dg-do compile { target c++20 } } + +template struct X { + static void f(short) requires B; // #1 + static void f(short); // #2 +}; +void test() { + auto x = &X::f; // OK, deduces void(*)(short), selects #1 + auto y = &X::f; // OK, deduces void(*)(short), selects #2 +}