public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: tweaks for explicit conversion fns diagnostic
Date: Fri, 25 Aug 2023 20:34:37 -0400	[thread overview]
Message-ID: <16883fbb-a076-ff23-a23d-b92c4035ec87@redhat.com> (raw)
In-Reply-To: <20230825233746.904433-1-polacek@redhat.com>

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


  reply	other threads:[~2023-08-26  0:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-25 23:37 Marek Polacek
2023-08-26  0:34 ` Jason Merrill [this message]
2023-08-28 23:24   ` [PATCH v2] " Marek Polacek
2023-08-29 20:42     ` Jason Merrill
2023-08-29 21:45       ` Marek Polacek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=16883fbb-a076-ff23-a23d-b92c4035ec87@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).