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.133.124]) by sourceware.org (Postfix) with ESMTPS id D46D93889806 for ; Fri, 25 Mar 2022 18:48:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D46D93889806 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-448-KQzDXMzuM_i5zL0tgN_EYg-1; Fri, 25 Mar 2022 14:48:30 -0400 X-MC-Unique: KQzDXMzuM_i5zL0tgN_EYg-1 Received: by mail-qt1-f197.google.com with SMTP id h11-20020a05622a170b00b002e0769b9018so6715568qtk.14 for ; Fri, 25 Mar 2022 11:48:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=DaJ28G429nYrGnOlzhdwruxYBDOOsntBgI8N6HHQdyg=; b=Dl1awKsP+xUUAR9nNrDe/+EdRzf3TSt1jzGMAFbCjzdSKCnh43fzjcv7LsbrevLo93 WNX0DrQd7pBUhg6Z/ZqZCdcS6zjcsV1Gz/HckkakgfKztO+YDT/1MRqaH0Q26e50PYOl UNloY8wrjnN7/+BbWUiAMfP6YzgdJ4a28WrAWB3drtLiTciRJp3vJZpiep237Tgdjbeb xOX0s2FvpDj4hF9L+FoJjiJz3KFnnGkPe6kgEUy1/tf90sam7Tps5xZGioe4qDglDWj2 xvxl8dXiJM7gI2ui3B+1lZjhzyKKMc/3XRm+rykdv5UlDEeAMpUWA+eaZLQ6YdKKQUm5 kaAw== X-Gm-Message-State: AOAM532V9LNGcH1HwrOcWKxIzGXoCJHbLz98j+B6GvGUzzMmpgmuxwyj s0xn/wPYcFyEbA/gOvN0c+ttDRXi2U4UG+cYxPIhyQbm+u/Iws7WP5jemaODCKrxf8+rqy7bgMX w1JCAs+whXaWLUj9U9A== X-Received: by 2002:ac8:57ca:0:b0:2e2:131b:6f0e with SMTP id w10-20020ac857ca000000b002e2131b6f0emr10689103qta.664.1648234109894; Fri, 25 Mar 2022 11:48:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxswLF/g0GxswX7ekm7/fAVcIhEPsccfAoNhaOu/M7cY80JrdSRfKkiR5BfD9z77Kt5jmg+kg== X-Received: by 2002:ac8:57ca:0:b0:2e2:131b:6f0e with SMTP id w10-20020ac857ca000000b002e2131b6f0emr10689087qta.664.1648234109558; Fri, 25 Mar 2022 11:48:29 -0700 (PDT) Received: from [192.168.1.149] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id b21-20020a05620a04f500b0067b4895472esm3730820qkh.5.2022.03.25.11.48.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 25 Mar 2022 11:48:28 -0700 (PDT) Message-ID: <28fae23a-67fa-b7ef-eb71-143a68cb517b@redhat.com> Date: Fri, 25 Mar 2022 14:48:27 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH] c++: diagnosing if-stmt with non-constant branches [PR105050] To: Patrick Palka , gcc-patches@gcc.gnu.org Cc: jwakely@redhat.com References: <20220325160731.2535506-1-ppalka@redhat.com> From: Jason Merrill In-Reply-To: <20220325160731.2535506-1-ppalka@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Fri, 25 Mar 2022 18:48:35 -0000 On 3/25/22 12:07, Patrick Palka wrote: > When an if-stmt is deemed non-constant because both of its branches are > non-constant, we issue a rather generic error which, given that it points > to the 'if' token, misleadingly suggests the condition is at fault: > > constexpr-105050.C:8:3: error: expression ‘’ is not a constant expression > 8 | if (p != q && *p < 0) > | ^~ > > This patch clarifies the error message to read: > > constexpr-105050.C:8:3: error: neither branch of ‘if’ is a constant expression > 8 | if (p != q && *p < 0) > | ^~ > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? OK. I wonder if we want to then diagnose the branches individually, but let's leave that for stage 1, if at all. > PR c++/105050 > > gcc/cp/ChangeLog: > > * constexpr.cc (potential_constant_expression_1) : > Clarify error message when a if-stmt is non-constant because its > branches are non-constant. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp1y/constexpr-105050.C: New test. > --- > gcc/cp/constexpr.cc | 7 ++++++- > gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C | 12 ++++++++++++ > 2 files changed, 18 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C > > diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc > index 778680b8270..9c40b051574 100644 > --- a/gcc/cp/constexpr.cc > +++ b/gcc/cp/constexpr.cc > @@ -9439,7 +9439,12 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, > } > } > if (flags & tf_error) > - error_at (loc, "expression %qE is not a constant expression", t); > + { > + if (TREE_CODE (t) == IF_STMT) > + error_at (loc, "neither branch of % is a constant expression"); > + else > + error_at (loc, "expression %qE is not a constant expression", t); > + } > return false; > > case VEC_INIT_EXPR: > diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C > new file mode 100644 > index 00000000000..99d5c9960ac > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-105050.C > @@ -0,0 +1,12 @@ > +// PR c++/105050 > +// { dg-do compile { target c++14 } } > + > +void g(); > +void h(); > + > +constexpr void f(int* p, int *q) { > + if (p != q && *p < 0) // { dg-error "neither branch of 'if' is a constant expression" } > + g(); > + else > + h(); > +}