From patchwork Thu Jul 20 09:36:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 123135 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2996787vqt; Thu, 20 Jul 2023 02:37:21 -0700 (PDT) X-Google-Smtp-Source: APBJJlH3pjSZgrKQg8BkEH8qCmBbbNZrIWiIAAZBTFHVXVcfc3ImBsct9V/+7SVmF4RYijkKcHhJ X-Received: by 2002:a17:907:8a28:b0:994:4ebe:7a6d with SMTP id sc40-20020a1709078a2800b009944ebe7a6dmr6716088ejc.19.1689845840711; Thu, 20 Jul 2023 02:37:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689845840; cv=none; d=google.com; s=arc-20160816; b=Oir/ad0q8ag+bPMNu0ofieNF8BBlY2LA5GYHEzZ6773KuH1nWWgR3JdSLRucn++Buz BrAjVej7C+YHWi6s24QlDe2NsOqavjNSDbatOyJE4pnLT8cTn/sdwos6Ct7F9TLN+SSe pUcj3HTm3IUGjW33F/bCJMpS2c5vQYxnkjM9u6puzSwVu/hlO9XYYlV7BuceE40rnDg8 5+JNmTtIJ09vCj2vY4lXGz/9yBuE2U07zf2/ZYssl5rw6aRATrfhTGo67UFBTsFFgZYS X9vZh1jsgQG07f2kpzihL2OHmTE6PJPV0VEcSNG0GiCitkVV8epztrubxsD2SCKLb2pi pNwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:date:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Bq6aJPj+9YfVBBJ2AtCoXDKw5Ytq3HlCa8roVmHgO5w=; fh=O6SbEp1Lmu8iWDJwmE5AT4hXTvupzW71de0eJ/uUO/o=; b=SQAI56LiCNEVncsRlAxV82TqW0nzXOV4uWXleaAYhGtUpjXEKTyJNBut+wh2bsQEy1 I8xGr+iR2twidPnFgsKS6ahorYo4rpkJ2EoVI/qV1Yozhib25Z/bIIRMOqXyL1BOUgq6 gfm3pb4AHaetvOl3JAcZbPEV4I4FIlpRNPtjroBxxK6wkH477y9EZw0s4XfuAPHVdfBP lwdP228ETG8x9otLtFp0lvz0nlomg/G658NnPka6a1GfNpS9COw4wtKZ8QU0tj0wD1zv hE94zSnIau5339XNNcV6jmJA3xiyyDs6m4r4vzWDIYbymbl6fHU/6oUfu14xE1h0fyoe sI6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=SpNePCsA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y25-20020a17090668d900b0099b422f9c93si352380ejr.524.2023.07.20.02.37.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 02:37:20 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=SpNePCsA; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 C65313857022 for ; Thu, 20 Jul 2023 09:37:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C65313857022 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689845837; bh=Bq6aJPj+9YfVBBJ2AtCoXDKw5Ytq3HlCa8roVmHgO5w=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=SpNePCsAf8/I/af/SEa4rII6p/rnl83qF0Lbh1JPkdbmtaRyK5w3zovYTXgmSNRwt tQQBbXBxtj94qe7iF0P0sWNPeTMAT6yY8dnTlBZhS/cwEgCuf/DO5nhqBjinr3xY8X vJtsxHK6FuOknGOBo0lHxuqCWgP4I7KwfwjPpChQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id EB2A83858C33 for ; Thu, 20 Jul 2023 09:36:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB2A83858C33 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1b8b2886364so3288385ad.0 for ; Thu, 20 Jul 2023 02:36:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689845790; x=1690450590; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Bq6aJPj+9YfVBBJ2AtCoXDKw5Ytq3HlCa8roVmHgO5w=; b=e4xzd+vQjipY2EoCii3O4wiINZx43s+7ivsk5gY/L0ptMu9x9KBVycRFKYmLOpujyZ vl11iC7dnQAOpkNgaNEd3RbWkO1HoHg2fMh93gDWcoW/U9SgtSmfUuU7vPZ0Zr5j6rm0 YrU44xrJF5aqPB+VwMNnqa8cCsjOtDdmkPhFNnTlquZJA3ALAnOHzMRVlrMz2wZMBMZV 0btoXs5TgETe/1Xe5Ef9aAJFkKRLsvFUe3Ay56zmQ5HsuIkGkHCwZtUlLsjfonlKRVlS y2IYD2FXCckmAjyKzxxCAyZUSnLeWM/ssUYekYDhEvrNxhyZyjSLj/WUR7wDE57ub3LB Crjw== X-Gm-Message-State: ABy/qLa6gWb/KqLpQq3oyJMEfd7hnk83FPE3iLS6zJEMkcMmgmbI5y16 axEJHJ1RTr/40qzxiq8cqbwX/FhsCBk= X-Received: by 2002:a17:902:8548:b0:1b8:7e55:7a8c with SMTP id d8-20020a170902854800b001b87e557a8cmr4374716plo.56.1689845789918; Thu, 20 Jul 2023 02:36:29 -0700 (PDT) Received: from Thaum.localdomain (59-102-120-25.tpgi.com.au. [59.102.120.25]) by smtp.gmail.com with ESMTPSA id w13-20020a170902d70d00b001afd821c057sm845765ply.58.2023.07.20.02.36.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 02:36:29 -0700 (PDT) Date: Thu, 20 Jul 2023 19:36:24 +1000 To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Patrick Palka Subject: [PATCH v4 2/3] c++: Improve constexpr error for dangling local variables [PR110619] Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-11.6 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, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771931791994170684 X-GMAIL-MSGID: 1771931791994170684 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. PR c++/110619 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/cpp1y/constexpr-lifetime6.C: Likewise. * g++.dg/cpp1y/constexpr-110619.C: New test. * 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-110619.C | 10 ++++++++++ gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C | 4 ++-- gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C | 8 ++++---- gcc/testsuite/g++.dg/warn/Wreturn-local-addr-6.C | 3 --- 6 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 8fb47fd179e..107407de513 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -1260,7 +1260,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 859b133a18d..47233b3b717 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -11273,8 +11273,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; } /* A naive attempt to reduce the number of -Wdangling-reference false diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C new file mode 100644 index 00000000000..cca13302238 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C @@ -0,0 +1,10 @@ +// { dg-do compile { target c++14 } } +// { dg-options "-Wno-return-local-addr" } +// PR c++/110619 + +constexpr auto f() { + int i = 0; + return &i; +}; + +static_assert( f() != nullptr ); 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/cpp1y/constexpr-lifetime6.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C index f358aff4490..b81e89af79c 100644 --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C @@ -4,12 +4,12 @@ struct Empty {}; constexpr const Empty& empty() { - return Empty{}; + return Empty{}; // { dg-message "note: declared here" } } -constexpr const Empty& empty_parm(Empty e) { +constexpr const Empty& empty_parm(Empty e) { // { dg-message "note: declared here" } return e; } -constexpr Empty a = empty(); // { dg-error "" } -constexpr Empty b = empty_parm({}); // { dg-error "" } +constexpr Empty a = empty(); // { dg-error "accessing object outside its lifetime" } +constexpr Empty b = empty_parm({}); // { 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" } } */