From patchwork Wed Oct 10 15:58:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 981971 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-487271-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="VCoe41Yi"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42Vf0q1h98z9sBk for ; Thu, 11 Oct 2018 02:59:04 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=YuUch2UcYqsaGMG+0NNHGaq1mmHS66IMChFBtQfTjkn/7bfTuovU3 S4s6ggJeFPgO13G8js32SS6FmVUnlocRZ5r43vttNsRRky0BWYJWksRaLlCKedFd IEKaWFFZ6MpuTyFNVNl3Itjo2Diu2klT1Udi5Y1Hzc/9YOI8mGhbnM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=y05U7GpoRkT6rdi+zRAkk4+wCT0=; b=VCoe41Yik134BQTYrkmD CtXoDXweEPQvDXfExvuIodr884xvwPvlUvUjRFSjmvWzjxLBj1KnW1rVffHahHo5 RLLr/BtGY4LFsipOUhyGBKxZ6Y4fInmgpI2kUIgdE/FgLWpmi38FXApyhoVbwUqS 1Zqaf3ga62oSc27OnUmRKfY= Received: (qmail 15049 invoked by alias); 10 Oct 2018 15:58:57 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 15031 invoked by uid 89); 10 Oct 2018 15:58:57 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=sk:constex, integer_zerop 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; Wed, 10 Oct 2018 15:58:54 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F0398356CE for ; Wed, 10 Oct 2018 15:58:52 +0000 (UTC) Received: from redhat.com (ovpn-120-116.rdu2.redhat.com [10.10.120.116]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 867056BC1C; Wed, 10 Oct 2018 15:58:52 +0000 (UTC) Date: Wed, 10 Oct 2018 11:58:50 -0400 From: Marek Polacek To: GCC Patches , Jason Merrill Subject: C++ PATCH for c++/87567, constexpr rejects call to non-constexpr function Message-ID: <20181010155850.GD19003@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) In this testcase, the call to f() can never be a constant expression, but that's not a problem because it is never reached. We handle a similar scenario for IF_STMT, so we can just do the same. The RANGE_FOR_STMT case seems to never be reached in the whole testsuite, so I didn't change that. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2018-10-10 Marek Polacek PR c++/87567 - constexpr rejects call to non-constexpr function. * constexpr.c (potential_constant_expression_1) : Return true if the condition is always false. : Likewise. * g++.dg/cpp1y/constexpr-loop7.C: New test. diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c index 403edda8c47..4fa8c965a9d 100644 --- gcc/cp/constexpr.c +++ gcc/cp/constexpr.c @@ -5818,8 +5818,16 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, case FOR_STMT: if (!RECUR (FOR_INIT_STMT (t), any)) return false; - if (!RECUR (FOR_COND (t), rval)) + tmp = FOR_COND (t); + if (!RECUR (tmp, rval)) return false; + if (tmp) + { + if (!processing_template_decl) + tmp = cxx_eval_outermost_constant_expr (tmp, true); + if (integer_zerop (tmp)) + return true; + } if (!RECUR (FOR_EXPR (t), any)) return false; if (!RECUR (FOR_BODY (t), any)) @@ -5840,8 +5848,13 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, return true; case WHILE_STMT: - if (!RECUR (WHILE_COND (t), rval)) + tmp = WHILE_COND (t); + if (!RECUR (tmp, rval)) return false; + if (!processing_template_decl) + tmp = cxx_eval_outermost_constant_expr (tmp, true); + if (integer_zerop (tmp)) + return true; if (!RECUR (WHILE_BODY (t), any)) return false; if (breaks (jump_target) || continues (jump_target)) diff --git gcc/testsuite/g++.dg/cpp1y/constexpr-loop7.C gcc/testsuite/g++.dg/cpp1y/constexpr-loop7.C index e69de29bb2d..6733820fee2 100644 --- gcc/testsuite/g++.dg/cpp1y/constexpr-loop7.C +++ gcc/testsuite/g++.dg/cpp1y/constexpr-loop7.C @@ -0,0 +1,21 @@ +// PR c++/87567 +// { dg-do compile { target c++14 } } + +constexpr bool always_false() { return false; } +int f() { return 1; } + +constexpr int +fn1 () +{ + while (always_false ()) + return f(); + return 0; +} + +constexpr int +fn2 () +{ + for (;always_false();) + return f(); + return 0; +}