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 114F93858D20 for ; Tue, 29 Aug 2023 20:42:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 114F93858D20 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=1693341757; 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=u11KU+akv0q1p3qggGGGwD3y3w23jWTyhkmJHZZSmGk=; b=EfLowv0x9eBmZRhAkQ1C2X69Vl9bZCYbQyaWKkKcbPRvUNHgLoInlQ4sxQcBMTw0Myio9k IzYYwew4pv4DQqM/Yb7M2WHnUBf4x4gprZdPtmadifviAsMeh724TqRdFNtcg+Zjk0oj4J jwA7s2Zf/WBgQD8s9pPo2fvg6HtvPxg= 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-696-S-GD4FcTP2GHo0S49Kzyng-1; Tue, 29 Aug 2023 16:42:36 -0400 X-MC-Unique: S-GD4FcTP2GHo0S49Kzyng-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-410a0cbccfbso54058961cf.1 for ; Tue, 29 Aug 2023 13:42:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693341755; x=1693946555; h=content-transfer-encoding:in-reply-to:from:references:cc: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=u11KU+akv0q1p3qggGGGwD3y3w23jWTyhkmJHZZSmGk=; b=HzuzbWL5AfW1eywIhl07I/DTCBWiivmRnm+HTZQivfBBC4svc/CHJ3spuBlPLafs51 NgIyl6pZJ7Pn7f0m2LiGo9tgl9wgROjS0RvoJNnndi3FfQIQsTLNgLDqg3wQQ5fkPJPM jIJG8e+0+ja9xOhLcPeFaqfdoFfcEVnFLpQ2Xk58SvSrC/2zVE1P/IH6eIIaa3/A1vz1 /Kl01htEVCB3nc+SL0GzlAeMlIbL5AMbq98zX5UvUzGJwFz2ZuMZyn4z4hxPX767TgHH ssekEHIxnYh3lantjiYVifLfgU3sVSfdVFTKrisKZPr4rDm4gDQFKx7WhIH4wo73Ifpc MwIQ== X-Gm-Message-State: AOJu0YxkWeZmIyCA9fadroUlHS7LFnl/zHMtq8XAyZhfRxLZrptYwhet pEO9ZKYrLL4AGlAmX71UuTsBim2IRKS5UOuVXcLKEjFAiZvOROrIKO9T7rlHrZ53P/fgubii19O vKxtFfNdkqaOOhIyR9ect/SklNg== X-Received: by 2002:ac8:5705:0:b0:411:f6a8:a0e4 with SMTP id 5-20020ac85705000000b00411f6a8a0e4mr155252qtw.61.1693341755127; Tue, 29 Aug 2023 13:42:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEcDGsNXbyET25KmJwTwcvQPA1ktbCLc5KFTpi1R6VfyIpjMtwD8zNr8BGMK2l+WD+qqXtO0g== X-Received: by 2002:ac8:5705:0:b0:411:f6a8:a0e4 with SMTP id 5-20020ac85705000000b00411f6a8a0e4mr155240qtw.61.1693341754754; Tue, 29 Aug 2023 13:42:34 -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 l28-20020ac84cdc000000b00401e04c66fesm3252621qtv.37.2023.08.29.13.42.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 29 Aug 2023 13:42:34 -0700 (PDT) Message-ID: Date: Tue, 29 Aug 2023 16:42:33 -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 v2] c++: tweaks for explicit conversion fns diagnostic To: Marek Polacek Cc: GCC Patches References: <20230825233746.904433-1-polacek@redhat.com> <16883fbb-a076-ff23-a23d-b92c4035ec87@redhat.com> From: Jason Merrill In-Reply-To: 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.2 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/28/23 19:24, Marek Polacek wrote: > On Fri, Aug 25, 2023 at 08:34:37PM -0400, Jason Merrill wrote: >> 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. > > Done. > >>> +++ 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. > > Done except for throw. To handle arg passing I moved the call to > maybe_show_nonconverting_candidate one line down. I guess a testcase > for throw would be > > struct T { > T() { } // #1 > explicit T(const T&) { } // #2 > }; > > void > g () > { > T t{}; > throw t; > } > > but #2 isn't a viable candidate so this would take more effort to handle. True, copy-initialization is different when the types are the same. > We just say about #1 that "candidate expects 0 arguments, 1 provided". > > clang++ says > > e.C:3:12: note: explicit constructor is not a candidate > 3 | explicit T(const T&) { } > | ^ That would be better; in add_candidates when we see an explicit constructor we could add it to bad_fns instead of ignoring it. But that doesn't need to be part of this patch. > 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 | 6 ++++ > gcc/testsuite/g++.dg/diagnostic/explicit.C | 33 +++++++++++++++++ > 4 files changed, 77 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..52c9f4265a4 100644 > --- a/gcc/cp/call.cc > +++ b/gcc/cp/call.cc > @@ -14323,4 +14332,28 @@ is_list_ctor (tree decl) > return true; > } > > +/* We know that can_convert_arg_bad already said "no" when trying to convert > + FROM to TO with ARG and FLAGS. Try to figure out if it was because > + an explicit conversion function was skipped when looking for a way to > + perform the conversion. At this point we've already printed an error. */ > + > +void > +maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags) > +{ > + if (!(flags & LOOKUP_ONLYCONVERTING)) > + return; > + > + conversion_obstack_sentinel cos; > + conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false, > + flags & ~LOOKUP_ONLYCONVERTING, tf_none); > + if (c && !c->bad_p && c->user_conv_p) > + /* Ay, the conversion would have worked in copy-init context. */ s/copy/direct/ OK with that change. Jason