diff options
author | Marek Polacek <polacek@redhat.com> | 2020-03-10 18:55:42 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2020-03-10 23:12:41 -0400 |
commit | e11d05c1ed26257493130762a8ae240f1bc06e87 (patch) | |
tree | 637dab153ef59698c03e1513b71568a444ca724a /gcc | |
parent | 481fcfe6fec156ca2a6baea4b623076e2eefa6a6 (diff) | |
download | gcc-e11d05c1ed26257493130762a8ae240f1bc06e87.zip gcc-e11d05c1ed26257493130762a8ae240f1bc06e87.tar.gz gcc-e11d05c1ed26257493130762a8ae240f1bc06e87.tar.bz2 |
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/cp/decl.c | 7 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp0x/initlist-overload1.C | 15 |
4 files changed, 33 insertions, 0 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9e0b488..ebccb51 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-03-10 Marek Polacek <polacek@redhat.com> + + 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 <jason@redhat.com> PR c++/93901 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index bb24274..aa58e5f 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 c76d891..d243255 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-03-10 Marek Polacek <polacek@redhat.com> + + PR c++/94124 - wrong conversion error with non-viable overload. + * g++.dg/cpp0x/initlist-overload1.C: New test. + 2020-03-10 Jiufu Guo <guojiufu@linux.ibm.com> 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 0000000..12bb606 --- /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 <int N> struct A { typedef int _Type[N]; }; +template <int N> struct B { typename A<N>::_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}}}; } |