From patchwork Wed Jun 12 20:29:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1947117 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=RQiKd9vW; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VzxzT3vjkz20Pb for ; Thu, 13 Jun 2024 06:32:05 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CD987388264B for ; Wed, 12 Jun 2024 20:32:03 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 073E53882169 for ; Wed, 12 Jun 2024 20:31:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 073E53882169 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 073E53882169 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718224296; cv=none; b=Yh9ZIhI/2JPSEgauenR5/a2KTvqZXLzPNr4a45mQLZPFIA4WLscWUVG79j/VJGg8bUDfdy6lTql5qGiUBNpmPhXijIp7Yo8phXaNeFR/5TbONfKVTd40YbdjVJ4tfEX7138zQf5KoKf3e790VSUHdKEgHJYsA6GkKekeQ8A2nks= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718224296; c=relaxed/simple; bh=tD740ZKPNhIvR1UgYd1yezu+F9wC/GW56xODResjyYI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=H+2BomwXDv5JXNYD2rmWA0brg/W6V7weWHl5Rb/df8aPFH+NwyC+uHAEYvRY8lU3FU4RlDdqBxxq+qf4PaoHoQRQgr65IeUInFyXlJkj15BElJkukBC7ye0W3uXjtSrAI1RSnUebX2rtWPBNq4C3PbgguUjTy3yX3kC8GOvGCC4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718224294; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=SVmN/UcvlWEFDsxW8fqovgHObySveKLWfpBXjq+jwYc=; b=RQiKd9vWD3srvQdJ6zh/BAIb8BHarGJr7PM4zwZKH+iJIbtqH/np/Gh8MpwnZ4Yft5w8kL 4KpeRRN3gxoMQhu3/qwPMmqNIekzicppdfd6/BgXQiizVjXENgk1OUQjBbA9yjL1/njvPD zmqBcVd/9iJMds2kXfpVLhoPqiPSFMM= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-465-TlB3Qcy8N9i2XCJ3RB41oA-1; Wed, 12 Jun 2024 16:31:31 -0400 X-MC-Unique: TlB3Qcy8N9i2XCJ3RB41oA-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4924418FF908; Wed, 12 Jun 2024 20:31:18 +0000 (UTC) Received: from localhost (unknown [10.39.192.128]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4EED319560AF; Wed, 12 Jun 2024 20:31:15 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Improve diagnostics for invalid std::hash specializations [PR115420] Date: Wed, 12 Jun 2024 21:29:18 +0100 Message-ID: <20240612203114.879303-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org This should be safe to test when the class is instantiated (unlike testing that the hash function is invocable, which we delay until it's needed). A disabled std::hash specialization is not copy constructible, and std::hash for a forward-declared std::string is disabled, so this greatly improves the error in the PR. I think it should be an improvement more generally too. Tested x86_64-linux. -- >8 -- When using a key type without a valid std::hash specialization the unordered containers give confusing diagnostics about the default constructor being deleted. Add a static_assert that will fail for disabled std::hash specializations (and for a subset of custom hash functions). libstdc++-v3/ChangeLog: PR libstdc++/115420 * include/bits/hashtable.h (_Hashtable): Add static_assert to check that hash function is copy constructible. * testsuite/23_containers/unordered_map/115420.cc: New test. --- libstdc++-v3/include/bits/hashtable.h | 2 ++ .../23_containers/unordered_map/115420.cc | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 libstdc++-v3/testsuite/23_containers/unordered_map/115420.cc diff --git a/libstdc++-v3/include/bits/hashtable.h b/libstdc++-v3/include/bits/hashtable.h index 983aa909d6c..361da2b3b4d 100644 --- a/libstdc++-v3/include/bits/hashtable.h +++ b/libstdc++-v3/include/bits/hashtable.h @@ -210,6 +210,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(is_same{}, "unordered container must have the same value_type as its allocator"); #endif + static_assert(is_copy_constructible<_Hash>::value, + "hash function must be copy constructible"); using __traits_type = _Traits; using __hash_cached = typename __traits_type::__hash_cached; diff --git a/libstdc++-v3/testsuite/23_containers/unordered_map/115420.cc b/libstdc++-v3/testsuite/23_containers/unordered_map/115420.cc new file mode 100644 index 00000000000..5528bf813bd --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/unordered_map/115420.cc @@ -0,0 +1,16 @@ +// { dg-do compile { target c++11 } } + +#include + +struct S { }; + +void +test_pr115420() +{ + std::unordered_map m; // { dg-error "here" } +} + +// { dg-error "hash function must be copy constructible" "" { target *-*-* } 0 } +// { dg-prune-output "use of deleted function" } +// { dg-prune-output "is private" } +// { dg-prune-output "no matching function" }