From cf7dae01734eea0dfb4c387e4cd40e1f9a682f56 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Fri, 18 Sep 2020 16:57:34 -0400 Subject: [PATCH] c++: CTAD and explicit deduction guides for copy-list-init [PR90210] This PR points out that we accept template struct tuple { tuple(T); }; // #1 template explicit tuple(T t) -> tuple; // #2 tuple t = { 1 }; despite the 'explicit' deduction guide in a copy-list-initialization context. That's because in deduction_guides_for we first find the user-defined deduction guide (#2), and then ctor_deduction_guides_for creates artificial deduction guides: one from the tuple(T) constructor and a copy guide. So we end up with these three guides: (1) template tuple(T) -> tuple [DECL_NONCONVERTING_P] (2) template tuple(tuple) -> tuple (3) template tuple(T) -> tuple Then, in do_class_deduction, we prune this set, and get rid of (1). Then overload resolution selects (3) and we succeed. But [over.match.list]p1 says "In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed." It also goes on to say that this differs from other situations where only converting constructors are considered for copy-initialization. Therefore for list-initialization we consider explicit constructors and complain if one is chosen. E.g. convert_like_internal/ck_user can give an error. So my logic runs that we should not prune the deduction_guides_for guides in a copy-list-initialization context, and only complain if we actually choose an explicit deduction guide. This matches clang++/EDG/msvc++. gcc/cp/ChangeLog: PR c++/90210 * pt.c (do_class_deduction): Don't prune explicit deduction guides in copy-list-initialization. In copy-list-initialization, if an explicit deduction guide was selected, give an error. gcc/testsuite/ChangeLog: PR c++/90210 * g++.dg/cpp1z/class-deduction73.C: New test. --- gcc/cp/pt.c | 49 ++++++++++++++----- .../g++.dg/cpp1z/class-deduction73.C | 41 ++++++++++++++++ 2 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction73.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 652b4583b35..869477f2c2e 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -28977,6 +28977,7 @@ do_class_deduction (tree ptype, tree tmpl, tree init, tree type = TREE_TYPE (tmpl); bool try_list_ctor = false; + bool list_init_p = false; releasing_vec rv_args = NULL; vec *&args = *&rv_args; @@ -28984,6 +28985,7 @@ do_class_deduction (tree ptype, tree tmpl, tree init, args = make_tree_vector (); else if (BRACE_ENCLOSED_INITIALIZER_P (init)) { + list_init_p = true; try_list_ctor = TYPE_HAS_LIST_CTOR (type); if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1) { @@ -29016,9 +29018,10 @@ do_class_deduction (tree ptype, tree tmpl, tree init, if (cands == error_mark_node) return error_mark_node; - /* Prune explicit deduction guides in copy-initialization context. */ + /* Prune explicit deduction guides in copy-initialization context (but + not copy-list-initialization). */ bool elided = false; - if (flags & LOOKUP_ONLYCONVERTING) + if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING)) { for (lkp_iterator iter (cands); !elided && iter; ++iter) if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter))) @@ -29087,18 +29090,42 @@ do_class_deduction (tree ptype, tree tmpl, tree init, --cp_unevaluated_operand; } - if (call == error_mark_node - && (complain & tf_warning_or_error)) + if (call == error_mark_node) { - error ("class template argument deduction failed:"); + if (complain & tf_warning_or_error) + { + error ("class template argument deduction failed:"); - ++cp_unevaluated_operand; - call = build_new_function_call (cands, &args, complain | tf_decltype); - --cp_unevaluated_operand; + ++cp_unevaluated_operand; + call = build_new_function_call (cands, &args, + complain | tf_decltype); + --cp_unevaluated_operand; - if (elided) - inform (input_location, "explicit deduction guides not considered " - "for copy-initialization"); + if (elided) + inform (input_location, "explicit deduction guides not considered " + "for copy-initialization"); + } + return error_mark_node; + } + /* [over.match.list]/1: In copy-list-initialization, if an explicit + constructor is chosen, the initialization is ill-formed. */ + else if (flags & LOOKUP_ONLYCONVERTING) + { + tree fndecl = cp_get_callee_fndecl_nofold (call); + if (fndecl && DECL_NONCONVERTING_P (fndecl)) + { + if (complain & tf_warning_or_error) + { + // TODO: Pass down location from cp_finish_decl. + error ("class template argument deduction for %qT failed: " + "explicit deduction guide selected in " + "copy-list-initialization", type); + inform (DECL_SOURCE_LOCATION (fndecl), + "explicit deduction guide declared here"); + + } + return error_mark_node; + } } /* If CTAD succeeded but the type doesn't have any explicit deduction diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction73.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction73.C new file mode 100644 index 00000000000..b37ddedd1bf --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction73.C @@ -0,0 +1,41 @@ +// PR c++/90210 +// { dg-do compile { target c++17 } } + +template struct tuple { tuple(T); }; +template explicit tuple(T t) -> tuple; +tuple t = { 1 }; // { dg-error "explicit deduction guide selected" } +tuple t1 = tuple{ 1 }; +tuple t2{ 1 }; + +template struct A { A(T, T); }; +template explicit A(T, T) -> A; +A a = {1, 1}; // { dg-error "explicit deduction guide selected" } +A a1 = A{1, 1}; +A a2{1, 1}; + +template +struct B { + B(T, U); +}; +template +B(T, U) -> B; // SFINAEd-out +B b = { 1, 2 }; // OK +B b1 = B{ 1, 2 }; // OK +B b2{ 1, 2 }; // OK + +// Overriden implicit default constructor deduction guide: +template +struct C { }; +explicit C() -> C; +C c = {}; // { dg-error "explicit deduction guide selected" } +C c1 = C{}; +C c2{}; + +// Overriden copy guide: +template +struct D { }; +template explicit D(D) -> D; +D d; +D d1 = {d}; // { dg-error "explicit deduction guide selected" } +D d2 = D{d}; +D d3{d}; -- 2.30.2