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: 58045 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 3AB603857B8E for ; Mon, 26 Sep 2022 15:23:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3AB603857B8E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664205830; bh=H9DXccxLrbMN7Iu04szs6RtqsV337f2kmVxf1oh39b4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=OhRqvFlZ7GqSkqf7l9b1xkTiyPyeScjdNWKlOnWKji4DVYuyishvY78zv7K2lpEhe jTDJ3qX0d4Gz2s4NTRBh9q7Pwf9B73ByiBjV0zZ+3NGaTvABXORXO22t9AV4UCrDuR fxo+xJWSIbG7bsjyid2ilifEbb9qctMEtikyQXT8= 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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(""); +}