From: Patrick Palka Date: Fri, 10 Nov 2023 15:58:04 +0000 (-0500) Subject: c++: decltype of capture proxy [PR79378, PR96917] X-Git-Tag: basepoints/gcc-15~4809 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=705ab7927c81b77503d229513fac991106617766;p=thirdparty%2Fgcc.git c++: decltype of capture proxy [PR79378, PR96917] We typically don't see capture proxies in finish_decltype_type because process_outer_var_ref is a no-op within an unevaluated context and so a use of a captured variable within decltype resolves to the captured variable, not the capture. But we can see them during decltype(auto) deduction and for decltype of an init-capture, which suggests we need to handle capture proxies specially within finish_decltype_type after all. This patch adds such handling. PR c++/79378 PR c++/96917 gcc/cp/ChangeLog: * semantics.cc (finish_decltype_type): Handle an id-expression naming a capture proxy specially. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/decltype-auto7.C: New test. * g++.dg/cpp1y/lambda-init20.C: New test. Reviewed-by: Jason Merrill --- diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 4059e74bdb79..f5ae8b5af85a 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -11643,8 +11643,28 @@ finish_decltype_type (tree expr, bool id_expression_or_member_access_p, /* Fall through for fields that aren't bitfields. */ gcc_fallthrough (); - case FUNCTION_DECL: case VAR_DECL: + if (is_capture_proxy (expr)) + { + if (is_normal_capture_proxy (expr)) + { + expr = DECL_CAPTURED_VARIABLE (expr); + type = TREE_TYPE (expr); + type = non_reference (type); + } + else + { + expr = DECL_VALUE_EXPR (expr); + gcc_assert (TREE_CODE (expr) == COMPONENT_REF); + expr = TREE_OPERAND (expr, 1); + type = TREE_TYPE (expr); + } + break; + } + /* Fall through for variables that aren't capture proxies. */ + gcc_fallthrough (); + + case FUNCTION_DECL: case CONST_DECL: case PARM_DECL: case RESULT_DECL: diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C new file mode 100644 index 000000000000..a37b9db38d4a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C @@ -0,0 +1,53 @@ +// PR c++/96917 +// { dg-do compile { target c++14 } } + +int main() { + int x = 0; + int y = 0; + const int cx = 0; + const int cy = 0; + + [x, &y, cx, &cy] { + decltype(auto) a = x; + using ty1 = int; + using ty1 = decltype(x); + using ty1 = decltype(a); + + decltype(auto) b = y; + using ty2 = int; + using ty2 = decltype(y); + using ty2 = decltype(b); + + decltype(auto) ca = cx; + using ty3 = const int; + using ty3 = decltype(cx); + using ty3 = decltype(ca); + + decltype(auto) cb = cy; + using ty4 = const int; + using ty4 = decltype(cy); + using ty4 = decltype(cb); + }; + + [x=x, &y=y, cx=cx, &cy=cy] { + decltype(auto) a = x; + using ty1 = int; + using ty1 = decltype(x); + using ty1 = decltype(a); + + decltype(auto) b = y; + using ty2 = int&; + using ty2 = decltype(y); + using ty2 = decltype(b); + + decltype(auto) ca = cx; + using ty3 = int; + using ty3 = decltype(cx); + using ty3 = decltype(ca); + + decltype(auto) cb = cy; + using ty4 = const int&; + using ty4 = decltype(cy); + using ty4 = decltype(cb); + }; +} diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-init20.C b/gcc/testsuite/g++.dg/cpp1y/lambda-init20.C new file mode 100644 index 000000000000..a06b77a664d9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/lambda-init20.C @@ -0,0 +1,22 @@ +// PR c++/79378 +// { dg-do compile { target c++14 } } + +int main() { + int x = 0; + [x=x, &r=x] { + using ty1 = int; + using ty1 = decltype(x); + + using ty2 = int&; + using ty2 = decltype(r); + }; + + const int cx = 0; + [x=cx, &r=cx] { + using ty1 = int; + using ty1 = decltype(x); + + using ty2 = const int&; + using ty2 = decltype(r); + }; +}