From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 59576 invoked by alias); 5 Sep 2016 17:11:26 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 59565 invoked by uid 89); 5 Sep 2016 17:11:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.0 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=nnn, 2016-09-05 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 05 Sep 2016 17:11:24 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7DDC54E4CD for ; Mon, 5 Sep 2016 17:11:23 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-204-43.brq.redhat.com [10.40.204.43]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u85HBLaU027104 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Mon, 5 Sep 2016 13:11:22 -0400 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id u85HBKVn023121; Mon, 5 Sep 2016 19:11:20 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id u85HBJ7A023120; Mon, 5 Sep 2016 19:11:19 +0200 Date: Mon, 05 Sep 2016 17:14:00 -0000 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Fix constexpr switch handling (PR c++/77467) Message-ID: <20160905171119.GU14857@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-IsSubscribed: yes X-SW-Source: 2016-09/txt/msg00229.txt.bz2 Hi! cxx_eval_switch_expr assumes that SWITCH_EXPR's body is always a STATEMENT_LIST, but that doesn't have to be the case. As the testcase shows, if there are any variable declarations in the switch body, it can be also a BIND_EXPR, which cxx_eval_constant_expression handles properly, and as bar in the testcase shows, it can be also just a single statement (as try isn't allowed in constexpr functions, I think we just want to do what cxx_eval_statement_list would do on such a statement if it was wrapped into a STATEMENT_LIST - ignore it, as the case NNN: or default: is not present. Bootstrapped/regtested on x86_64-linux and i686-linux? What about older branches? 2016-09-05 Jakub Jelinek PR c++/77467 * constexpr.c (cxx_eval_switch_expr): Call cxx_eval_constant_expression instead of cxx_eval_statement_list, for body other than STATEMENT_LIST or BIND_EXPR don't evaluate the body at all. * g++.dg/cpp1y/constexpr-77467.C: New test. --- gcc/cp/constexpr.c.jj 2016-08-30 08:42:06.000000000 +0200 +++ gcc/cp/constexpr.c 2016-09-05 11:34:30.185518395 +0200 @@ -3572,8 +3572,12 @@ cxx_eval_switch_expr (const constexpr_ct *jump_target = cond; tree body = TREE_OPERAND (t, 1); - cxx_eval_statement_list (ctx, body, - non_constant_p, overflow_p, jump_target); + /* If body is a statement other than STATEMENT_LIST or BIND_EXPR, + it should be skipped. E.g. switch (a) b = a; */ + if (TREE_CODE (body) == STATEMENT_LIST + || TREE_CODE (body) == BIND_EXPR) + cxx_eval_constant_expression (ctx, body, false, + non_constant_p, overflow_p, jump_target); if (breaks (jump_target) || switches (jump_target)) *jump_target = NULL_TREE; return NULL_TREE; --- gcc/testsuite/g++.dg/cpp1y/constexpr-77467.C.jj 2016-09-05 11:19:30.593750642 +0200 +++ gcc/testsuite/g++.dg/cpp1y/constexpr-77467.C 2016-09-05 11:37:11.929477518 +0200 @@ -0,0 +1,33 @@ +// PR c++/77467 +// { dg-do compile { target c++14 } } + +constexpr int +foo (const int x, const unsigned n) noexcept +{ + switch (n) + { + case 0: + return 1; + case 1: + return x; + default: + const auto m = (n >> 1); + const auto y = foo (x, m); + return ((m << 1) == n) ? y * y : x * y * y; + } +} + +static_assert (foo (3, 2) == 9, ""); +static_assert (foo (2, 3) == 8, ""); + +constexpr int +bar (int x) +{ + int a = x; + switch (x) + a = x + 1; + return a; +} + +static_assert (bar (0) == 0, ""); +static_assert (bar (1) == 1, ""); Jakub