From patchwork Tue Feb 15 09:05:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Fran=C3=A7ois_Dumont?= X-Patchwork-Id: 51117 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3F1353858403 for ; Tue, 15 Feb 2022 09:06:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F1353858403 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1644916012; bh=OM1pwMaZ7Z6sP1KL7JxOeZpc2m0Aw4SrnlfVxbJog0k=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=SrcKSXaQnXsF34AY2XJaABWjONQmBV0JUlnQwQGEMUxyxAKDJtnVJHW6QM8kSMY6w b7qLlej7V1guyDpLBS/P65EkcFjr+iSsCRqrDtOa9HRXGqpOKRZzHlfS9JYROkdQcG pUMZuzrEE/K6f2fcYjSMAV2a/zwDSk1fH1c8RcYc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id 9CF803858D20; Tue, 15 Feb 2022 09:05:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9CF803858D20 Received: by mail-wm1-x32d.google.com with SMTP id k127-20020a1ca185000000b0037bc4be8713so970227wme.3; Tue, 15 Feb 2022 01:05:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:from:subject; bh=OM1pwMaZ7Z6sP1KL7JxOeZpc2m0Aw4SrnlfVxbJog0k=; b=ziu8SVCmYyZOsUajkvBmpbRcltTTNfa3LnPKhx+q8nPUrR9dM9FbViS0DOLiKjyME4 bZcrjR8N0VLXzCGqPJDqZMXgJ+BKD6hLkbTzoMH40Pb8ROTdHCOFi18PIOppMvGDY7RM HhZmfdd8f0lH+lBUXkJcO97Xl9ITKfW4buKsTbjVBtiuswVBU8eIn2/YVqq10ogOlGYb U4nWf7JV3WxA2Op4QPF9aXI30LO4pQ/H8O4t9+O31eSRdVz9zKZ+LhsmxEMeazBO+Nsf i3smiR5pn2F9ybd0ilzmvxhKx5d684YoyDKdpEboqq/cn0vIcL54rm5G1BXpp9aWyCbo UOlA== X-Gm-Message-State: AOAM533PUJ8nBX3JGyRkvfbyUO8He2ZfD8nXkemzAoFM5P4114D5ODOp HSWDr6opWjL2lMZ3Dbi3iAEfDWTeMMo= X-Google-Smtp-Source: ABdhPJwQRitA0yAz8Sjp7Zo3HeSisMPPYQpyJSs5hEDVBQ9+CQHv1LYEp1JHVKDWpAWR2wHsPfm7Hg== X-Received: by 2002:a7b:c8d4:0:b0:37c:32be:55b4 with SMTP id f20-20020a7bc8d4000000b0037c32be55b4mr2208032wml.53.1644915949243; Tue, 15 Feb 2022 01:05:49 -0800 (PST) Received: from ?IPV6:2a01:e0a:1dc:b1c0:15ac:685:187a:ad9b? ([2a01:e0a:1dc:b1c0:15ac:685:187a:ad9b]) by smtp.googlemail.com with ESMTPSA id h21sm6191168wmq.26.2022.02.15.01.05.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Feb 2022 01:05:48 -0800 (PST) Message-ID: <2fe3937d-1b9a-a547-bb41-225d3d5426a2@gmail.com> Date: Tue, 15 Feb 2022 10:05:47 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: en-US To: "libstdc++@gcc.gnu.org" Subject: [PATCH][_Hashtable] Fix insertion of range of type convertible to value_type PR 56112 X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, URIBL_SBL autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fran=C3=A7ois_Dumont_via_Gcc-patches?= From: =?utf-8?q?Fran=C3=A7ois_Dumont?= Reply-To: =?utf-8?q?Fran=C3=A7ois_Dumont?= Cc: gcc-patches Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" We have a regression regarding management of types convertible to value_type. It is an occurrence of PR 56112 but for the insert method.     libstdc++: [_Hashtable] Insert range of types convertible to value_type PR 56112     Fix insertion of range of types convertible to value_type.     libstdc++-v3/ChangeLog:             PR libstdc++/56112             * include/bits/hashtable.h             (_Hashtable<>::_M_insert_unique_aux): New.             (_Hashtable<>::_S_to_value): New.             (_Hashtable<>::_M_insert(_Arg&&, const _NodeGenerator&, true_type)): Use latters.             * testsuite/23_containers/unordered_map/cons/56112.cc: Use dg-do compile.             * testsuite/23_containers/unordered_set/cons/56112.cc: New test. Tested under Linux x86_64. Ok to commit ? François diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h index 5e1a417f7cd..5a502c02fe0 100644 --- a/libstdc++-v3/include/bits/hashtable.h +++ b/libstdc++-v3/include/bits/hashtable.h @@ -898,14 +898,51 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template std::pair - _M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen, - true_type /* __uks */) + _M_insert_unique_aux(_Arg&& __arg, const _NodeGenerator& __node_gen) { return _M_insert_unique( _S_forward_key(_ExtractKey{}(std::forward<_Arg>(__arg))), std::forward<_Arg>(__arg), __node_gen); } + template + static typename std::enable_if::value, + _Kt&&>::type + _S_to_value(_Kt&& __x) noexcept + { return std::forward<_Kt>(__x); } + + template + static typename std::enable_if::value, + const std::pair<_Kt, _Val>&>::type + _S_to_value(const std::pair<_Kt, _Val>& __x) noexcept + { return __x; } + + template + static typename std::enable_if::value, + std::pair<_Kt, _Val>&&>::type + _S_to_value(std::pair<_Kt, _Val>&& __x) noexcept + { return std::move(__x); } + + static value_type&& + _S_to_value(value_type&& __x) noexcept + { return std::move(__x); } + + static const value_type& + _S_to_value(const value_type& __x) noexcept + { return __x; } + + template + std::pair + _M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen, + true_type /* __uks */) + { + return _M_insert_unique_aux( + _S_to_value(std::forward<_Arg>(__arg)), __node_gen); + } + template iterator _M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen, diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/56112.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/56112.cc index c4cdeee234c..8ec0d89af69 100644 --- a/libstdc++-v3/testsuite/23_containers/unordered_map/cons/56112.cc +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/cons/56112.cc @@ -1,4 +1,4 @@ -// { dg-do run { target c++11 } } +// { dg-do compile { target c++11 } } // Copyright (C) 2013-2022 Free Software Foundation, Inc. // @@ -25,20 +25,23 @@ struct Key explicit Key(const int* p) : value(p) { } ~Key() { value = nullptr; } - bool operator==(const Key& k) const { return *value == *k.value; } + bool operator==(const Key& k) const + { return *value == *k.value; } const int* value; }; struct hash { - std::size_t operator()(const Key& k) const noexcept { return *k.value; } + std::size_t operator()(const Key& k) const noexcept + { return *k.value; } }; struct S { int value; - operator std::pair() const { return {Key(&value), value}; } + operator std::pair() const + { return {Key(&value), value}; } }; int main() @@ -46,4 +49,7 @@ int main() S s[1] = { {2} }; std::unordered_map m(s, s+1); std::unordered_multimap mm(s, s+1); + + m.insert(s, s + 1); + mm.insert(s, s + 1); } diff --git a/libstdc++-v3/testsuite/23_containers/unordered_set/cons/56112.cc b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/56112.cc new file mode 100644 index 00000000000..aa34ed4c603 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_set/cons/56112.cc @@ -0,0 +1,55 @@ +// { dg-do compile { target c++11 } } + +// Copyright (C) 2022 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 +// . + +#include +#include + +struct Key +{ + explicit Key(const int* p) : value(p) { } + ~Key() { value = nullptr; } + + bool operator==(const Key& k) const + { return *value == *k.value; } + + const int* value; +}; + +struct hash +{ + std::size_t operator()(const Key& k) const noexcept + { return *k.value; } +}; + +struct S +{ + int value; + operator Key() const + { return Key(&value); } +}; + +int main() +{ + S s[1] = { {2} }; + std::unordered_set m(s, s+1); + std::unordered_multiset mm(s, s+1); + + m.insert(s, s + 1); + mm.insert(s, s + 1); +}