From patchwork Tue Jan 8 13:58:03 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Dodji Seketeli X-Patchwork-Id: 210391 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]) by ozlabs.org (Postfix) with SMTP id 634052C0095 for ; Wed, 9 Jan 2013 00:58:28 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1358258308; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:From:To:Cc:Subject:Date:Message-ID:User-Agent: MIME-Version:Content-Type:Content-Transfer-Encoding:Mailing-List: Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:Sender:Delivered-To; bh=GIXAFYI5T5SeJ5FvIUoOv/EmPIg=; b=WNtzS7KRf/6Y/zLPXiofa8W8sjTuvTkxnHADGRamphTKQRUeb0/h5ZJMsAbYUF XsbynYYc+orzxXw8zRw22YgltrTBJit9OaTOAFZ39utyLL6+fk3qu5sEAIM2/1pF N2+M6uB0rVHHdcS6C9VSd+FL0DI9Sty3n06AHcmmf66+U= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:From:To:Cc:Subject:X-URL:Date:Message-ID:User-Agent:MIME-Version:Content-Type:Content-Transfer-Encoding:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=yGYovVf/jk/zRWetCgYkBAwhzp4UxF2/NuNSylZx9t/bx0MD5v20W2NdozdO03 kImywkk8yPVjq2+E1fK7BFAnUjEHmpRh3G8jQu6nIySMY3GUS1RaWe39wqEe1+Tj TQ+ghLZAT+UN4wvpzlC1tW+eflHbC6Q4Nx0J7n6YWfdWg=; Received: (qmail 7526 invoked by alias); 8 Jan 2013 13:58:19 -0000 Received: (qmail 7512 invoked by uid 22791); 8 Jan 2013 13:58:16 -0000 X-SWARE-Spam-Status: No, hits=-6.1 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 08 Jan 2013 13:58:06 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r08Dw612025357 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 8 Jan 2013 08:58:06 -0500 Received: from localhost (ovpn-116-53.ams2.redhat.com [10.36.116.53]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r08Dw4Y8018431 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 8 Jan 2013 08:58:05 -0500 Received: by localhost (Postfix, from userid 1000) id 137D19F16C; Tue, 8 Jan 2013 14:58:04 +0100 (CET) From: Dodji Seketeli To: GCC Patches Cc: Jason Merrill Subject: [PATCH] PR c++/55663 - constexpr function templ instantiation considered non-const as alias templ arg X-URL: http://www.redhat.com Date: Tue, 08 Jan 2013 14:58:03 +0100 Message-ID: <877gnnwzdg.fsf@redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) MIME-Version: 1.0 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 Hello, Consider the example of the problem report 1 template 2 constexpr bool the_truth () { return true; } 3 4 template 5 struct Takes_bool { }; 6 7 template 8 using Alias = Takes_bool; 9 10 template 11 struct test { using type = Alias()>; }; 12 13 int main () { 14 test a; 15 16 return 0; 17 } that yields the error: test.cc: In substitution of ‘template using Alias = Takes_bool [with bool B = the_truth()]’: test.cc:11:51: required from ‘struct test’ test.cc:14:13: required from here test.cc:11:51: error: integral expression ‘the_truth()’ is not constant struct test { using type = Alias()>; }; I think the issue happens in the course of instantiating test at line 14, when we look into instantiating Alias()> (at line 11), with T = int. There, when we check the argument 'the_truth()' to see if it actually is a constant expression, in check_instantiated_arg, we fail to recognize its constexpr-ness b/c we just look at its TREE_CONSTANT. Would the patch below be ok-ish in testing for the const-ness of that argument in a general enough way that takes into account its constexpr-ness? Bootstapped and tested on x86_64-unknown-linux-gnu against trunk. gcc/cp/ PR c++/55663 * cp-tree.h (cxx_is_constant_expression): Declare ... * semantics.c (cxx_is_constant_expression): ... new function. * pt.c (check_instantiated_arg): Use the new cxx_is_constant_expression in lieu of TREE_CONSTANT. gcc/testsuite/ PR c++/55663 * g++.dg/cpp0x/alias-decl-31.C: New test. --- gcc/cp/cp-tree.h | 1 + gcc/cp/pt.c | 2 +- gcc/cp/semantics.c | 9 +++++++++ gcc/testsuite/g++.dg/cpp0x/alias-decl-31.C | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-31.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 810df7d..9d52ba7 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -5608,6 +5608,7 @@ extern bool potential_rvalue_constant_expression (tree); extern bool require_potential_constant_expression (tree); extern bool require_potential_rvalue_constant_expression (tree); extern tree cxx_constant_value (tree); +extern bool cxx_is_constant_expression (tree); extern tree maybe_constant_value (tree); extern tree maybe_constant_init (tree); extern bool is_sub_constant_expr (tree); diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 30bafa0..74ccfbf 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -14426,7 +14426,7 @@ check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain) constant. */ else if (TREE_TYPE (t) && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) - && !TREE_CONSTANT (t)) + && !cxx_is_constant_expression (t)) { if (complain & tf_error) error ("integral expression %qE is not constant", t); diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 2e02295..e40d48f 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -8077,6 +8077,15 @@ cxx_constant_value (tree t) return cxx_eval_outermost_constant_expr (t, false); } +/* Return TRUE iff E is a constant expression. */ + +bool +cxx_is_constant_expression (tree e) +{ + tree t = cxx_constant_value (e); + return (t != error_mark_node && t != NULL_TREE); +} + /* If T is a constant expression, returns its reduced value. Otherwise, if T does not have TREE_CONSTANT set, returns T. Otherwise, returns a version of T without TREE_CONSTANT. */ diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-31.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-31.C new file mode 100644 index 0000000..83eea47 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-31.C @@ -0,0 +1,20 @@ +// Origin: PR c++/55663 +// { dg-do compile { target c++11 } } + +template +constexpr bool the_truth () { return true; } + +template + struct Takes_bool { }; + +template + using Alias = Takes_bool; + +template + struct test { using type = Alias()>; }; + +int main () { + test a; + + return 0; +}