From b7beb16ac139b766a3b6adcae15ae5a358c0b83c Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Mon, 10 Oct 2016 16:48:51 -0400 Subject: [PATCH] C++17 class deduction issues PR c++/77890 PR c++/77912 * pt.c (do_class_deduction): Set cp_unevaluated_operand. (tsubst) [TEMPLATE_TYPE_PARM]: Copy CLASS_PLACEHOLDER_TEMPLATE. From-SVN: r240948 --- gcc/cp/ChangeLog | 7 +++++++ gcc/cp/pt.c | 15 ++++++++----- .../g++.dg/cpp1z/class-deduction19.C | 20 ++++++++++++++++++ .../g++.dg/cpp1z/class-deduction20.C | 21 +++++++++++++++++++ 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction19.C create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction20.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 02adc2b5bf8..ff0435e62ea 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2016-10-10 Jason Merrill + + PR c++/77890 + PR c++/77912 + * pt.c (do_class_deduction): Set cp_unevaluated_operand. + (tsubst) [TEMPLATE_TYPE_PARM]: Copy CLASS_PLACEHOLDER_TEMPLATE. + 2016-10-08 Jason Merrill * cp-gimplify.c (cp_fold): Add variable name. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index f6cd3ea47ba..28b1c987153 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -13233,11 +13233,15 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) TYPE_POINTER_TO (r) = NULL_TREE; TYPE_REFERENCE_TO (r) = NULL_TREE; - /* Propagate constraints on placeholders. */ if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) - if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t)) - PLACEHOLDER_TYPE_CONSTRAINTS (r) - = tsubst_constraint (constr, args, complain, in_decl); + { + /* Propagate constraints on placeholders. */ + if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t)) + PLACEHOLDER_TYPE_CONSTRAINTS (r) + = tsubst_constraint (constr, args, complain, in_decl); + else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t)) + CLASS_PLACEHOLDER_TEMPLATE (r) = pl; + } if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM) /* We have reduced the level of the template @@ -24431,9 +24435,10 @@ do_class_deduction (tree tmpl, tree init, tsubst_flags_t complain) return error_mark_node; } + ++cp_unevaluated_operand; tree t = build_new_function_call (cands, &args, /*koenig*/false, complain|tf_decltype); - + --cp_unevaluated_operand; release_tree_vector (args); return TREE_TYPE (t); diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction19.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction19.C new file mode 100644 index 00000000000..38327d1c6c3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction19.C @@ -0,0 +1,20 @@ +// PR c++/77912 +// { dg-options -std=c++1z } + +template struct S{S(T){}}; + +//error: invalid use of template type parameter 'S' +template auto f(T t){return S(t);} + +int main() +{ + //fails + f(42); + + //fails + //error: invalid use of template type parameter 'S' + [](auto a){return S(a);}(42); + + //works + [](int a){return S(a);}(42); +} diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction20.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction20.C new file mode 100644 index 00000000000..58e8f7dca76 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction20.C @@ -0,0 +1,21 @@ +// PR c++/77890 +// { dg-options -std=c++1z } + +template struct S{S(F&&f){}}; +void f() +{ + S([]{}); +} + +template +struct scope_guard : TF +{ + scope_guard(TF f) : TF{f} { } + ~scope_guard() { (*this)(); } +}; + +void g() +{ + struct K { void operator()() {} }; + scope_guard _{K{}}; +} -- 2.30.2