From patchwork Fri Apr 12 19:33:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1923248 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=KBTXtZ8R; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4VGRbd2cGdz1yYB for ; Sat, 13 Apr 2024 05:34:53 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A0651385843B for ; Fri, 12 Apr 2024 19:34:51 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 35FD73858C50 for ; Fri, 12 Apr 2024 19:34:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 35FD73858C50 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 35FD73858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712950472; cv=none; b=U5CiCB4XZtKGvBLzG7vYbAgIkVM/IvPrCh8HKT+nD9W+gxk0w9BSkBXI3lOkxMNSv+IC9VqWstMU065tmbJ254Gh6Kd7HKjbxVVSZyWlV+cEbf19Bub1Ghyi8JlXhH3mJUjuiDacWvNR1wCILos4Z//JxSuDxgApCGoEQJouO5U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712950472; c=relaxed/simple; bh=JwcN/4sPkYkCqZtTVEv8YIe3BgoNsIC6czzQ1lc3J24=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=GUziwGSFfmznpYpD5Ite9Buj9A4njNRYqhvuaQfc/0r19Nkt2lTnV1coRIr7PP2UNXOHYlb7opzcL/ni4LnL3E/SH8C98mrVCU3wpAaB4pT8lxCLVQODZ+IIDeXgSl6x+5efGL3eKjBWih2hqNNgQKrDhPluwiFfZR6E9TINzTM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712950469; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=VVAiEIFXQNjeMwAU4tPElydJ8bP3HWIYAEmQIYw78pA=; b=KBTXtZ8R/Oc+cIm+68cfZdLMG9qfDGjoL8ybchJNpuJMLrrR8aa0RXhcJgSR3V+2FdK8dl 192hr2lkgAMb2sWOtoH0dJLNCLFCgO2hGpU9bZtQhi1SCD+rhG9NCy+lX8pA3szqBnqCbd ZD8oElfREG94Bm7gt2CDfUmzjNTMGSg= Received: from mail-vk1-f200.google.com (mail-vk1-f200.google.com [209.85.221.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-692-vTBVzxQtPAm3U21tcJiuHw-1; Fri, 12 Apr 2024 15:34:27 -0400 X-MC-Unique: vTBVzxQtPAm3U21tcJiuHw-1 Received: by mail-vk1-f200.google.com with SMTP id 71dfb90a1353d-4daca01d01bso609900e0c.3 for ; Fri, 12 Apr 2024 12:34:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712950466; x=1713555266; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VVAiEIFXQNjeMwAU4tPElydJ8bP3HWIYAEmQIYw78pA=; b=kqeloDHoxRnvU7yMk4S90OQV3LhkgK3hiHFFBYH1KEnyQZ1Pl42os2gyzmmTq2F15L g0eFURIbK0uDCg3UN/1hxRyNyj3YiNiyvZo2EDx/N66FYoD7ryztiBF2q4eoji80G3l8 xpZlxi5/DlGn8v2KC8VAnXckaVK5YcjjnxGpMEjAjqFliFRz1W42rpAnon4FhKzbsk97 DTumtX6M6Fh9nqwK+2qAYCiPw5csgDzkBUYDqptnbmeHqE1HGwlQtZURzTCB4GMI7YzC 4l21YZLq1v/vNfAv71WBARJOqCAyGUm/dh4Tqpe/UkJoFRjW6a9vXOCBdpMpGxIzTJV6 wPyg== X-Gm-Message-State: AOJu0YzjZxp86N+i0i7aKPnkyNAUS8c+I0Yj44JaBgD44gcgnrPRwKRv Us+OdU/48bHeFnAP8t6cWL459mgorknOV9v+P969WUnPckWJehZJMjS5BLfVDrJm1fy5FIZEnXz ZKaHNZgTob9Q4RuGXUTK/rdltDfL5+pVKUSt3aik+j9T7giociL0MlEm/vwG6Ivo+SUXAkZKxPy EALYUlyLHPY15Eb20uxF5uuED+91y1ZK5x2lE= X-Received: by 2002:a05:6122:2507:b0:4cc:29cf:a1fa with SMTP id cl7-20020a056122250700b004cc29cfa1famr4161695vkb.4.1712950466646; Fri, 12 Apr 2024 12:34:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHyDulLoPeL/C/Ip9pgPQ5PT4CSBAIJEqDykAvu/ZNpgX0Ypp7guyZzVxhlGO5L3Vq5+jpbzw== X-Received: by 2002:a05:6122:2507:b0:4cc:29cf:a1fa with SMTP id cl7-20020a056122250700b004cc29cfa1famr4161661vkb.4.1712950466137; Fri, 12 Apr 2024 12:34:26 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id a5-20020a0562140c2500b0069b5150712csm2088961qvd.56.2024.04.12.12.34.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 12:34:25 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Cc: Patrick Palka Subject: [pushed] c++: reference cast, conversion fn [PR113141] Date: Fri, 12 Apr 2024 15:33:54 -0400 Message-ID: <20240412193424.2467514-1-jason@redhat.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.3 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, 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.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 Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- The second testcase in 113141 is a separate issue: we first decide that the conversion is ill-formed, but then when recalculating the special c_cast_p handling makes us think it's OK. We don't want that, it should continue to fall back to the reinterpret_cast interpretation. And while we're here, let's warn that we're not using the conversion function. Note that the standard seems to say that in this case we should treat (Matrix &) as const_cast(static_cast(X)), which would use the conversion operator, but that doesn't match existing practice, so let's resolve that another day. I've raised this issue with CWG; at the moment I lean toward never binding a temporary in a C-style cast to reference type, which would also be a change from existing practice. PR c++/113141 gcc/c-family/ChangeLog: * c.opt: Add -Wcast-user-defined. gcc/ChangeLog: * doc/invoke.texi: Document -Wcast-user-defined. gcc/cp/ChangeLog: * call.cc (reference_binding): For an invalid cast, warn and don't recalculate. gcc/testsuite/ChangeLog: * g++.dg/conversion/ref12.C: New test. Co-authored-by: Patrick Palka --- gcc/doc/invoke.texi | 13 +++++++++++++ gcc/c-family/c.opt | 4 ++++ gcc/cp/call.cc | 12 +++++++++++- gcc/testsuite/g++.dg/conversion/ref12.C | 20 ++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/conversion/ref12.C base-commit: 0fd824d717ca901319864a5eeba4e62b278f8329 diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 5d5e70c3033..e3285587e4e 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -9391,6 +9391,19 @@ In a cast involving pointer to member types this warning warns whenever the type cast is changing the pointer to member type. This warning is enabled by @option{-Wextra}. +@opindex Wcast-user-defined +@opindex Wno-cast-user-defined +@item -Wcast-user-defined +Warn when a cast to reference type does not involve a user-defined +conversion that the programmer might expect to be called. + +@smallexample +struct A @{ operator const int&(); @} a; +auto r = (int&)a; // warning +@end smallexample + +This warning is enabled by default. + @opindex Wwrite-strings @opindex Wno-write-strings @item -Wwrite-strings diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 56cccf2a67b..848c2fda203 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -514,6 +514,10 @@ Wcast-qual C ObjC C++ ObjC++ Var(warn_cast_qual) Warning Warn about casts which discard qualifiers. +Wcast-user-defined +C++ ObjC++ Var(warn_cast_user_defined) Warning Init(1) +Warn about a cast to reference type that does not use a related user-defined conversion function. + Wcatch-value C++ ObjC++ Warning Alias(Wcatch-value=, 1, 0) Warn about catch handlers of non-reference type. diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 15b5647298e..dbdd7c29fe8 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -2034,7 +2034,17 @@ reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags, recalculate the second conversion sequence. */ for (conversion *t = conv; t; t = next_conversion (t)) if (t->kind == ck_user - && DECL_CONV_FN_P (t->cand->fn)) + && c_cast_p && !maybe_valid_p) + { + if (complain & tf_warning) + warning (OPT_Wcast_user_defined, + "casting %qT to %qT does not use %qD", + from, rto, t->cand->fn); + /* Don't let recalculation try to make this valid. */ + break; + } + else if (t->kind == ck_user + && DECL_CONV_FN_P (t->cand->fn)) { tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn)); /* A prvalue of non-class type is cv-unqualified. */ diff --git a/gcc/testsuite/g++.dg/conversion/ref12.C b/gcc/testsuite/g++.dg/conversion/ref12.C new file mode 100644 index 00000000000..27ed9122769 --- /dev/null +++ b/gcc/testsuite/g++.dg/conversion/ref12.C @@ -0,0 +1,20 @@ +// PR c++/113141 + +struct Matrix { }; + +struct TPoint3 { private: operator const Matrix(); }; + +void f(Matrix&); + +int main() { + TPoint3 X; + Matrix& m = (Matrix &)X; // { dg-warning "does not use" } + f((Matrix &)X); // { dg-warning "does not use" } +} + +struct A { private: operator const int&(); } a; +int &r = (int&)a; // { dg-warning "does not use" } + +struct B { B(int); }; +int i; +B &br = (B&)i; // { dg-warning "does not use" }