diff options
author | Marek Polacek <polacek@redhat.com> | 2020-11-18 22:49:59 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2020-11-19 13:14:41 -0500 |
commit | 25056bdf94d5f3e66bef039702f7fae799ef16b9 (patch) | |
tree | 75c4f85b1eb7fda62c5fbf90379a9b1e7eadccbb /gcc | |
parent | e1f07131e2d88b08f75ffea2a8dcfb16607629aa (diff) | |
download | gcc-25056bdf94d5f3e66bef039702f7fae799ef16b9.zip gcc-25056bdf94d5f3e66bef039702f7fae799ef16b9.tar.gz gcc-25056bdf94d5f3e66bef039702f7fae799ef16b9.tar.bz2 |
c++: Fix crash with broken deduction from {} [PR97895]
Unfortunately, the otherwise beautiful
for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init))
is not immune to an empty constructor, so we have to check
CONSTRUCTOR_ELTS first.
gcc/cp/ChangeLog:
PR c++/97895
* pt.c (do_auto_deduction): Don't crash when the constructor has
zero elements.
gcc/testsuite/ChangeLog:
PR c++/97895
* g++.dg/cpp0x/auto54.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/pt.c | 11 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp0x/auto54.C | 10 |
2 files changed, 17 insertions, 4 deletions
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 1babf83..a1b6631 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -29250,10 +29250,13 @@ do_auto_deduction (tree type, tree init, tree auto_node, return error_mark_node; if (BRACE_ENCLOSED_INITIALIZER_P (init)) - /* We don't recurse here because we can't deduce from a nested - initializer_list. */ - for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init)) - elt.value = resolve_nondeduced_context (elt.value, complain); + { + /* We don't recurse here because we can't deduce from a nested + initializer_list. */ + if (CONSTRUCTOR_ELTS (init)) + for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init)) + elt.value = resolve_nondeduced_context (elt.value, complain); + } else init = resolve_nondeduced_context (init, complain); diff --git a/gcc/testsuite/g++.dg/cpp0x/auto54.C b/gcc/testsuite/g++.dg/cpp0x/auto54.C new file mode 100644 index 0000000..0c1815a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/auto54.C @@ -0,0 +1,10 @@ +// PR c++/97895 +// { dg-do compile { target c++11 } } + +namespace std { + template<typename T> struct initializer_list { + const T *ptr; + decltype(sizeof 0) n; + }; + auto a = {}; // { dg-error "unable to deduce" } +} |