From patchwork Tue Sep 17 21:16:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1986589 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=NOsJrG+A; 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 4X7ZSL2f7mz1y20 for ; Wed, 18 Sep 2024 07:20:18 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 28176385C6C9 for ; Tue, 17 Sep 2024 21:20:16 +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 ESMTP id 599AD385842C for ; Tue, 17 Sep 2024 21:19:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 599AD385842C 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 599AD385842C 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=1726607995; cv=none; b=cYUS3Cs1+ubIoAIj/GkHwGfgSyJtFPWCyCqFPpuS/raYGaWKOlaZTx9cyIqgiLNT+zCz4jUZ1HDDsyJbTwLBhZBjgL18Ggwl3slq6UxQJNoX7Yv+mT7eaKIOkwc6+V0Q7i++5cx/iO+zjikUrq+qpr4Glwx7ZOkHgxtniFc2fzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726607995; c=relaxed/simple; bh=2rtoOiFb5SSmraoAyce5MFPnJBbJE/MXG20DpRO3Zdw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=TDAPq/o9EhhKLq+td3T2po5HsNG5So26oHcNcXoTlviSOee/owF9vicFsUl7w8G2IL4PSpHtjiu7yh38XRV7F1wGjLdJw/08ba3lIgezfo52eHZk0h4o344y50Zg/ji53cxlR+b+MrK8d8Y/73ndhlHtMbvrCAo0C+c+LaKsb54= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726607993; 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=CH6r+SK5P1/0mBchwDLhNaDUKxDLtw+U9iZdCzCVlo0=; b=NOsJrG+AOg39LfpyHK7JhmNDzn+1/RqOimNG1qwCg2WpcJ/SElG4YCi0Afk8C0Fr0z2DUu gXxW5HRc4LAkSEf4zuEMOJvtWND6UILLJNrdFA1toRiX97Br9uRyns/zrqS0Y00s4iqGwd 1UZQwy9BdM0ODZZwYiMoMjDs0TBUBGc= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-176-k_kT1oJKNh6fmtSDOhKj6A-1; Tue, 17 Sep 2024 17:19:51 -0400 X-MC-Unique: k_kT1oJKNh6fmtSDOhKj6A-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-4582493b3f7so122478521cf.0 for ; Tue, 17 Sep 2024 14:19:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726607991; x=1727212791; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CH6r+SK5P1/0mBchwDLhNaDUKxDLtw+U9iZdCzCVlo0=; b=PGpILrm4qOuS0e/NqlEa7qhQ8udRVFkddwHbHBT6obnq9F+txp5cYYEhMgNfu1Q3HI 8MsnsIygCgp9VBetgJ7UvKgSw/HLrRbGa4AhJDQL70mPlIz7LBShaRWoAQhwF2nv1XGX npW8DToxcuVYAj2wmkvqmE9o0eEV7/3ZYYT5xH9gaMvNtPmIvyuhsnXx/SOozaB5iCdM 0isvN7NACx/v69TH0Q64Uls+pDZqqBg1SWQ1XCbtOnl4AlQQxh7+Cw9WFuIA/rsB19mh o7ktWm8Utc1zsK/8CVxPLA1Rqi3ZgoSddug+WuzDaILA9RybbmoyjzenjhvImnVWUDce QTTg== X-Gm-Message-State: AOJu0YzdqOg9WGhsK0VZmjXdhn33IGcAup+jak1vBWmj5UBVm/X9VpET /qPO26lolR+Y/XtdXKzlJ7tlh1vqHSp94DF8yt6AwlWsRTJt2zEnmQ/2DdG7TAB2ULy0E1gAygb by1jrqSVmxDqd+EXkB5+38pvFvi4rpM7TGvCsEpOk73x1Awo7Ob8OGArKTt9CuFVTkE6L1eePs8 pTpOtypRuIc7CBJn3WXVm0iPRRHwMs293X/Ac= X-Received: by 2002:a05:622a:5b92:b0:458:4457:8e69 with SMTP id d75a77b69052e-4599d23e4e6mr230685451cf.20.1726607990777; Tue, 17 Sep 2024 14:19:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFhGlO2gohbyjV8wiF/GjXsDFSoXRXk3S5C1sMGBp3u8hYIyoh3Zk2l/V6RMo3WYAr8VBBJkQ== X-Received: by 2002:a05:622a:5b92:b0:458:4457:8e69 with SMTP id d75a77b69052e-4599d23e4e6mr230685051cf.20.1726607990296; Tue, 17 Sep 2024 14:19:50 -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 d75a77b69052e-459aad07404sm42279211cf.61.2024.09.17.14.19.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2024 14:19:49 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org, Marek Polacek Subject: [PATCH 1/2] c++: -Wdangling-reference and empty class [PR115361] Date: Tue, 17 Sep 2024 17:16:47 -0400 Message-ID: <20240917211948.72205-1-jason@redhat.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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_H3, 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. Marek, any concerns? -- 8< -- We can't have a dangling reference to an empty class unless it's specifically to that class or one of its bases. This was giving a false positive on the _ExtractKey pattern in libstdc++ hashtable.h. This also adjusts the order of arguments to reference_related_p, which is relevant for empty classes (unlike scalars). Several of the classes in the testsuite needed to gain data members to continue to warn. PR c++/115361 gcc/cp/ChangeLog: * call.cc (do_warn_dangling_reference): Check is_empty_class. gcc/testsuite/ChangeLog: * g++.dg/ext/attr-no-dangling6.C: * g++.dg/ext/attr-no-dangling7.C: * g++.dg/ext/attr-no-dangling8.C: * g++.dg/ext/attr-no-dangling9.C: * g++.dg/warn/Wdangling-reference1.C: * g++.dg/warn/Wdangling-reference2.C: * g++.dg/warn/Wdangling-reference3.C: Make classes non-empty. * g++.dg/warn/Wdangling-reference23.C: New test. --- gcc/cp/call.cc | 12 +++++++----- gcc/testsuite/g++.dg/ext/attr-no-dangling6.C | 6 +++--- gcc/testsuite/g++.dg/ext/attr-no-dangling7.C | 6 +++--- gcc/testsuite/g++.dg/ext/attr-no-dangling8.C | 2 ++ gcc/testsuite/g++.dg/ext/attr-no-dangling9.C | 1 + gcc/testsuite/g++.dg/warn/Wdangling-reference1.C | 1 + gcc/testsuite/g++.dg/warn/Wdangling-reference2.C | 2 +- gcc/testsuite/g++.dg/warn/Wdangling-reference23.C | 14 ++++++++++++++ gcc/testsuite/g++.dg/warn/Wdangling-reference3.C | 1 + 9 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference23.C base-commit: a9f9391e1eeccb9d062b9e73ef27ac577b23ed64 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 664088eed9c..1ecf3aac705 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -14356,12 +14356,14 @@ do_warn_dangling_reference (tree expr, bool arg_p) if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true))) { /* If we know the temporary could not bind to the return type, - don't warn. This is for scalars only because for classes - we can't be sure we are not returning its sub-object. */ - if (SCALAR_TYPE_P (TREE_TYPE (arg)) + don't warn. This is for scalars and empty classes only + because for other classes we can't be sure we are not + returning its sub-object. */ + if ((SCALAR_TYPE_P (TREE_TYPE (arg)) + || is_empty_class (TREE_TYPE (arg))) && TYPE_REF_P (rettype) - && !reference_related_p (TREE_TYPE (arg), - TREE_TYPE (rettype))) + && !reference_related_p (TREE_TYPE (rettype), + TREE_TYPE (arg))) continue; return expr; } diff --git a/gcc/testsuite/g++.dg/ext/attr-no-dangling6.C b/gcc/testsuite/g++.dg/ext/attr-no-dangling6.C index 5b349e8e682..1fc426d20d3 100644 --- a/gcc/testsuite/g++.dg/ext/attr-no-dangling6.C +++ b/gcc/testsuite/g++.dg/ext/attr-no-dangling6.C @@ -2,9 +2,9 @@ // { dg-do compile { target c++20 } } // { dg-options "-Wdangling-reference" } -class X { }; -const X x1; -const X x2; +class X { int i; }; +const X x1 {}; +const X x2 {}; constexpr bool val () { return true; } struct ST { static constexpr bool value = true; }; diff --git a/gcc/testsuite/g++.dg/ext/attr-no-dangling7.C b/gcc/testsuite/g++.dg/ext/attr-no-dangling7.C index a5fb809e6bd..04c6badf0b6 100644 --- a/gcc/testsuite/g++.dg/ext/attr-no-dangling7.C +++ b/gcc/testsuite/g++.dg/ext/attr-no-dangling7.C @@ -2,9 +2,9 @@ // { dg-do compile { target c++20 } } // { dg-options "-Wdangling-reference" } -class X { }; -const X x1; -const X x2; +class X { int i; }; +const X x1 {}; +const X x2 {}; template [[gnu::no_dangling(N)]] const X& get(const int& i); // { dg-error "parameter packs not expanded" } diff --git a/gcc/testsuite/g++.dg/ext/attr-no-dangling8.C b/gcc/testsuite/g++.dg/ext/attr-no-dangling8.C index 8208d751a4b..aa196315a38 100644 --- a/gcc/testsuite/g++.dg/ext/attr-no-dangling8.C +++ b/gcc/testsuite/g++.dg/ext/attr-no-dangling8.C @@ -8,6 +8,7 @@ template constexpr bool is_reference_v = true; template struct [[gnu::no_dangling(is_reference_v)]] S { + int i; int &foo (const int &); }; @@ -15,6 +16,7 @@ template struct X { template struct [[gnu::no_dangling(is_reference_v && is_reference_v)]] Y { + int i; int &foo (const int &); }; }; diff --git a/gcc/testsuite/g++.dg/ext/attr-no-dangling9.C b/gcc/testsuite/g++.dg/ext/attr-no-dangling9.C index 65b4f7145a9..d7fd897de53 100644 --- a/gcc/testsuite/g++.dg/ext/attr-no-dangling9.C +++ b/gcc/testsuite/g++.dg/ext/attr-no-dangling9.C @@ -12,6 +12,7 @@ using true_type = bool_constant; using false_type = bool_constant; struct S { + int i; template [[gnu::no_dangling(B)]] int &foo (const int &); }; diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C index 1718c28165e..a184317dd5c 100644 --- a/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C @@ -131,6 +131,7 @@ int n = 1; const int& refmax = max(n - 1, n + 1); // { dg-warning "dangling reference" } struct Y { + int i; operator int&(); operator int&&(); const int& foo(const int&); diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference2.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference2.C index dafdb43f1b9..a3d5ad6d867 100644 --- a/gcc/testsuite/g++.dg/warn/Wdangling-reference2.C +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference2.C @@ -3,7 +3,7 @@ // { dg-options "-Wdangling-reference" } namespace std { -struct any {}; +struct any { void *p; ~any(); }; template _ValueType any_cast(any &&); template struct remove_reference { using type = _Tp; }; template _Tp forward(typename remove_reference<_Tp>::type); diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference23.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference23.C new file mode 100644 index 00000000000..e59ccc5057b --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference23.C @@ -0,0 +1,14 @@ +// PR c++/115361 +// { dg-additional-options -Wdangling-reference } + +struct B { int i; }; + +struct A { + const int & operator()(const B& b) { return b.i; } +}; + +int main() +{ + B b = {}; + const int &r = A()(b); +} diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference3.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference3.C index 4bc20c13b3f..7db1dc86855 100644 --- a/gcc/testsuite/g++.dg/warn/Wdangling-reference3.C +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference3.C @@ -18,6 +18,7 @@ struct G { }; struct F { + int i; G& f(); };