From patchwork Sat Apr 2 01:35:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 605379 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 3qcLR96sdDz9sRB for ; Sat, 2 Apr 2016 12:35:17 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=XXmEuGU9; 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 :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=D1WiD5/k41BMElSrl igUJLABBvfZdSDXqqcSagCnHLOXyawIxXa/cYL7eOdvSFNc3oycHc86I4GKyv9GT 6cIfQHel0OzW4d0W9hw6t30aRNhXh7jo+EUWcMZYy6InwlELozCsHmbJbGJAJeVN KTdOnGOyQ+Gp70I8a2AAMg87zI= 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 :subject:to:references:cc:from:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=Cjx5MUpSXCR4zU/HtM1vQ1t kcz0=; b=XXmEuGU9J/se2nj1MSWhYnUGLrz4qUzMUmb3Mrv93PQ3NgQhwuOY8VS R6Asx332jAkUw35K7Nq4jsDI+gvlnBFzar07bdOE+p2Y7huFT7jhA+0CqKreF1iw RXqmtodVom6zakNdH8SbUp3aaIVpfkU8FQCcZaWclfKJ0MW0SY2E= Received: (qmail 41657 invoked by alias); 2 Apr 2016 01:35:09 -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 41635 invoked by uid 89); 2 Apr 2016 01:35:08 -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; Sat, 02 Apr 2016 01:35:07 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (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 4B92C6438E for ; Sat, 2 Apr 2016 01:35:06 +0000 (UTC) Received: from [10.10.116.43] (ovpn-116-43.rdu2.redhat.com [10.10.116.43]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u321Z5pQ006680; Fri, 1 Apr 2016 21:35:05 -0400 Subject: Re: [C++ PATCH] Reject self-recursive constexpr calls even in templates (PR c++/70449) To: Jakub Jelinek References: <20160401191941.GG3017@tucnak.redhat.com> <56FF21B2.7060107@redhat.com> Cc: gcc-patches@gcc.gnu.org From: Jason Merrill Message-ID: <56FF21C9.4050003@redhat.com> Date: Fri, 1 Apr 2016 21:35:05 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 MIME-Version: 1.0 In-Reply-To: <56FF21B2.7060107@redhat.com> On 04/01/2016 09:34 PM, Jason Merrill wrote: > On 04/01/2016 03:19 PM, Jakub Jelinek wrote: >> 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. > > That's odd, we should have failed to instantiate the template. > Investigating further, it seems that we can check for DECL_INITIAL == > error_mark_node to tell that a function is still being defined. So this > patch does that, and also replaces my earlier fix for 70344. > > Tested x86_64-pc-linux-gnu, applying to trunk. > ...with the patch. commit 522aeea737412d552c78b58466280cf9e6c38924 Author: Jason Merrill Date: Fri Apr 1 20:30:09 2016 -0400 PR c++/70449 PR c++/70344 * pt.c (instantiate_decl): A function isn't fully defined if DECL_INITIAL is error_mark_node. * constexpr.c (cxx_eval_call_expression): Likewise. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index ea605dc..979f01f 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1239,21 +1239,6 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, return t; } - if (fun == current_function_decl) - { - /* A call to the current function, i.e. - 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; - } - constexpr_ctx new_ctx = *ctx; if (DECL_CONSTRUCTOR_P (fun) && !ctx->object && TREE_CODE (t) == AGGR_INIT_EXPR) @@ -1308,7 +1293,10 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, { if (!ctx->quiet) { - if (DECL_INITIAL (fun)) + if (DECL_INITIAL (fun) == error_mark_node) + error_at (loc, "%qD called in a constant expression before its " + "definition is complete", fun); + else if (DECL_INITIAL (fun)) { /* The definition of fun was somehow unsuitable. */ error_at (loc, "%qD called in a constant expression", fun); diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index a6398c0..2d93a5c 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -21741,7 +21741,8 @@ instantiate_decl (tree d, int defer_ok, if (TREE_CODE (d) == FUNCTION_DECL) { deleted_p = DECL_DELETED_FN (code_pattern); - pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE + pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE + && DECL_INITIAL (code_pattern) != error_mark_node) || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern) || deleted_p); } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-recursion1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-recursion1.C new file mode 100644 index 0000000..79e0b5a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-recursion1.C @@ -0,0 +1,16 @@ +// 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; +} + +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; +}