From e011c0100a83030e0cfd230658debbb6c97660a1 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Mon, 21 Oct 2019 18:22:41 +0000 Subject: [PATCH] PR c++/92106 - ICE with structured bindings and -Wreturn-local-addr. * typeck.c (maybe_warn_about_returning_address_of_local): Avoid recursing on null initializer and return false instead. * g++.dg/cpp1z/decomp50.C: New test. From-SVN: r277264 --- gcc/cp/ChangeLog | 6 ++++ gcc/cp/typeck.c | 6 ++-- gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/g++.dg/cpp1z/decomp50.C | 51 +++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/decomp50.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 30accc59417..29c6bbd3230 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2019-10-21 Marek Polacek + + PR c++/92106 - ICE with structured bindings and -Wreturn-local-addr. + * typeck.c (maybe_warn_about_returning_address_of_local): Avoid + recursing on null initializer and return false instead. + 2019-10-17 JeanHeyd Meneide Implement p1301 [[nodiscard("should have a reason")]] + p1771 DR diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 83604f546fc..627dfc2a4fc 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -9355,8 +9355,10 @@ maybe_warn_about_returning_address_of_local (tree retval) tree base = DECL_DECOMP_BASE (whats_returned); if (TYPE_REF_P (TREE_TYPE (base))) { - tree init = DECL_INITIAL (base); - return maybe_warn_about_returning_address_of_local (init); + if (tree init = DECL_INITIAL (base)) + return maybe_warn_about_returning_address_of_local (init); + else + return false; } } bool w = false; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0259236e416..7365a393a7d 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-10-21 Marek Polacek + + PR c++/92106 - ICE with structured bindings and -Wreturn-local-addr. + * g++.dg/cpp1z/decomp50.C: New test. + 2019-10-21 Richard Biener PR tree-optimization/92162 diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp50.C b/gcc/testsuite/g++.dg/cpp1z/decomp50.C new file mode 100644 index 00000000000..5400a826948 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp50.C @@ -0,0 +1,51 @@ +// PR c++/92106 - ICE with structured bindings and -Wreturn-local-addr. +// { dg-do compile { target c++17 } } + +template struct B; +template struct B<_Tp *> { typedef _Tp& reference; }; +struct C { + template using rebind = _Up *; +}; +template class D { +public: + typename B<_Iterator>::reference operator*(); + void operator++(); +}; + +template +bool operator!=(D<_Iterator, _Container>, D<_Iterator, _Container>); +template class F { +public: + typedef _Tp value_type; +}; + +template struct G { + template struct H { using type = C::rebind<_Tp>; }; + using const_pointer = typename H::type; +}; +template > class I { + typedef D::const_pointer, int> const_iterator; + +public: + const_iterator begin(); + const_iterator end(); +}; + +struct A { + struct J { + int name; + int value; + }; + I members; + template const int *find(Key) { + for (const auto &[name, value] : members) + // See + // for why we don't warn here. + return &value; // { dg-bogus "address of local variable" } + return nullptr; + } +}; +int main() { + A a; + a.find(""); +} -- 2.34.1