* C++ PATCH for c++/84906, silent wrong code with ambiguous conversion
@ 2018-03-16 18:49 Jason Merrill
0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2018-03-16 18:49 UTC (permalink / raw)
To: gcc-patches List
[-- Attachment #1: Type: text/plain, Size: 335 bytes --]
The problem here was that when we initially looked for the conversion
we were in direct-initialization context, but then when we tried to do
the lookup again to get an error it was done in copy-initialization
context, so it was no longer ambiguous. Fixed by remembering which
we're in.
Tested x86_64-pc-linux-gnu, applying to trunk.
[-- Attachment #2: 84906.diff --]
[-- Type: text/x-patch, Size: 2601 bytes --]
commit 0959fc01e907ccc575d09280154b437efce97f33
Author: Jason Merrill <jason@redhat.com>
Date: Fri Mar 16 11:24:23 2018 -0400
PR c++/84906 - silent wrong code with ambiguous conversion.
* call.c (build_user_type_conversion_1): Set need_temporary_p on
ambiguous conversion.
(convert_like_real): Check it.
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index a32f41915fc..23d4f82a1a0 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -93,7 +93,8 @@ struct conversion {
BOOL_BITFIELD bad_p : 1;
/* If KIND is ck_ref_bind ck_base_conv, true to indicate that a
temporary should be created to hold the result of the
- conversion. */
+ conversion. If KIND is ck_ambig, true if the context is
+ copy-initialization. */
BOOL_BITFIELD need_temporary_p : 1;
/* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
from a pointer-to-derived to pointer-to-base is being performed. */
@@ -3943,6 +3944,8 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags,
cand->second_conv->user_conv_p = true;
if (!any_strictly_viable (candidates))
cand->second_conv->bad_p = true;
+ if (flags & LOOKUP_ONLYCONVERTING)
+ cand->second_conv->need_temporary_p = true;
/* If there are viable candidates, don't set ICS_BAD_FLAG; an
ambiguous conversion is no worse than another user-defined
conversion. */
@@ -6834,8 +6837,10 @@ convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
if (complain & tf_error)
{
/* Call build_user_type_conversion again for the error. */
- build_user_type_conversion (totype, convs->u.expr, LOOKUP_IMPLICIT,
- complain);
+ int flags = (convs->need_temporary_p
+ ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
+ build_user_type_conversion (totype, convs->u.expr, flags, complain);
+ gcc_assert (seen_error ());
if (fn)
inform (DECL_SOURCE_LOCATION (fn),
" initializing argument %P of %qD", argnum, fn);
diff --git a/gcc/testsuite/g++.dg/cpp1y/auto-fn50.C b/gcc/testsuite/g++.dg/cpp1y/auto-fn50.C
new file mode 100644
index 00000000000..a2c7e40755b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/auto-fn50.C
@@ -0,0 +1,23 @@
+// PR c++/84906
+// { dg-do compile { target c++14 } }
+
+extern "C" int puts(const char*);
+
+struct aa {
+ operator auto() {
+ puts("auto");
+ return false;
+ }
+ explicit operator bool() {
+ puts("bool");
+ return true;
+ }
+};
+
+int main() {
+ aa x;
+ if (x) // { dg-error "ambiguous" }
+ puts("here");
+ else
+ puts("there");
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2018-03-16 18:45 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-16 18:49 C++ PATCH for c++/84906, silent wrong code with ambiguous conversion Jason Merrill
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).