From patchwork Fri Jun 2 15:53:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 70538 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 59B413858284 for ; Fri, 2 Jun 2023 15:54:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 59B413858284 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685721265; bh=aic8PyonBI7jgIbghIelOoZBDrWqkNe17KsDWHqR+5I=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=kYpI0fPaHPiK4Hu5aMfEX2BjvEcpl53fWqUhuhU1rLJ79yt/5Bfgr4DD2kw3r8PAG JIzoFpBbCAzAjZZSS5tMSZu/uzh8blGvrrInGC8QpB7IAuSOtD6fqTDSUS/+lzjn4g H/06bniBZU2lA96eX7mvBnvzY19fsFMS/S2uKMWo= 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 287773858D32 for ; Fri, 2 Jun 2023 15:53:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 287773858D32 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-347-ouLYfUqYM0m9c5mn8sdrCA-1; Fri, 02 Jun 2023 11:53:53 -0400 X-MC-Unique: ouLYfUqYM0m9c5mn8sdrCA-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6260cd5068cso27348496d6.2 for ; Fri, 02 Jun 2023 08:53:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685721233; x=1688313233; 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=aic8PyonBI7jgIbghIelOoZBDrWqkNe17KsDWHqR+5I=; b=FChyANMdvBA6krZkjQy9dYVr0xXtVCJr4jPC9Et79HFrZWQ/kyLSV8dlp2Ovs07/yX AUF2QKtsXNhMrfSRoI4sPjuDTwzQU8S0H8Z9Hho/Sw2ewq+4PmCtc1jRvqN5Ck44ayKh 7CJbwi1pzdXR+Snoc7jDlOyEGQJevk+by3vIx8XJR27404/jc6zjFOPYq6XkoSyjqFqP Zkilaz1coMZvDHbsBgUEvHs4QFP4e0Pishp9bHVxpe56Le0roFTj9LmQ5Fcwi9O8dvoT mo7twgUPDmr3rHHLfcAp0dS3VPNs5Jii/L2FyjHXUnXSXdvOXqNs0MBM4wru2TgaoLwO ZB5Q== X-Gm-Message-State: AC+VfDz85GG0zLI0TrL3GXh6KR7CwCFTh11VKzhSSL1bvj+PqeKFIWx3 154T0gz/ZKfMYCC00/iWLNVqh6WF8lfzvgLd6gf3YmRQqCY0Ll7n5j3YJVphF0H59KWXN2hvuFr HSMRSG2gQOyb/7XhbySKs2ZefG8Gn/ZzbLPNm11rgPUyY0k9s8NlfbC3DvYHsJTazeUboFx9QIg == X-Received: by 2002:a05:6214:21a9:b0:626:3a4d:b0ce with SMTP id t9-20020a05621421a900b006263a4db0cemr13575016qvc.49.1685721232765; Fri, 02 Jun 2023 08:53:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ680AMWpJ8/a50C1MYdijdr0ajBhz5PHGQ3FopZpHX9CkkYqIqL9fJOHFDVIiyW6n1Aa8q3Wg== X-Received: by 2002:a05:6214:21a9:b0:626:3a4d:b0ce with SMTP id t9-20020a05621421a900b006263a4db0cemr13575000qvc.49.1685721232355; Fri, 02 Jun 2023 08:53:52 -0700 (PDT) Received: from jason.com (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 ml4-20020a056214584400b0061fb6cc2051sm990636qvb.15.2023.06.02.08.53.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 08:53:51 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: fix explicit/copy problem [PR109247] Date: Fri, 2 Jun 2023 11:53:49 -0400 Message-Id: <20230602155349.1775177-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.9 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, 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 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-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- In the testcase, the user wants the assignment to use the operator= declared in the class, but because [over.match.list] says that explicit constructors are also considered for list-initialization, as affirmed in CWG1228, we end up choosing the implicitly-declared copy assignment operator, using the explicit constructor template for the argument, which is ill-formed. Other implementations haven't implemented CWG1228, so we keep getting bug reports. Discussion in CWG led to the idea for this targeted relaxation: if we use an explicit constructor for the conversion to the argument of a copy or move special member function, that makes the candidate worse than another. DR 2735 PR c++/109247 gcc/cp/ChangeLog: * call.cc (sfk_copy_or_move): New. (joust): Add tiebreaker for explicit conv and copy ctor. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist-explicit3.C: New test. --- gcc/cp/call.cc | 31 +++++++++++++++++++ .../g++.dg/cpp0x/initlist-explicit3.C | 15 +++++++++ 2 files changed, 46 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-explicit3.C base-commit: 957798e44e7194f7b6a67b19f85ff72eab9a0d0e diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 5d504e5c696..d6154f1a319 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -12611,6 +12611,17 @@ cand_parms_match (z_candidate *c1, z_candidate *c2) return compparms (parms1, parms2); } +/* True iff FN is a copy or move constructor or assignment operator. */ + +static bool +sfk_copy_or_move (tree fn) +{ + if (TREE_CODE (fn) != FUNCTION_DECL) + return false; + special_function_kind sfk = special_function_p (fn); + return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment; +} + /* Compare two candidates for overloading as described in [over.match.best]. Return values: @@ -12910,6 +12921,26 @@ joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn, return winner; } + /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an + explicit conversion (due to list-initialization) is worse. */ + { + z_candidate *sp = nullptr; + if (sfk_copy_or_move (cand1->fn)) + sp = cand1; + if (sfk_copy_or_move (cand2->fn)) + sp = sp ? nullptr : cand2; + if (sp) + { + conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)]; + if (conv->user_conv_p) + for (; conv; conv = next_conversion (conv)) + if (conv->kind == ck_user + && DECL_P (conv->cand->fn) + && DECL_NONCONVERTING_P (conv->cand->fn)) + return (sp == cand1) ? -1 : 1; + } + } + /* or, if not that, F1 is a non-template function and F2 is a template function specialization. */ diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-explicit3.C b/gcc/testsuite/g++.dg/cpp0x/initlist-explicit3.C new file mode 100644 index 00000000000..b0c92784566 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-explicit3.C @@ -0,0 +1,15 @@ +// PR c++/109247 +// { dg-do compile { target c++11 } } + +template struct optional { + template explicit optional(_Up); + template void operator=(_Up); +}; +int setPattern_pattern; +struct SourceBrush { + struct Brush { + int brush; + }; + void setPattern() { m_brush = {setPattern_pattern}; } + optional m_brush; +};