diff options
author | Patrick Palka <ppalka@redhat.com> | 2023-11-10 10:58:04 -0500 |
---|---|---|
committer | Patrick Palka <ppalka@redhat.com> | 2023-11-10 10:58:04 -0500 |
commit | 705ab7927c81b77503d229513fac991106617766 (patch) | |
tree | 66c2ef60abeb65d30e3fba18c7aa768251ca4694 | |
parent | 5dbaf4851bbf56b6176dca1f1e7d38a16b5b84ee (diff) | |
download | gcc-705ab7927c81b77503d229513fac991106617766.zip gcc-705ab7927c81b77503d229513fac991106617766.tar.gz gcc-705ab7927c81b77503d229513fac991106617766.tar.bz2 |
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 <jason@redhat.com>
-rw-r--r-- | gcc/cp/semantics.cc | 22 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp1y/decltype-auto7.C | 53 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp1y/lambda-init20.C | 22 |
3 files changed, 96 insertions, 1 deletions
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 4059e74..f5ae8b5 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 0000000..a37b9db --- /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 0000000..a06b77a --- /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); + }; +} |