From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 6C4DA3858C2F for ; Fri, 23 Jun 2023 16:58:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6C4DA3858C2F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687539487; 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: in-reply-to:in-reply-to:references:references; bh=u71ncaShp8OHp5Qu3v/DN7Y8dlxcqlcSG05pi317hKw=; b=Lunflg63G2DML2ekczFIN0MKooB4U0wemg2u2ZPb12ZNoEOmyWreVgr1jaHo7qRI444HM0 /3wPLseMrHV2YwDg4K9/o3sGbtI+T0jVpQXNRgQi/KDhQ+/1MLzJvRX3BeT4zN38Ppe7hU Hr9HDzmN2Ry54pJXlXDXLpmDU7g4Eas= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-537-wxYpcFnbPCWbyH1ZEdHLzA-1; Fri, 23 Jun 2023 12:58:02 -0400 X-MC-Unique: wxYpcFnbPCWbyH1ZEdHLzA-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-763a36c3447so80850985a.3 for ; Fri, 23 Jun 2023 09:58:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687539481; x=1690131481; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=u71ncaShp8OHp5Qu3v/DN7Y8dlxcqlcSG05pi317hKw=; b=a8loj6mpj6e12LkLRRz3fxw2DLpulOC7bfynpBF7VadqdMP6AvOwofhAqeMBndSUzD rmpn5kdkzDltDuEvz+qvh3tGgMIpGLke41srlPp3iP3lxvZQGXG4MKiXJPgBtMrA5Po2 np9zO52BiQH9Q8rtMjJpjwFVweWIGAQjzQR928rs/IgpXLjd9XZ14eKjWNWKyCWY+U8i trHCjiDLtn2xIEe5bh5zZhoMhfjXTQikEGmiaYQDvEPH6GAKgZxJnIh4dBrduPwa8SFP wEZXj5qe93+bX3WFQc6ELck5W1XKjA+wcwJkVTu/0nuXP4Yh4jR8PxNFEJve0sowce2l BhyQ== X-Gm-Message-State: AC+VfDxPgUs5Hya6dAuXX6hpY45PuZkZzn3gSfD4o808WzSmCuQhIe9N V8iFPLf4uNkhjegOzRYEsy7EkKIPJefFOJtHpFnJCxDY+saSFauiHaMinblcjVR4XI46+wUPxGY jReqpJ28duWcU6YaB2mdE468yOw== X-Received: by 2002:a05:620a:6082:b0:764:33ca:c856 with SMTP id dx2-20020a05620a608200b0076433cac856mr7963502qkb.0.1687539481263; Fri, 23 Jun 2023 09:58:01 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7XVyAOvWhmOvsIeX7h+MrKfF3mznUO+tlliaM0j9DcRIpvfy//tigRFYu9+XydZC2VpmiA0A== X-Received: by 2002:a05:620a:6082:b0:764:33ca:c856 with SMTP id dx2-20020a05620a608200b0076433cac856mr7963493qkb.0.1687539481013; Fri, 23 Jun 2023 09:58:01 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id o21-20020a05620a131500b0075b35e72a21sm4722188qkj.86.2023.06.23.09.58.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 09:58:00 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Fri, 23 Jun 2023 12:57:59 -0400 (EDT) To: Nathaniel Shead cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH v2 2/3] c++: Improve constexpr error for dangling local variables In-Reply-To: <20230329023258.13487-3-nathanieloshead@gmail.com> Message-ID: References: <20230329023258.13487-1-nathanieloshead@gmail.com> <20230329023258.13487-3-nathanieloshead@gmail.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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 List-Id: On Wed, 29 Mar 2023, Nathaniel Shead via Gcc-patches wrote: > 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. LGTM. It seems the original motivation for returning a null pointer here was to avoid issuing duplicate warnings (https://gcc.gnu.org/legacy-ml/gcc-patches/2014-04/msg00269.html) which your patch addresses. > > 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" } } */ > -- > 2.34.1 > >