From: Patrick Palka Date: Mon, 12 Jul 2021 20:35:18 +0000 (-0400) Subject: c++: permit deduction guides at class scope [PR79501] X-Git-Tag: basepoints/gcc-13~6053 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d75b8830e9dafb4e0c400c723653512adf40295;p=thirdparty%2Fgcc.git c++: permit deduction guides at class scope [PR79501] This adds support for declaring (class-scope) deduction guides for a member class template. Fortunately it seems only a couple of changes are needed in order for the existing CTAD machinery to handle them properly: we need to make sure to give them a FUNCTION_TYPE instead of a METHOD_TYPE, and we need to avoid using a BASELINK when looking them up. PR c++/79501 PR c++/100983 gcc/cp/ChangeLog: * decl.c (grokfndecl): Don't require that deduction guides are declared at namespace scope. Check that class-scope deduction guides have the same access as the member class template. (grokdeclarator): Pretend class-scope deduction guides are static. * search.c (lookup_member): Don't use a BASELINK for (class-scope) deduction guides. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/class-deduction92.C: New test. * g++.dg/cpp1z/class-deduction93.C: New test. * g++.dg/cpp1z/class-deduction94.C: New test. * g++.dg/cpp1z/class-deduction95.C: New test. --- diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 0df689b01f82..01d64a16125e 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -10042,12 +10042,6 @@ grokfndecl (tree ctype, if (deduction_guide_p (decl)) { - if (!DECL_NAMESPACE_SCOPE_P (decl)) - { - error_at (location, "deduction guide %qD must be declared at " - "namespace scope", decl); - return NULL_TREE; - } tree type = TREE_TYPE (DECL_NAME (decl)); if (in_namespace == NULL_TREE && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type)) @@ -10057,6 +10051,13 @@ grokfndecl (tree ctype, inform (location_of (type), " declared here"); return NULL_TREE; } + if (DECL_CLASS_SCOPE_P (decl) + && current_access_specifier != declared_access (TYPE_NAME (type))) + { + error_at (location, "deduction guide %qD must have the same access " + "as %qT", decl, type); + inform (location_of (type), " declared here"); + } if (funcdef_flag) error_at (location, "deduction guide %qD must not have a function body", decl); @@ -12037,6 +12038,10 @@ grokdeclarator (const cp_declarator *declarator, storage_class = declspecs->storage_class; if (storage_class == sc_static) staticp = 1 + (decl_context == FIELD); + else if (decl_context == FIELD && sfk == sfk_deduction_guide) + /* Treat class-scope deduction guides as static member functions + so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */ + staticp = 2; if (virtualp) { diff --git a/gcc/cp/search.c b/gcc/cp/search.c index 7b1836854767..af41bfe58352 100644 --- a/gcc/cp/search.c +++ b/gcc/cp/search.c @@ -1226,7 +1226,10 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type, rval = error_mark_node; } - if (rval && is_overloaded_fn (rval)) + if (rval && is_overloaded_fn (rval) + /* Don't use a BASELINK for class-scope deduction guides since + they're not actually member functions. */ + && !dguide_name_p (name)) rval = build_baselink (rval_binfo, basetype_path, rval, (IDENTIFIER_CONV_OP_P (name) ? TREE_TYPE (name): NULL_TREE)); diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction92.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction92.C new file mode 100644 index 000000000000..4920ca4f4a0e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction92.C @@ -0,0 +1,17 @@ +// PR c++/79501 +// { dg-do compile { target c++17 } } + +template +struct X { + template + struct B { T t; }; + + template B(T, decltype(V)=V) -> B; + + auto foo() { return B{V}; } +}; + +X<42> x; +using type = decltype(x.foo()); +using type = decltype(decltype(x)::B{42}); +using type = X<42>::B; diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction93.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction93.C new file mode 100644 index 000000000000..9d2db7a55a22 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction93.C @@ -0,0 +1,25 @@ +// PR c++/79501 +// { dg-do compile { target c++17 } } +// A variant of class-deduction78.C where List and its deduction guides are +// defined at class scope. + +using size_t = decltype(sizeof(42)); + +struct A { + template + struct List { + T head; + List tail; + }; + + template + struct List {}; + + template List(T) -> List; + template List(T, List) -> List; +}; + +int main() { + using type = decltype(A::List{0, A::List{1, A::List{2}}}); + using type = A::List; +} diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction94.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction94.C new file mode 100644 index 000000000000..f29ebd2c2b84 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction94.C @@ -0,0 +1,19 @@ +// PR c++/79501 +// { dg-do compile { target c++17 } } + +struct X { +protected: + template + struct B { T t; }; + + template B(T) -> B; +}; + +struct Y { +protected: + template + struct B { T t; }; + +private: + template B(T) -> B; // { dg-error "access" } +}; diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction95.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction95.C new file mode 100644 index 000000000000..05cbb2babfe1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction95.C @@ -0,0 +1,11 @@ +// PR c++/100983 +// { dg-do compile { target c++17 } } + +struct X { + template + struct Y { template Y(Ts...); }; + + template Y(Ts...) -> Y; +}; + +X::Y y{1,2,3};