From patchwork Wed Mar 29 02:32:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1762530 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=mPX7KO10; dkim-atps=neutral Received: from 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PmVwn2kNMz1yYk for ; Wed, 29 Mar 2023 13:33:44 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 08FEC38582BD for ; Wed, 29 Mar 2023 02:33:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 08FEC38582BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680057222; bh=on6EJVT8JRdKCBbK6+vcjXgEic7lAUzwm3OxazHxFQ4=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=mPX7KO10uAJZZcpfPhiLIoHN1JjhSh+01/PrZeC6lh0szcw+ahc/q3If0zxCSA6Ea WCdyJMmPjCsHDRuh4ugBkWl/EHJ9cloAOITAgrcsQkok9hjsEtqsKo3wu/A8yxQFB4 x+Sa9JW16eZif5qlnSXDh+4tXDK44BgiJsWX3ZF8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by sourceware.org (Postfix) with ESMTPS id F28B33858C54 for ; Wed, 29 Mar 2023 02:33:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F28B33858C54 Received: by mail-pj1-x1032.google.com with SMTP id f6-20020a17090ac28600b0023b9bf9eb63so14688284pjt.5 for ; Tue, 28 Mar 2023 19:33:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680057200; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=on6EJVT8JRdKCBbK6+vcjXgEic7lAUzwm3OxazHxFQ4=; b=5Y3s7SxnHxig7KkEsjmvJ/cnag+pNqASmUH3EOJvGMWGBwnB6x1618keegNIrXg7vP e1vH3+121UHphuCCYi8mDiF9UzS7rnJAfuxDd6x1kUldTiOeD7ehVWhh7f/DbAlNa5Yt sh7ckB+hNuM6qdwTHptXWLMFGYMO1lcb1wujsGcsTdAfP0QNYERM2CzaStxAlBjN3d0V 3t3lIIMkTGq0MEBB4/+jpgmmhTcnMyPqZj6RegOSlIkwllgL6vvHav4VltDWJ+QptKwS 0W63YSGKm0CGnpX4FbAOb2qvFjS1hGHzclBcrVuSeholTd8k5lhLLezOYBjWyPp84cJ6 ydzg== X-Gm-Message-State: AO0yUKWGU3i/FXVE+cHC0I3q+6iYMYs3o1pZXV4B5ntLCDt34hDLYnYv WB59loPc/NEXIS79o8AWwRwfmd3pqzs= X-Google-Smtp-Source: AK7set/DLkWDzt2azpiKZnEKL+h9l8KzYptVlEvA2S2nAGs+XqgnbejLZCDr9Toqj6BUvmaVdOHRYQ== X-Received: by 2002:a05:6a20:6730:b0:db:df13:4f73 with SMTP id q48-20020a056a20673000b000dbdf134f73mr14656601pzh.26.1680057199737; Tue, 28 Mar 2023 19:33:19 -0700 (PDT) Received: from Thaum.localdomain (125-209-144-50.tpgi.com.au. [125.209.144.50]) by smtp.gmail.com with ESMTPSA id r1-20020a62e401000000b005a8ba70315bsm21811004pfh.6.2023.03.28.19.33.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 19:33:19 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Nathaniel Shead Subject: [PATCH v2 2/3] c++: Improve constexpr error for dangling local variables Date: Wed, 29 Mar 2023 13:32:57 +1100 Message-Id: <20230329023258.13487-3-nathanieloshead@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230329023258.13487-1-nathanieloshead@gmail.com> References: <20230329023258.13487-1-nathanieloshead@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: Nathaniel Shead via Gcc-patches From: Nathaniel Shead Reply-To: Nathaniel Shead Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Currently, when typeck discovers that a return statement will refer to a local variable it rewrites to return a null pointer. This causes the error messages for using the return value in a constant expression to be unhelpful, especially for reference return values. This patch removes this "optimisation". Relying on this raises a warning by default and causes UB anyway, so there should be no issue in doing so. We also suppress additional warnings from later passes that detect this as a dangling pointer, since we've already indicated this anyway. gcc/cp/ChangeLog: * semantics.cc (finish_return_stmt): Suppress dangling pointer reporting on return statement if already reported. * typeck.cc (check_return_expr): Don't set return expression to zero for dangling addresses. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-lifetime5.C: Test reported message is correct. * g++.dg/warn/Wreturn-local-addr-6.C: Remove check for return value optimisation. Signed-off-by: Nathaniel Shead --- gcc/cp/semantics.cc | 5 ++++- gcc/cp/typeck.cc | 5 +++-- gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C | 4 ++-- gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C | 3 --- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 87c2e8a7111..14b4b7f4ce1 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -1246,7 +1246,10 @@ finish_return_stmt (tree expr) r = build_stmt (input_location, RETURN_EXPR, expr); if (no_warning) - suppress_warning (r, OPT_Wreturn_type); + { + suppress_warning (r, OPT_Wreturn_type); + suppress_warning (r, OPT_Wdangling_pointer_); + } r = maybe_cleanup_point_expr_void (r); r = add_stmt (r); diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index afb956087ce..a7d642e2029 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -11235,8 +11235,9 @@ check_return_expr (tree retval, bool *no_warning) else if (!processing_template_decl && maybe_warn_about_returning_address_of_local (retval, loc) && INDIRECT_TYPE_P (valtype)) - retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval, - build_zero_cst (TREE_TYPE (retval))); + /* Suppress the Wdangling-pointer warning in the return statement + that would otherwise occur. */ + *no_warning = true; } if (processing_template_decl) diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C index a4bc71d890a..ad3ef579f63 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C @@ -1,11 +1,11 @@ // { dg-do compile { target c++14 } } // { dg-options "-Wno-return-local-addr" } -constexpr const int& id(int x) { return x; } +constexpr const int& id(int x) { return x; } // { dg-message "note: declared here" } constexpr bool test() { const int& y = id(3); return y == 3; } -constexpr bool x = test(); // { dg-error "" } +constexpr bool x = test(); // { dg-error "accessing object outside its lifetime" } diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C index fae8b7e766f..ec8e241d83e 100644 --- a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C +++ b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C @@ -24,6 +24,3 @@ return_addr_local_as_intref (void) return (const intptr_t&)a; // { dg-warning "\\\[-Wreturn-local-addr]" } */ } - -/* Verify that the return value has been replaced with zero: - { dg-final { scan-tree-dump-times "return 0;" 2 "optimized" } } */