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 612D43858D37 for ; Sat, 26 Aug 2023 00:34:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 612D43858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693010083; 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: in-reply-to:in-reply-to:references:references; bh=0lss6FLDdKmq0ovm/M74FChBHIBNvAH/dNaQN9OXOf4=; b=ZbaL5QMY32q1hKVNAmZG6Jy/IafGjaAEQh8LECxvbbLKPYUvg1HLYH9vkR4tyHEACbg6Wx 6S3uQnLucp8tSfXYwQ9oC79D0s/SkjpnafVEUSUaohdlawVi7FxnjtUIfnTmBCP/nmCHqN IQ2R4f1XDRpRozrw080k86wic5b3atc= 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-602-K9riFKEUMLmuyz7pg99ckQ-1; Fri, 25 Aug 2023 20:34:40 -0400 X-MC-Unique: K9riFKEUMLmuyz7pg99ckQ-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-64d09183380so15962446d6.0 for ; Fri, 25 Aug 2023 17:34:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693010079; x=1693614879; h=content-transfer-encoding:in-reply-to:from:references: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=0lss6FLDdKmq0ovm/M74FChBHIBNvAH/dNaQN9OXOf4=; b=UbnserL6+NT5EkqD5tqUSbJ71PSqcwGUfu3Ei+Yhl9B/dGimV6EinnmHq5PBD/QLf0 V2FNwalw3lWrE6Y29BQNa1I7i0Qab1HBy2E9rmJFun7atHZiKRK85PgaokmZvQ/wvA0q q1lPGwa4w0L/vehcf1pKd2SMamWZ2BjbH1p/3xqd1xEQzxBOKB+OTWEolwkDphbZ/Hhu 3IOupelLvQmpE3nF3MiWyadxaOhjCjbceecJsn7lnzq4EM0eR1JaF+l9JBqfVF2YhNdv npwod1klMo+C+MGxkmsRg7HEkGmzvBdg+ljVWnWU+8h1R+lu1hZhJ2bgKQZEW4VCza7Y RwDA== X-Gm-Message-State: AOJu0YyZ6yHp4HZlr+DiBAIjoONzqg6sob53uFyiZ3BYrsmAjy7yUcDu wDzQ4FLEDxB2XPjfxwnWaL1lA2OMVbo3PhdcfMHsN0gfze7DgrGb6/pv0YuKtnUGVbFUFoXc9iX N0eaiZFWp6gwF8ngINxTJmspMfA== X-Received: by 2002:a05:6214:320b:b0:63c:f964:dd67 with SMTP id qj11-20020a056214320b00b0063cf964dd67mr22125762qvb.49.1693010079705; Fri, 25 Aug 2023 17:34:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDuV6bXd2B6sAfyQfRxWswB25iWtM6C/A5BaHAMJWL57bm+85vxP7FXWOV+wqljzgvP1AGDg== X-Received: by 2002:a05:6214:320b:b0:63c:f964:dd67 with SMTP id qj11-20020a056214320b00b0063cf964dd67mr22125752qvb.49.1693010079436; Fri, 25 Aug 2023 17:34:39 -0700 (PDT) Received: from [192.168.1.108] (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 v8-20020a0cdd88000000b0064743dd0633sm880009qvk.106.2023.08.25.17.34.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 25 Aug 2023 17:34:38 -0700 (PDT) Message-ID: <16883fbb-a076-ff23-a23d-b92c4035ec87@redhat.com> Date: Fri, 25 Aug 2023 20:34:37 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH] c++: tweaks for explicit conversion fns diagnostic To: Marek Polacek , GCC Patches References: <20230825233746.904433-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20230825233746.904433-1-polacek@redhat.com> 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=-13.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,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 List-Id: On 8/25/23 19:37, Marek Polacek wrote: > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > -- >8 -- > > 1) When saying that a conversion is erroneous because it would use > an explicit constructor, it might be nice to show where exactly > the explicit constructor is located. For example, with this patch: > > [...] > explicit.C:4:12: note: 'S::S(int)' declared here > 4 | explicit S(int) { } > | ^ > > 2) When a conversion doesn't work out merely because the conversion > function necessary to do the conversion couldn't be used because > it was marked explicit, it would be useful to the user to say so, > rather than just saying "cannot convert". For example, with this patch: > > explicit.C:13:12: error: cannot convert 'S' to 'bool' in initialization > 13 | bool b = S{1}; > | ^~~~ > | | > | S > explicit.C:5:12: note: explicit conversion function was not considered > 5 | explicit operator bool() const { return true; } > | ^~~~~~~~ > > gcc/cp/ChangeLog: > > * call.cc (convert_like_internal): Show where the conversion function > was declared. > (maybe_show_nonconverting_candidate): New. > * cp-tree.h (maybe_show_nonconverting_candidate): Declare. > * typeck.cc (convert_for_assignment): Call it. > > gcc/testsuite/ChangeLog: > > * g++.dg/diagnostic/explicit.C: New test. > --- > gcc/cp/call.cc | 41 +++++++++++++++++++--- > gcc/cp/cp-tree.h | 1 + > gcc/cp/typeck.cc | 5 +++ > gcc/testsuite/g++.dg/diagnostic/explicit.C | 16 +++++++++ > 4 files changed, 59 insertions(+), 4 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/diagnostic/explicit.C > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > index 23e458d3252..09ebcf6a115 100644 > --- a/gcc/cp/call.cc > +++ b/gcc/cp/call.cc > @@ -8459,12 +8459,21 @@ convert_like_internal (conversion *convs, tree expr, tree fn, int argnum, > if (pedwarn (loc, 0, "converting to %qT from initializer list " > "would use explicit constructor %qD", > totype, convfn)) > - inform (loc, "in C++11 and above a default constructor " > - "can be explicit"); > + { > + inform (loc, "in C++11 and above a default constructor " > + "can be explicit"); > + inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here", > + convfn); I'd swap these two informs. > +++ b/gcc/testsuite/g++.dg/diagnostic/explicit.C > @@ -0,0 +1,16 @@ > +// { dg-do compile { target c++11 } } > + > +struct S { > + explicit S(int) { } > + explicit operator bool() const { return true; } // { dg-message "explicit conversion function was not considered" } > + explicit operator int() const { return 42; } // { dg-message "explicit conversion function was not considered" } > +}; > + > +void > +g () > +{ > + S s = {1}; // { dg-error "would use explicit constructor" } > + bool b = S{1}; // { dg-error "cannot convert .S. to .bool. in initialization" } > + int i; > + i = S{2}; // { dg-error "cannot convert .S. to .int. in assignment" } > +} Let's also test other copy-initialization contexts: parameter passing, return, throw, aggregate member initialization. Jason