diff options
author | Jason Merrill <jason@redhat.com> | 2022-04-14 08:16:45 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2022-04-14 14:51:49 -0400 |
commit | 74b2e20222cf4fb24b90561ddb6f0989738bb722 (patch) | |
tree | 4497f32f6a794c993200be3df9b3b86cc504e3a9 /gcc/cp/lambda.cc | |
parent | d634c5d7c78c6ec0fa39d96984460475564519c8 (diff) | |
download | gcc-74b2e20222cf4fb24b90561ddb6f0989738bb722.zip gcc-74b2e20222cf4fb24b90561ddb6f0989738bb722.tar.gz gcc-74b2e20222cf4fb24b90561ddb6f0989738bb722.tar.bz2 |
c++: lambda and the current instantiation [PR82980]
When a captured variable is type-dependent, we've expressed the type of the
capture field and proxy with a decltype variant. But if the type is "the
current instantiation", we need to be able to see that so that we can do
lookup inside it just like we could with the captured variable itself.
I also tried looking through lambda capture in
cp_parser_postfix_dot_deref_expression, but this way seems cleaner. I plan
to treat more types as deducible in stage 1.
I considered also using this in do_auto_deduction, but think that would be
wrong: [temp.dep.expr] says an id-expression is type-dependent if it is
"associated by name lookup with a variable declared with a type that
contains a placeholder type where the initializer is type-dependent". That
doesn't clearly exclude deducing a dependent type from the initializer, but
it seems like a barrier, and other implementations agree.
PR c++/82980
gcc/cp/ChangeLog:
* lambda.cc (type_deducible_expression_p): New.
(lambda_capture_field_type): Check it.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/lambda/lambda-current-inst1.C: New test.
Diffstat (limited to 'gcc/cp/lambda.cc')
-rw-r--r-- | gcc/cp/lambda.cc | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc index f22798d..65579ed 100644 --- a/gcc/cp/lambda.cc +++ b/gcc/cp/lambda.cc @@ -183,6 +183,24 @@ lambda_function (tree lambda) return lambda; } +/* True if EXPR is an expression whose type can be used directly in lambda + capture. Not to be used for 'auto'. */ + +static bool +type_deducible_expression_p (tree expr) +{ + if (!type_dependent_expression_p (expr)) + return true; + if (BRACE_ENCLOSED_INITIALIZER_P (expr) + || TREE_CODE (expr) == EXPR_PACK_EXPANSION) + return false; + tree t = non_reference (TREE_TYPE (expr)); + if (!t) return false; + while (TREE_CODE (t) == POINTER_TYPE) + t = TREE_TYPE (t); + return currently_open_class (t); +} + /* Returns the type to use for the FIELD_DECL corresponding to the capture of EXPR. EXPLICIT_INIT_P indicates whether this is a C++14 init capture, and BY_REFERENCE_P indicates whether we're @@ -211,7 +229,7 @@ lambda_capture_field_type (tree expr, bool explicit_init_p, else type = do_auto_deduction (type, expr, auto_node); } - else if (type_dependent_expression_p (expr)) + else if (!type_deducible_expression_p (expr)) { type = cxx_make_type (DECLTYPE_TYPE); DECLTYPE_TYPE_EXPR (type) = expr; |