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 F1EEF3857034 for ; Fri, 25 Mar 2022 16:07:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F1EEF3857034 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-54-gBhU640fPr6rxjlHRd5T-w-1; Fri, 25 Mar 2022 12:07:44 -0400 X-MC-Unique: gBhU640fPr6rxjlHRd5T-w-1 Received: by mail-qk1-f200.google.com with SMTP id w200-20020a3762d1000000b0067d2149318dso5396814qkb.1 for ; Fri, 25 Mar 2022 09:07:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=xdq1KA8qPgvjUC1dZZ5x1mUAWYnxViBBTeXi88J/TwE=; b=b4ClfwboXFCVc/Iy83auOJAzwN8Oi9KvxC4YtN1BXFTTQAwVlfAwtm0UP5/pKZBcjN LwYIScukyGIVej6qAm/dACbwHgYrkbzxN7EybCzy80gEv7NTI7fbaFnUhXwXcP6/KcKD wx5LOH0DAabkxb9gLZZIf96/C1L+gUgHL3BxhoH4Sy9yO67GNPE0w52w6rFPJdVzL8n2 9Nx71HXKgnfIKZsKo0mMH8GaS0HAZFrXpuctj7dMZi0RZtGM7UGoHzULxOwC5yM1wm/Q V4g/68DgvLPpzXo0dZbCaZzNThFCVBt80Yp7CHrpHLE8VmEDul//+zZKPtXjdrTHn8Oy uJ/A== X-Gm-Message-State: AOAM53318Lw6+TzLeZguYzuy+LWs2Cmu/QlhoLJaXT1Z7FHWpIO5p1eR FiFFwA/TLEYbXm10f06vfjTzGNz3K4zys0E3jrxH6f/QtupB4pM7IjXWoZiQtk0qamrmWKP4Ym2 iLZkai4EIWTGSqL/MWchT8HH3jGEKoIBaPcMtP0/Y7wH0fPTwPQAe6fi/26FMi9mZIdY= X-Received: by 2002:a05:620a:4706:b0:67d:a135:c912 with SMTP id bs6-20020a05620a470600b0067da135c912mr7607739qkb.344.1648224463526; Fri, 25 Mar 2022 09:07:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwK0CJmIQssCytltys3X6uaN3Q/KoIS87f+f4BT4qu1Kcq8Z9tzgZOZzhOyFLOOB/ndkr6LVA== X-Received: by 2002:a05:620a:4706:b0:67d:a135:c912 with SMTP id bs6-20020a05620a470600b0067da135c912mr7607711qkb.344.1648224463196; Fri, 25 Mar 2022 09:07:43 -0700 (PDT) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id s31-20020a05622a1a9f00b002e1df010316sm5921263qtc.80.2022.03.25.09.07.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 09:07:42 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, jwakely@redhat.com, Patrick Palka Subject: [PATCH] c++: diagnosing if-stmt with non-constant branches [PR105050] Date: Fri, 25 Mar 2022 12:07:31 -0400 Message-Id: <20220325160731.2535506-1-ppalka@redhat.com> X-Mailer: git-send-email 2.35.1.655.ga68dfadae5 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.6 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_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 16:07:48 -0000 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? 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(); +} -- 2.35.1.655.ga68dfadae5