[gcc/devel/ranger] libstdc++: Fix regression in std::_Construct (PR 94831)

Aldy Hernandez aldyh@gcc.gnu.org
Wed Jun 17 20:50:53 GMT 2020


https://gcc.gnu.org/g:162c40a4c127cc55d701bb8760e17708d0ca2fe0

commit 162c40a4c127cc55d701bb8760e17708d0ca2fe0
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Apr 28 23:26:21 2020 +0100

    libstdc++: Fix regression in std::_Construct (PR 94831)
    
    By trying to reuse the existing std::_Construct function as a wrapper
    for std::construct_at I introduced regressions, because changing
    std::_Construct to return non-void made it ill-formed for array types.
    
    The solution is to revert _Construct to its former state, and change
    allocator_traits::construct to explicitly call construct_at instead.
    This decouples all the existing callers of _Construct from the new
    construct_at requirements.
    
            PR libstdc++/94831
            * include/bits/alloc_traits.h (_S_construct): Restore placement
            new-expression for C++11/14/17 and call std::construct_at directly
            for C++20.
            * include/bits/stl_construct.h (_Construct): Revert to non-constexpr
            function returning void.
            * testsuite/20_util/specialized_algorithms/
            uninitialized_value_construct/94831.cc: New test.
            * testsuite/23_containers/vector/cons/94831.cc: New test.

Diff:
---
 libstdc++-v3/ChangeLog                             | 12 +++++++++
 libstdc++-v3/include/bits/alloc_traits.h           |  8 +++++-
 libstdc++-v3/include/bits/stl_construct.h          | 20 ++++++---------
 .../uninitialized_value_construct/94831.cc         | 29 ++++++++++++++++++++++
 .../testsuite/23_containers/vector/cons/94831.cc   | 29 ++++++++++++++++++++++
 5 files changed, 84 insertions(+), 14 deletions(-)

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index f2ddc5e4776..a5b6769ce88 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,15 @@
+2020-04-28  Jonathan Wakely  <jwakely@redhat.com>
+
+	PR libstdc++/94831
+	* include/bits/alloc_traits.h (_S_construct): Restore placement
+	new-expression for C++11/14/17 and call std::construct_at directly
+	for C++20.
+	* include/bits/stl_construct.h (_Construct): Revert to non-constexpr
+	function returning void.
+	* testsuite/20_util/specialized_algorithms/
+	uninitialized_value_construct/94831.cc: New test.
+	* testsuite/23_containers/vector/cons/94831.cc: New test.
+
 2020-04-28  Patrick Palka  <ppalka@redhat.com>
 
 	LWG 3433 subrange::advance(n) has UB when n < 0
diff --git a/libstdc++-v3/include/bits/alloc_traits.h b/libstdc++-v3/include/bits/alloc_traits.h
index 061d353e3f0..6066f48d24c 100644
--- a/libstdc++-v3/include/bits/alloc_traits.h
+++ b/libstdc++-v3/include/bits/alloc_traits.h
@@ -253,7 +253,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	_S_construct(_Alloc&, _Tp* __p, _Args&&... __args)
 	noexcept(noexcept(::new((void*)__p)
 			  _Tp(std::forward<_Args>(__args)...)))
-	{ std::_Construct(__p, std::forward<_Args>(__args)...); }
+	{
+#if __cplusplus <= 201703L
+	  ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);
+#else
+	  std::construct_at(__p, std::forward<_Args>(__args)...);
+#endif
+	}
 
       template<typename _Alloc2, typename _Tp>
 	static _GLIBCXX14_CONSTEXPR auto
diff --git a/libstdc++-v3/include/bits/stl_construct.h b/libstdc++-v3/include/bits/stl_construct.h
index 5c9a84d9497..72a7cb4712c 100644
--- a/libstdc++-v3/include/bits/stl_construct.h
+++ b/libstdc++-v3/include/bits/stl_construct.h
@@ -65,9 +65,9 @@
  * std::destroy_n, and the C++20 function std::construct_at.
  * It also provides std::_Construct, std::_Destroy,and std::_Destroy_n functions
  * which are defined in all standard modes and so can be used in C++98-14 code.
- * The _Construct and _Destroy functions will dispatch to construct_at and
- * destroy_at during constant evaluation, because calls to those functions are
- * intercepted by the compiler to allow use in constant expressions.
+ * The _Destroy functions will dispatch to destroy_at during constant
+ * evaluation, because calls to that function are intercepted by the compiler
+ * to allow use in constant expressions.
  */
 
 namespace std _GLIBCXX_VISIBILITY(default)
@@ -104,23 +104,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    */
 #if __cplusplus >= 201103L
   template<typename _Tp, typename... _Args>
-    constexpr _Tp*
+    inline void
     _Construct(_Tp* __p, _Args&&... __args)
-    {
-#if __cplusplus > 201703L
-      return std::construct_at(__p, std::forward<_Args>(__args)...);
-#else
-      return ::new(static_cast<void*>(__p)) _Tp(std::forward<_Args>(__args)...);
-#endif
-    }
+    { ::new(static_cast<void*>(__p)) _Tp(std::forward<_Args>(__args)...); }
 #else
   template<typename _T1, typename _T2>
-    inline _T1*
+    inline void
     _Construct(_T1* __p, const _T2& __value)
     {
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // 402. wrong new expression in [some_]allocator::construct
-      return ::new(static_cast<void*>(__p)) _T1(__value);
+      ::new(static_cast<void*>(__p)) _T1(__value);
     }
 #endif
 
diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct/94831.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct/94831.cc
new file mode 100644
index 00000000000..27506fae09f
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_value_construct/94831.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++17" }
+// { dg-do compile { target c++17 } }
+
+#include <memory>
+
+void
+test01()
+{
+  // PR libstdc++/94831
+  float i[2];
+  std::uninitialized_value_construct(&i, &i + 1);
+}
diff --git a/libstdc++-v3/testsuite/23_containers/vector/cons/94831.cc b/libstdc++-v3/testsuite/23_containers/vector/cons/94831.cc
new file mode 100644
index 00000000000..72dfcaae9af
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/vector/cons/94831.cc
@@ -0,0 +1,29 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do compile { target c++11 } }
+
+#include <vector>
+
+void
+test01()
+{
+  // PR libstdc++/94831.cc
+  // We allow this in C++11 and later as a GNU extension.
+  // FIXME: make it ill-formed when __STRICT_ANSI__ is defined.
+  std::vector<float[2]> v(1u);
+}


More information about the Libstdc++-cvs mailing list