From patchwork Fri Apr 1 19:19:41 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 604994 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 3qcB683njFz9sD3 for ; Sat, 2 Apr 2016 06:19:59 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=R4bTHXk5; dkim-atps=neutral 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:cc:subject:message-id:reply-to:mime-version :content-type; q=dns; s=default; b=Fmq1y/qS68hzk3BVrjSpeuU1xM9CN IApAExhz7pNeRHYgbC3cdpDScJL7kIH5kSV4HHMHOubjboBbEFEo/vf5LtUuTgjr JWXQQ6sz+IRrOuQRiIXhspqUSiptLyc8gFdK4Ru8bx/LbucQdhgGIpMAp82C4EUS 1S0ioMIXxlTLAM= 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:cc:subject:message-id:reply-to:mime-version :content-type; s=default; bh=DGRs35z56B4QZG4yE4qJsFdExm0=; b=R4b THXk5WjekHjtOhbDFARCtwM5leY+Wn7Mbbm2YjdJk8XFmMu/hRGLH1dCEF7jPgRJ 9MTekCqUClt8tWsE+Zq6fXuSev0hMKfN42sBU24Xh8v5i512l/BF9XT/WSY5oICd RKsZdnrRgI7HXVQWorewhbqc9ntcGxVZYLmXSJnA= Received: (qmail 125270 invoked by alias); 1 Apr 2016 19:19:49 -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 125144 invoked by uid 89); 1 Apr 2016 19:19:48 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.9 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= 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 (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 01 Apr 2016 19:19:46 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (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 837D829AD0 for ; Fri, 1 Apr 2016 19:19:45 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-113-22.phx2.redhat.com [10.3.113.22]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u31JJhlk017318 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 1 Apr 2016 15:19:44 -0400 Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id u31JJgDX010787; Fri, 1 Apr 2016 21:19:42 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id u31JJfmn010786; Fri, 1 Apr 2016 21:19:41 +0200 Date: Fri, 1 Apr 2016 21:19:41 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Reject self-recursive constexpr calls even in templates (PR c++/70449) Message-ID: <20160401191941.GG3017@tucnak.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-IsSubscribed: yes Hi! As the testcase shows, when not in a template, cxx_eval_call_expression already complains about self-recursive calls in constexpr contexts, but if we are in a function template, we ICE on the testcase, because we try to instantiate the function template we are in the middle of parsing, e.g. function_end_locus is UNKNOWN_LOCATION, and only the statements that have been already parsed are in there. The patch attempts to reject that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2016-04-01 Jakub Jelinek PR c++/70449 * constexpr.c (cxx_eval_call_expression): Before calling instantiate_decl check if not trying to instantiate current function template and handle that the same as if fun == current_function_decl. * g++.dg/cpp1y/pr70449.C: New test. Jakub --- gcc/cp/constexpr.c.jj 2016-03-29 19:31:21.000000000 +0200 +++ gcc/cp/constexpr.c 2016-04-01 16:26:53.591088640 +0200 @@ -1293,6 +1293,27 @@ cxx_eval_call_expression (const constexp if (!DECL_INITIAL (fun) && DECL_TEMPLOID_INSTANTIATION (fun)) { + tree d = fun; + if (DECL_CLONED_FUNCTION_P (d)) + d = DECL_CLONED_FUNCTION (d); + d = template_for_substitution (d); + if (DECL_TEMPLATE_RESULT (d) == current_function_decl) + { + /* A call to the current function template, i.e. + template + constexpr int f (int i) { + constexpr int j = f(i-1); + return j; + } + This would be OK without the constexpr on the declaration + of j. */ + if (!ctx->quiet) + error_at (loc, "%qD called in a constant expression before its " + "definition is complete", fun); + *non_constant_p = true; + return t; + } + ++function_depth; instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); --function_depth; --- gcc/testsuite/g++.dg/cpp1y/pr70449.C.jj 2016-04-01 16:42:55.055190752 +0200 +++ gcc/testsuite/g++.dg/cpp1y/pr70449.C 2016-04-01 16:43:43.207545314 +0200 @@ -0,0 +1,26 @@ +// PR c++/70449 +// { dg-do compile { target c++14 } } +// { dg-options "-Wall" } + +template +constexpr int f1 () +{ + enum E { a = f1<0> () }; // { dg-error "called in a constant expression before its definition is complete|is not an integer constant" } + return 0; +} + +template +constexpr int f2 () +{ + enum E { a = f2<0> () }; + return 0; +} + +constexpr int f3 () +{ + enum E { a = f3 () }; // { dg-error "called in a constant expression before its definition is complete|is not an integer constant" } + return 0; +} + +constexpr int c = f1<0> (); +constexpr int d = f3 ();