public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104365] New: Overload ambiguity not detected
@ 2022-02-03 10:28 andris at gcc dot gnu.org
  2022-02-03 11:11 ` [Bug c++/104365] " pinskia at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: andris at gcc dot gnu.org @ 2022-02-03 10:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

            Bug ID: 104365
           Summary: Overload ambiguity not detected
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andris at gcc dot gnu.org
  Target Milestone: ---

Created attachment 52336
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52336&action=edit
Test example

Overload ambiguity is not detected when 2 constructors with parameter types
- bool
- const std::string&
are available and string literal is provided as parameter. Compiler chooses
conversion to bool. Tested with versions gcc-11.2.0 (Arch Linux), gcc-8.5.0
(RHEL 8).

Test example attached

At least warning (or better an error) in this case would be nice as otherwise
failure to detected ambiguity causes difficult to find errors

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
@ 2022-02-03 11:11 ` pinskia at gcc dot gnu.org
  2022-02-03 11:17 ` andris at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-03 11:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I don't think this is ambigous as conversion from const char* to bool is better
than doing a copy constructor. 


Note only MSVC warns about the conversion:
<source>(18): warning C4800: Implicit conversion from 'const char *' to bool.
Possible information loss
<source>(18): note: consider using explicit cast or comparison to nullptr to
avoid this warning


No other compiler I tested warns though.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
  2022-02-03 11:11 ` [Bug c++/104365] " pinskia at gcc dot gnu.org
@ 2022-02-03 11:17 ` andris at gcc dot gnu.org
  2022-02-03 11:45 ` rguenth at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: andris at gcc dot gnu.org @ 2022-02-03 11:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

--- Comment #2 from Andris Pavenis <andris at gcc dot gnu.org> ---
OK. Then warning would be nice.

Otherwise it is a trap when one chooses to add bool parameter at end after
string 
parameter and other overloaded method with one more string parameter at end is
also present.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
  2022-02-03 11:11 ` [Bug c++/104365] " pinskia at gcc dot gnu.org
  2022-02-03 11:17 ` andris at gcc dot gnu.org
@ 2022-02-03 11:45 ` rguenth at gcc dot gnu.org
  2022-02-03 12:19 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-02-03 11:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-02-03 11:45 ` rguenth at gcc dot gnu.org
@ 2022-02-03 12:19 ` redi at gcc dot gnu.org
  2022-02-03 12:21 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-03 12:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-02-03
             Status|UNCONFIRMED                 |WAITING
     Ever confirmed|0                           |1
             Blocks|                            |87403

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> I don't think this is ambigous as conversion from const char* to bool is
> better than doing a copy constructor. 

Right, a standard conversion sequence is better than a user-defined conversion.

I think if you want a new warning here you need to be very clear about exactly
what you're asking for.

MSVC warns about converting const char* to bool, but that's nothing to do with
ambiguous overloads, it warns when there is no overloading at all:

void f(bool);
void g(const char* s) { f(s); }

This is warning about narrowing, not confusing overloads.

So please explain precisely what you want to warn, when you *don't* want it to
warn, and how users can easily modify their code to avoid the warning if the
overload set is written exactly how they intend it to be written, and is
correct.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87403
[Bug 87403] [Meta-bug] Issues that suggest a new warning

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-02-03 12:19 ` redi at gcc dot gnu.org
@ 2022-02-03 12:21 ` redi at gcc dot gnu.org
  2022-02-03 12:58 ` andris at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-03 12:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Your original testcase shows one example where you want a warning, but that's
not a specification. Should it only warn when std::string and bool are
involved, and there's a char* argument? Or should it be more general? How
general? What should the semantics be?

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2022-02-03 12:21 ` redi at gcc dot gnu.org
@ 2022-02-03 12:58 ` andris at gcc dot gnu.org
  2022-02-03 13:55 ` andris at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: andris at gcc dot gnu.org @ 2022-02-03 12:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

--- Comment #5 from Andris Pavenis <andris at gcc dot gnu.org> ---
The warning should be in case when both
1) there is preferred standard conversion sequence for parameter of one
overloaded method
2) there is other user defined conversion sequences for one more more other
overloaded methods

Message format could be similar as current ambiguity message only downgraded to
warning (maybe preferred conversion sequence could be marked in the message)

const char* -> bool and const char* -> std::string could be one example which
could likely cause problems

I could imagine similar situation with integer number conversion and some
extended precision number library.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2022-02-03 12:58 ` andris at gcc dot gnu.org
@ 2022-02-03 13:55 ` andris at gcc dot gnu.org
  2022-02-03 13:58 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: andris at gcc dot gnu.org @ 2022-02-03 13:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

--- Comment #6 from Andris Pavenis <andris at gcc dot gnu.org> ---
Suggested format (changed types to get real ambiguity and edited generated
message):

20220203-1.cpp: In function 'int main()':
20220203-1.cpp:19:24: warning: call of overloaded 'Test(const char [4],
unsigned 
char[4])' is ambiguous
   21 |     Test test("foo", "bar");
      |                          ^
20220203-1.cpp:12:5: note: chosen candidate: 'Test::Test(const string&, bool)'
   12 |     Test(const std::string&, bool)
      |     ^~~~
20220203-1.cpp:7:5: note: candidate: 'Test::Test(const string&, const
std::string&)'
    7 |     Test(const std::string&, const std::string&)
      |     ^~~~

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2022-02-03 13:55 ` andris at gcc dot gnu.org
@ 2022-02-03 13:58 ` redi at gcc dot gnu.org
  2022-02-03 14:03 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-03 13:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|1                           |0
             Status|WAITING                     |UNCONFIRMED

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Thanks

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2022-02-03 13:58 ` redi at gcc dot gnu.org
@ 2022-02-03 14:03 ` redi at gcc dot gnu.org
  2022-02-03 14:16 ` andris at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-03 14:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Andris Pavenis from comment #5)
