From patchwork Mon Sep 26 15:22:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1469 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp1548380wrt; Mon, 26 Sep 2022 08:24:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5oyOfBKzV4vCJWCNIZOabJ5n/77JYP7sL/FRKIA9qFSznPR0pHuUjWz9r8XlJGSpGiowCY X-Received: by 2002:a17:907:da9:b0:783:a3b4:2cff with SMTP id go41-20020a1709070da900b00783a3b42cffmr3569109ejc.51.1664205868307; Mon, 26 Sep 2022 08:24:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664205868; cv=none; d=google.com; s=arc-20160816; b=QSdMKIZBFgE05PINcmP/UTJewmlhAcmulj5jxUHaAeFBggZLFN47kvExScMpkbQucn GPcRORGnu5H48hVMJ6vOnRnXdAaI0Ac/KMNE6LOQNFENR+l36VdSWtGDKjnGVbAfh320 tse1DBQ4w7f3lMZvGvO9L/469ta/0WsZzwp0TzYkhKSjeZO3AmvAZG7J6ZKggYg71wCp Fwu8vsAh7MA+ce3nmAoKaT8/L1P4uX2X3b1HsaBE/mg2VXMRzf7AaoGTRI/sS39aC149 VA8+buHzGVZXw3i7ameVad/PW2JCsVO0nn5adNTmQW+siGjeHEVlIIS/+A/ht77HF0SC O02g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=H9DXccxLrbMN7Iu04szs6RtqsV337f2kmVxf1oh39b4=; b=kud/3YL8ZaKAlZjqawEGMVC5L47LTkDlbC5hl3U+qTfaMhSvRz90cIO1taLZ6X+UUt 1ac4JYKjCDiSIJasFSoUDajt2VsbCSYfpnvo+2kDNws6NhWQAtDhQbLrQu2aF9XtvaKv gxYCeyilvI0bTXMUgLiMZXPzyKiTrBREtOptUhjx0ysvA6++od2blE6eo+0RDXZdDOy1 vm6clxcT4B7dwAPLvFnDfzKrxYSoLTsSjhWoyxIoHCdT0FhsyieLreWkbD15xYrctja/ IXgSWsLat/Yi57aBiGQuvQ7LvbyNXn68ZBuzxzu7462inllkchONw5xrmr+k2f49MazB fXpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BEHxbn0g; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id bt10-20020a0564020a4a00b00448c8237904si14408922edb.73.2022.09.26.08.24.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Sep 2022 08:24:28 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BEHxbn0g; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0F77D3858290 for ; Mon, 26 Sep 2022 15:24:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0F77D3858290 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664205844; bh=H9DXccxLrbMN7Iu04szs6RtqsV337f2kmVxf1oh39b4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=BEHxbn0gYA7x3j2NsGHeirk4EUeTi4u4PUHo/hutCrHmGk5QH1JrbGhlke+CzfaCS OMBFlU78Adt+fXvnfsJHL2UtgZ+K5kAKHD0dsH2MlDt+K0KUzVgkxB66HWt2s06G7S ZAOnTIwl0tTXZl++wwajHwbLq+1DQBOzuMVc/neg= 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 E65013858C39 for ; Mon, 26 Sep 2022 15:23:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E65013858C39 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-387-23D8GWpiNG2vsMGHjuqQ3Q-1; Mon, 26 Sep 2022 11:23:07 -0400 X-MC-Unique: 23D8GWpiNG2vsMGHjuqQ3Q-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0362685A5B6 for ; Mon, 26 Sep 2022 15:23:07 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.9.111]) by smtp.corp.redhat.com (Postfix) with ESMTP id D1FFF2166B2C; Mon, 26 Sep 2022 15:23:06 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Instantiate less when evaluating __is_convertible Date: Mon, 26 Sep 2022 11:22:58 -0400 Message-Id: <20220926152258.20921-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745046332336713576?= X-GMAIL-MSGID: =?utf-8?q?1745046332336713576?= Jon reported that evaluating __is_convertible in this test leads to instantiating char_traits::eq, which is invalid (because we are trying to call a member function on a char) and so we fail to compile the test. __is_convertible doesn't and shouldn't need to instantiate so much, so let's limit it with a cp_unevaluated guard. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/106784 gcc/cp/ChangeLog: * method.cc (is_convertible): Use cp_unevaluated. gcc/testsuite/ChangeLog: * g++.dg/ext/is_convertible3.C: New test. --- gcc/cp/method.cc | 1 + gcc/testsuite/g++.dg/ext/is_convertible3.C | 66 ++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_convertible3.C base-commit: 2460f7cdef7ef9c971de79271afc0db73687a272 diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc index c35a59fe56c..45f70f5d3f3 100644 --- a/gcc/cp/method.cc +++ b/gcc/cp/method.cc @@ -2246,6 +2246,7 @@ is_convertible (tree from, tree to) { if (VOID_TYPE_P (from) && VOID_TYPE_P (to)) return true; + cp_unevaluated u; tree expr = build_stub_object (from); expr = perform_implicit_conversion (to, expr, tf_none); if (expr == error_mark_node) diff --git a/gcc/testsuite/g++.dg/ext/is_convertible3.C b/gcc/testsuite/g++.dg/ext/is_convertible3.C new file mode 100644 index 00000000000..c817dc6f146 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_convertible3.C @@ -0,0 +1,66 @@ +// PR c++/106784 +// { dg-do compile { target c++11 } } +// Make sure we don't reject this at runtime by trying to instantiate +// char_traits::eq(CharT, CharT) while evaluating __is_convertible. + +template +struct bool_constant { static constexpr bool value = B; }; +using true_type = bool_constant; +using false_type = bool_constant; + +template struct is_void : false_type { }; +template<> struct is_void : true_type { }; + +template T&& declval(); + +template struct enable_if { }; +template<> struct enable_if { using type = void; }; +template using enable_if_t = typename enable_if::type; + +template + struct is_convertible + : public bool_constant<__is_convertible(_From, _To)> + { }; + +template +struct char_traits +{ + static unsigned long length(const char* s) { eq(*s, *s); return 0; } + + static void eq(CharT l, CharT r) noexcept { l.f(r); } +}; + +template +struct basic_string_view +{ + using traits_type = char_traits; + + constexpr basic_string_view() = default; + constexpr basic_string_view(const basic_string_view&) = default; + + constexpr + basic_string_view(const CharT* __str) noexcept + : _M_len{traits_type::length(__str)} + { } + + unsigned long _M_len = 0; +}; + +template +struct basic_string +{ + template + enable_if_t>::value + && !is_convertible::value> + replace(int, T) { } + + void replace(unsigned long, const char*) { } + + void replace(const char* s) { replace(1, s); } +}; + +int main() +{ + basic_string s; + s.replace(""); +}