From patchwork Mon Sep 6 04:41:58 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 63872 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 63679B70EF for ; Mon, 6 Sep 2010 14:42:11 +1000 (EST) Received: (qmail 20064 invoked by alias); 6 Sep 2010 04:42:08 -0000 Received: (qmail 20053 invoked by uid 22791); 6 Sep 2010 04:42:06 -0000 X-SWARE-Spam-Status: No, hits=-6.0 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, 06 Sep 2010 04:42:01 +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.13.8/8.13.8) with ESMTP id o864g0fp021312 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 6 Sep 2010 00:42:00 -0400 Received: from [127.0.0.1] ([10.3.113.19]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o864fxOL004053 for ; Mon, 6 Sep 2010 00:41:59 -0400 Message-ID: <4C847116.3000507@redhat.com> Date: Mon, 06 Sep 2010 00:41:58 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.12) Gecko/20100831 Lightning/1.0b1 Shredder/3.0.8pre MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH to implement SFINAE for non-constant template arguments 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 In certain cases, an expression that seems like an integer constant expression turns out not to have a constant value; in the deduction context such a template argument should cause deduction to fail, not give an error. Tested x86_64-pc-linux-gnu, applied to trunk. commit 6131aca4355f7d5c383c0f29f82bc60b466ffa1f Author: Jason Merrill Date: Mon Sep 6 00:14:05 2010 -0400 * pt.c (fold_non_dependent_expr_sfinae): Split out from... (fold_non_dependent_expr): ...here. (convert_nontype_argument): Use it. Take complain parm. Use perform_implicit_conversion instead of ocp_convert. Allow cv-qual changes. (convert_template_argument): Pass complain down. (tsubst_template_arg): Suppress constant expression warnings. Don't fold here. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 759e22f..eb47ac4 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -131,7 +131,7 @@ static int type_unification_real (tree, tree, tree, const tree *, unsigned int, int, unification_kind_t, int); static void note_template_header (int); static tree convert_nontype_argument_function (tree, tree); -static tree convert_nontype_argument (tree, tree); +static tree convert_nontype_argument (tree, tree, tsubst_flags_t); static tree convert_template_argument (tree, tree, tree, tsubst_flags_t, int, tree); static int for_each_template_parm (tree, tree_fn_t, void*, @@ -4800,8 +4800,8 @@ redeclare_class_template (tree type, tree parms) /* Simplify EXPR if it is a non-dependent expression. Returns the (possibly simplified) expression. */ -tree -fold_non_dependent_expr (tree expr) +static tree +fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain) { if (expr == NULL_TREE) return NULL_TREE; @@ -4823,7 +4823,7 @@ fold_non_dependent_expr (tree expr) processing_template_decl = 0; expr = tsubst_copy_and_build (expr, /*args=*/NULL_TREE, - tf_error, + complain, /*in_decl=*/NULL_TREE, /*function_p=*/false, /*integral_constant_expression_p=*/true); @@ -4832,6 +4832,12 @@ fold_non_dependent_expr (tree expr) return expr; } +tree +fold_non_dependent_expr (tree expr) +{ + return fold_non_dependent_expr_sfinae (expr, tf_error); +} + /* EXPR is an expression which is used in a constant-expression context. For instance, it could be a VAR_DECL with a constant initializer. Extract the innermost constant expression. @@ -4960,7 +4966,7 @@ has_value_dependent_address (tree op) hacks can go away after we fix the double coercion problem. */ static tree -convert_nontype_argument (tree type, tree expr) +convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) { tree expr_type; @@ -4969,11 +4975,13 @@ convert_nontype_argument (tree type, tree expr) catch this later), but only to provide better diagnostic for this common user mistake. As suggested by DR 100, we do not mention linkage issues in the diagnostic as this is not the point. */ + /* FIXME we're making this OK. */ if (TREE_CODE (expr) == STRING_CST) { - error ("%qE is not a valid template argument for type %qT " - "because string literals can never be used in this context", - expr, type); + if (complain & tf_error) + error ("%qE is not a valid template argument for type %qT " + "because string literals can never be used in this context", + expr, type); return NULL_TREE; } @@ -4992,8 +5000,8 @@ convert_nontype_argument (tree type, tree expr) if (TYPE_REF_OBJ_P (type) && has_value_dependent_address (expr)) /* If we want the address and it's value-dependent, don't fold. */; - else - expr = fold_non_dependent_expr (expr); + else if (!type_unknown_p (expr)) + expr = fold_non_dependent_expr_sfinae (expr, complain); if (error_operand_p (expr)) return error_mark_node; expr_type = TREE_TYPE (expr); @@ -5061,15 +5069,16 @@ convert_nontype_argument (tree type, tree expr) do not fold into integer constants. */ if (TREE_CODE (expr) != INTEGER_CST) { - error ("%qE is not a valid template argument for type %qT " - "because it is a non-constant expression", expr, type); + if (complain & tf_error) + error ("%qE is not a valid template argument for type %qT " + "because it is a non-constant expression", expr, type); return NULL_TREE; } /* At this point, an implicit conversion does what we want, because we already know that the expression is of integral type. */ - expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); + expr = perform_implicit_conversion (type, expr, complain); if (expr == error_mark_node) return error_mark_node; @@ -5319,7 +5328,8 @@ convert_nontype_argument (tree type, tree expr) /* Sanity check: did we actually convert the argument to the right type? */ - gcc_assert (same_type_p (type, TREE_TYPE (expr))); + gcc_assert (same_type_ignoring_top_level_qualifiers_p + (type, TREE_TYPE (expr))); return expr; } @@ -5784,7 +5794,7 @@ convert_template_argument (tree parm, conversions can occur is part of determining which function template to call, or whether a given explicit argument specification is valid. */ - val = convert_nontype_argument (t, orig_arg); + val = convert_nontype_argument (t, orig_arg, complain); else val = orig_arg; @@ -8270,9 +8280,12 @@ tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) r = tsubst (t, args, complain, in_decl); else { + if (!(complain & tf_warning)) + ++c_inhibit_evaluation_warnings; r = tsubst_expr (t, args, complain, in_decl, /*integral_constant_expression_p=*/true); - r = fold_non_dependent_expr (r); + if (!(complain & tf_warning)) + --c_inhibit_evaluation_warnings; } return r; } diff --git a/gcc/testsuite/g++.dg/template/char1.C b/gcc/testsuite/g++.dg/template/char1.C index 64ffda8..c440577 100644 --- a/gcc/testsuite/g++.dg/template/char1.C +++ b/gcc/testsuite/g++.dg/template/char1.C @@ -1,4 +1,4 @@ template class String {}; -String s; +String s; // { dg-warning "overflow" } diff --git a/gcc/testsuite/g++.dg/template/nontype10.C b/gcc/testsuite/g++.dg/template/nontype10.C index cd3a3aa..83ac8e3 100644 --- a/gcc/testsuite/g++.dg/template/nontype10.C +++ b/gcc/testsuite/g++.dg/template/nontype10.C @@ -5,6 +5,6 @@ template struct A {}; template struct B {}; -A a; +A a; // { dg-warning "NULL" } B b; // { dg-error "" } diff --git a/gcc/testsuite/g++.dg/template/sfinae25.C b/gcc/testsuite/g++.dg/template/sfinae25.C new file mode 100644 index 0000000..e9ee83e --- /dev/null +++ b/gcc/testsuite/g++.dg/template/sfinae25.C @@ -0,0 +1,14 @@ +template +struct A { }; + +template +void f(A<1/J>); + +template +void f(...) { } + +int main() +{ + f<0>(); +} +