> The warning should be in case when both
> 1) there is preferred standard conversion sequence for parameter of one
> overloaded method

Standard conversions include T -> const T&, and derived-to-base conversions,
and 
T* -> void*. I don't think anybody would be surprised that those conversions
beat a user-defined one.

> 2) there is other user defined conversion sequences for one more more other
> overloaded methods

And non-member functions?


(In reply to Andris Pavenis from comment #6)
> 20220203-1.cpp:19:24: warning: call of overloaded 'Test(const char [4],
> unsigned 
> char[4])' is ambiguous


"is ambiguous" is incorrect though, so it would have to be clear that there is
no ambiguity in C++ terms, just potential for confusion.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
                   ` (8 preceding siblings ...)
  2022-02-03 14:03 ` redi at gcc dot gnu.org
@ 2022-02-03 14:16 ` andris at gcc dot gnu.org
  2022-02-04  8:13 ` egallager at gcc dot gnu.org
  2022-02-04  8:25 ` andris at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: andris at gcc dot gnu.org @ 2022-02-03 14:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

--- Comment #9 from Andris Pavenis <andris at gcc dot gnu.org> ---
>> The warning should be in case when both
>> 1) there is preferred standard conversion sequence for parameter of one
>> overloaded method
>
>Standard conversions include T -> const T&, and derived-to-base conversions, > >and  T* -> void*. I don't think anybody would be surprised that those >conversions beat a user-defined one.

Perhaps there should be no warning in these cases.
[const] char * -> bool is good example which would deserve warning. I do not
have other examples currently.

>> 2) there is other user defined conversion sequences for one more more other
>> overloaded methods
>
>And non-member functions?

Should be handled in the same way as member functions

>> 20220203-1.cpp:19:24: warning: call of overloaded 'Test(const char [4],
>> unsigned 
>> char[4])' is ambiguous
>
>"is ambiguous" is incorrect though, so it would have to be clear that there is >no ambiguity in C++ terms, just potential for confusion.

Maybe 'suspicious use of overloaded ...' or something similar

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
                   ` (9 preceding siblings ...)
  2022-02-03 14:16 ` andris at gcc dot gnu.org
@ 2022-02-04  8:13 ` egallager at gcc dot gnu.org
  2022-02-04  8:25 ` andris at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-02-04  8:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #10 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #8)
> (In reply to Andris Pavenis from comment #6)
> > 20220203-1.cpp:19:24: warning: call of overloaded 'Test(const char [4],
> > unsigned 
> > char[4])' is ambiguous
> 
> 
> "is ambiguous" is incorrect though, so it would have to be clear that there
> is no ambiguity in C++ terms, just potential for confusion.

maybe "could appear ambiguous to a human reader" instead?

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/104365] Overload ambiguity not detected
  2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
                   ` (10 preceding siblings ...)
  2022-02-04  8:13 ` egallager at gcc dot gnu.org
@ 2022-02-04  8:25 ` andris at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: andris at gcc dot gnu.org @ 2022-02-04  8:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104365

--- Comment #11 from Andris Pavenis <andris at gcc dot gnu.org> ---
OK. New version:

20220203-1.cpp: In function 'int main()':
20220203-1.cpp:19:24: warning: suspicious use of overloaded 'Test(const char
[4], const char[4])'
   21 |     Test test("foo", "bar");
      |                          ^
20220203-1.cpp:12:5: note: chosen candidate: 'Test::Test(const string&, bool)'
   12 |     Test(const std::string&, bool)
      |     ^~~~
20220203-1.cpp:7:5: note: candidate: 'Test::Test(const string&, const
std::string&)'
    7 |     Test(const std::string&, const std::string&)
      |     ^~~~
[tag] [reply] [−]Comment 7

Also suggestion 'could appear ambiguous ...' instead of 'suspicious use ...' is
OK for me. I have no special preferences here.

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-02-04  8:25 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-03 10:28 [Bug c++/104365] New: Overload ambiguity not detected andris at gcc dot gnu.org
2022-02-03 11:11 ` [Bug c++/104365] " pinskia at gcc dot gnu.org
2022-02-03 11:17 ` andris at gcc dot gnu.org
2022-02-03 11:45 ` rguenth at gcc dot gnu.org
2022-02-03 12:19 ` redi at gcc dot gnu.org
2022-02-03 12:21 ` redi at gcc dot gnu.org
2022-02-03 12:58 ` andris at gcc dot gnu.org
2022-02-03 13:55 ` andris at gcc dot gnu.org
2022-02-03 13:58 ` redi at gcc dot gnu.org
2022-02-03 14:03 ` redi at gcc dot gnu.org
2022-02-03 14:16 ` andris at gcc dot gnu.org
2022-02-04  8:13 ` egallager at gcc dot gnu.org
2022-02-04  8:25 ` andris at gcc dot gnu.org

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).