From patchwork Mon May 9 17:59:48 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 94833 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 62F19B6F1B for ; Tue, 10 May 2011 04:00:11 +1000 (EST) Received: (qmail 20714 invoked by alias); 9 May 2011 18:00:08 -0000 Received: (qmail 20702 invoked by uid 22791); 9 May 2011 18:00:06 -0000 X-SWARE-Spam-Status: No, hits=-6.3 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD 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; Mon, 09 May 2011 17:59:49 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p49Hxnb1008840 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 9 May 2011 13:59:49 -0400 Received: from [127.0.0.1] (ovpn-113-60.phx2.redhat.com [10.3.113.60]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p49Hxni9004419 for ; Mon, 9 May 2011 13:59:49 -0400 Message-ID: <4DC82B94.1040505@redhat.com> Date: Mon, 09 May 2011 13:59:48 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110421 Fedora/3.1.9-2.fc14 Lightning/1.0b2 Thunderbird/3.1.9 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/48936 (failure with static constant template variable in sizeof) 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 This issue had to do with us bailing out too soon when trying to mark_used a constant variable in an unevaluated context. I fixed this for 4.6 as part of the constexpr work, but a subset of that patch seems suitable for backporting. Tested x86_64-pc-linux-gnu, applying to 4.4 and 4.5. commit fc071e935530d226d24e08c25e5f66b0c61f0654 Author: Jason Merrill Date: Mon May 9 09:42:17 2011 -0400 PR c++/48936 * decl2.c (mark_used): Instantiate constant variables even in unevaluated context. diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index d27ed43..6c33434 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -3991,8 +3991,6 @@ possibly_inlined_p (tree decl) void mark_used (tree decl) { - HOST_WIDE_INT saved_processing_template_decl = 0; - /* If DECL is a BASELINK for a single function, then treat it just like the DECL for the function. Otherwise, if the BASELINK is for an overloaded function, we don't know which function was @@ -4029,9 +4027,6 @@ mark_used (tree decl) error ("used here"); return; } - /* If we don't need a value, then we don't need to synthesize DECL. */ - if (cp_unevaluated_operand != 0) - return; /* We can only check DECL_ODR_USED on variables or functions with DECL_LANG_SPECIFIC set, and these are also the only decls that we @@ -4059,9 +4054,10 @@ mark_used (tree decl) DECL. However, if DECL is a static data member initialized with a constant, we need the value right now because a reference to such a data member is not value-dependent. */ - if (TREE_CODE (decl) == VAR_DECL - && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) - && DECL_CLASS_SCOPE_P (decl)) + if (DECL_INTEGRAL_CONSTANT_VAR_P (decl) + && !DECL_INITIAL (decl) + && DECL_LANG_SPECIFIC (decl) + && DECL_TEMPLATE_INSTANTIATION (decl)) { /* Don't try to instantiate members of dependent types. We cannot just use dependent_type_p here because this function @@ -4071,12 +4067,14 @@ mark_used (tree decl) if (CLASSTYPE_TEMPLATE_INFO ((DECL_CONTEXT (decl))) && uses_template_parms (CLASSTYPE_TI_ARGS (DECL_CONTEXT (decl)))) return; - /* Pretend that we are not in a template, even if we are, so - that the static data member initializer will be processed. */ - saved_processing_template_decl = processing_template_decl; - processing_template_decl = 0; + instantiate_decl (decl, /*defer_ok=*/false, + /*expl_inst_class_mem_p=*/false); } + /* If we don't need a value, then we don't need to synthesize DECL. */ + if (cp_unevaluated_operand != 0) + return; + if (processing_template_decl) return; @@ -4149,8 +4147,6 @@ mark_used (tree decl) need. Therefore, we always try to defer instantiation. */ instantiate_decl (decl, /*defer_ok=*/true, /*expl_inst_class_mem_p=*/false); - - processing_template_decl = saved_processing_template_decl; } #include "gt-cp-decl2.h" diff --git a/gcc/testsuite/g++.dg/template/nontype23.C b/gcc/testsuite/g++.dg/template/nontype23.C new file mode 100644 index 0000000..dfda4fe --- /dev/null +++ b/gcc/testsuite/g++.dg/template/nontype23.C @@ -0,0 +1,9 @@ +// PR c++/48936 + +template int foo (void); +template struct S +{ + static const unsigned int a = sizeof (T); + enum { c = sizeof (foo <(a == 0)> ()) }; +}; +S x;