From e11d05c1ed26257493130762a8ae240f1bc06e87 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 10 Mar 2020 18:55:42 -0400 Subject: [PATCH] c++: Fix wrong conversion error with non-viable overload [PR94124] This is a bad interaction between sharing a constructor for an array and stripping its trailing zero-initializers. Here we reuse a ctor and then strip its 0s. This breaks overload resolution in this test: D can be initialized from {} but not from {0}, so if we truncate the constructor not to include the zero, the F(D) overload becomes valid and then we get the ambiguous conversion error. PR c++/94124 - wrong conversion error with non-viable overload. * decl.c (reshape_init_array_1): Unshare a constructor if we stripped trailing zero-initializers. * g++.dg/cpp0x/initlist-overload1.C: New test. --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/decl.c | 7 +++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/cpp0x/initlist-overload1.C | 15 +++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-overload1.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9e0b48886ad..ebccb511ee0 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-03-10 Marek Polacek + + PR c++/94124 - wrong conversion error with non-viable overload. + * decl.c (reshape_init_array_1): Unshare a constructor if we + stripped trailing zero-initializers. + 2020-03-10 Jason Merrill PR c++/93901 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index bb242743074..aa58e5f88ae 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -6062,6 +6062,13 @@ reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d, else if (last_nonzero < nelts - 1) nelts = last_nonzero + 1; + /* Sharing a stripped constructor can get in the way of + overload resolution. E.g., initializing a class from + {{0}} might be invalid while initializing the same class + from {{}} might be valid. */ + if (reuse) + new_init = unshare_constructor (new_init); + vec_safe_truncate (CONSTRUCTOR_ELTS (new_init), nelts); } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index c76d891a3e6..d243255b07c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-03-10 Marek Polacek + + PR c++/94124 - wrong conversion error with non-viable overload. + * g++.dg/cpp0x/initlist-overload1.C: New test. + 2020-03-10 Jiufu Guo PR target/93709 diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-overload1.C b/gcc/testsuite/g++.dg/cpp0x/initlist-overload1.C new file mode 100644 index 00000000000..12bb606ce67 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-overload1.C @@ -0,0 +1,15 @@ +// PR c++/94124 - wrong conversion error with non-viable overload. +// { dg-do compile { target c++11 } } + +template struct A { typedef int _Type[N]; }; +template struct B { typename A::_Type _M_elems; }; +class C { }; +struct D { + D(C); +}; + +struct F { + F(B<2>); + F(D); // This overload should not be viable. +}; +F fn1() { return {{{0}}}; } -- 2.30.2