aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2018-08-13 15:46:42 +0000
committerMarek Polacek <mpolacek@gcc.gnu.org>2018-08-13 15:46:42 +0000
commite6a1e5fe7346458208ede97741382061a6191024 (patch)
treed5b2c43f0745495e0a950a70183cf0936c71db04 /gcc
parent27a98312c54b6c00a4e7deb1bf663547cd4f2dcc (diff)
downloadgcc-e6a1e5fe7346458208ede97741382061a6191024.zip
gcc-e6a1e5fe7346458208ede97741382061a6191024.tar.gz
gcc-e6a1e5fe7346458208ede97741382061a6191024.tar.bz2
P0806R2 - Deprecate implicit capture of this via [=]
P0806R2 - Deprecate implicit capture of this via [=] * lambda.c (add_default_capture): Formatting fixes. Warn about deprecated implicit capture of this via [=]. * g++.dg/cpp2a/lambda-this1.C: New test. * g++.dg/cpp2a/lambda-this2.C: New test. * g++.dg/cpp2a/lambda-this3.C: New test. From-SVN: r263508
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/ChangeLog4
-rw-r--r--gcc/cp/lambda.c19
-rw-r--r--gcc/testsuite/ChangeLog7
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/lambda-this1.C51
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/lambda-this2.C51
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/lambda-this3.C55
6 files changed, 182 insertions, 5 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 859085b..da6703b 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,5 +1,9 @@
2018-08-13 Marek Polacek <polacek@redhat.com>
+ P0806R2 - Deprecate implicit capture of this via [=]
+ * lambda.c (add_default_capture): Formatting fixes. Warn about
+ deprecated implicit capture of this via [=].
+
PR c++/86915
* decl.c (create_array_type_for_decl): Handle null name.
diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c
index 54fc3ee..25a4d6f 100644
--- a/gcc/cp/lambda.c
+++ b/gcc/cp/lambda.c
@@ -695,14 +695,10 @@ tree
add_default_capture (tree lambda_stack, tree id, tree initializer)
{
bool this_capture_p = (id == this_identifier);
-
tree var = NULL_TREE;
-
tree saved_class_type = current_class_type;
- tree node;
-
- for (node = lambda_stack;
+ for (tree node = lambda_stack;
node;
node = TREE_CHAIN (node))
{
@@ -720,6 +716,19 @@ add_default_capture (tree lambda_stack, tree id, tree initializer)
== CPLD_REFERENCE)),
/*explicit_init_p=*/false);
initializer = convert_from_reference (var);
+
+ /* Warn about deprecated implicit capture of this via [=]. */
+ if (cxx_dialect >= cxx2a
+ && this_capture_p
+ && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) == CPLD_COPY
+ && !in_system_header_at (LAMBDA_EXPR_LOCATION (lambda)))
+ {
+ if (warning_at (LAMBDA_EXPR_LOCATION (lambda), OPT_Wdeprecated,
+ "implicit capture of %qE via %<[=]%> is deprecated "
+ "in C++20", this_identifier))
+ inform (LAMBDA_EXPR_LOCATION (lambda), "add explicit %<this%> or "
+ "%<*this%> capture");
+ }
}
current_class_type = saved_class_type;
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index c11b5c8..d7ed84e 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,5 +1,12 @@
2018-08-13 Marek Polacek <polacek@redhat.com>
+ P0806R2 - Deprecate implicit capture of this via [=]
+ * g++.dg/cpp2a/lambda-this1.C: New test.
+ * g++.dg/cpp2a/lambda-this2.C: New test.
+ * g++.dg/cpp2a/lambda-this3.C: New test.
+
+2018-08-13 Marek Polacek <polacek@redhat.com>
+
PR c++/86915
* g++.dg/diagnostic/auto1.C: New test.
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-this1.C b/gcc/testsuite/g++.dg/cpp2a/lambda-this1.C
new file mode 100644
index 0000000..a31b968
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/lambda-this1.C
@@ -0,0 +1,51 @@
+// P0806R2
+// { dg-do compile }
+// { dg-options "-std=c++2a" }
+
+struct X {
+ int x;
+ void foo (int n) {
+ auto a1 = [=] { x = n; }; // { dg-warning "implicit capture" }
+ auto a2 = [=, this] { x = n; };
+ auto a3 = [=, *this]() mutable { x = n; };
+ auto a4 = [&] { x = n; };
+ auto a5 = [&, this] { x = n; };
+ auto a6 = [&, *this]() mutable { x = n; };
+
+ auto a7 = [=] { // { dg-warning "implicit capture" }
+ auto a = [=] { // { dg-warning "implicit capture" }
+ auto a2 = [=] { x = n; }; // { dg-warning "implicit capture" }
+ };
+ };
+
+ auto a8 = [=, this] {
+ auto a = [=, this] {
+ auto a2 = [=, this] { x = n; };
+ };
+ };
+
+ auto a9 = [=, *this]() mutable {
+ auto a = [=, *this]() mutable {
+ auto a2 = [=, *this]() mutable { x = n; };
+ };
+ };
+
+ auto a10 = [&] {
+ auto a = [&] {
+ auto a2 = [&] { x = n; };
+ };
+ };
+
+ auto a11 = [&, this] {
+ auto a = [&, this] {
+ auto a2 = [&, this] { x = n; };
+ };
+ };
+
+ auto a12 = [&, *this]() mutable {
+ auto a = [&, *this]() mutable {
+ auto a2 = [&, *this]() mutable { x = n; };
+ };
+ };
+ }
+};
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-this2.C b/gcc/testsuite/g++.dg/cpp2a/lambda-this2.C
new file mode 100644
index 0000000..307fb4c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/lambda-this2.C
@@ -0,0 +1,51 @@
+// P0806R2
+// { dg-do compile }
+// { dg-options "-std=c++2a -Wno-deprecated" }
+
+struct X {
+ int x;
+ void foo (int n) {
+ auto a1 = [=] { x = n; }; // { dg-bogus "implicit capture" }
+ auto a2 = [=, this] { x = n; };
+ auto a3 = [=, *this]() mutable { x = n; };
+ auto a4 = [&] { x = n; };
+ auto a5 = [&, this] { x = n; };
+ auto a6 = [&, *this]() mutable { x = n; };
+
+ auto a7 = [=] { // { dg-bogus "implicit capture" }
+ auto a = [=] { // { dg-bogus "implicit capture" }
+ auto a2 = [=] { x = n; }; // { dg-bogus "implicit capture" }
+ };
+ };
+
+ auto a8 = [=, this] {
+ auto a = [=, this] {
+ auto a2 = [=, this] { x = n; };
+ };
+ };
+
+ auto a9 = [=, *this]() mutable {
+ auto a = [=, *this]() mutable {
+ auto a2 = [=, *this]() mutable { x = n; };
+ };
+ };
+
+ auto a10 = [&] {
+ auto a = [&] {
+ auto a2 = [&] { x = n; };
+ };
+ };
+
+ auto a11 = [&, this] {
+ auto a = [&, this] {
+ auto a2 = [&, this] { x = n; };
+ };
+ };
+
+ auto a12 = [&, *this]() mutable {
+ auto a = [&, *this]() mutable {
+ auto a2 = [&, *this]() mutable { x = n; };
+ };
+ };
+ }
+};
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-this3.C b/gcc/testsuite/g++.dg/cpp2a/lambda-this3.C
new file mode 100644
index 0000000..5e5c8b3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/lambda-this3.C
@@ -0,0 +1,55 @@
+// P0806R2
+// { dg-do compile }
+// { dg-options "-std=c++17" }
+
+struct X {
+ int x;
+ void foo (int n) {
+ auto a1 = [=] { x = n; }; // { dg-bogus "implicit capture" }
+ auto a2 = [=, this] { x = n; };
+ // { dg-warning "explicit by-copy capture" "" { target c++17_down } .-1 }
+ auto a3 = [=, *this]() mutable { x = n; };
+ auto a4 = [&] { x = n; };
+ auto a5 = [&, this] { x = n; };
+ auto a6 = [&, *this]() mutable { x = n; };
+
+ auto a7 = [=] { // { dg-bogus "implicit capture" }
+ auto a = [=] { // { dg-bogus "implicit capture" }
+ auto a2 = [=] { x = n; }; // { dg-bogus "implicit capture" }
+ };
+ };
+
+ auto a8 = [=, this] {
+ // { dg-warning "explicit by-copy capture" "" { target c++17_down } .-1 }
+ auto a = [=, this] {
+ // { dg-warning "explicit by-copy capture" "" { target c++17_down } .-1 }
+ auto a2 = [=, this] { x = n; };
+ // { dg-warning "explicit by-copy capture" "" { target c++17_down } .-1 }
+ };
+ };
+
+ auto a9 = [=, *this]() mutable {
+ auto a = [=, *this]() mutable {
+ auto a2 = [=, *this]() mutable { x = n; };
+ };
+ };
+
+ auto a10 = [&] {
+ auto a = [&] {
+ auto a2 = [&] { x = n; };
+ };
+ };
+
+ auto a11 = [&, this] {
+ auto a = [&, this] {
+ auto a2 = [&, this] { x = n; };
+ };
+ };
+
+ auto a12 = [&, *this]() mutable {
+ auto a = [&, *this]() mutable {
+ auto a2 = [&, *this]() mutable { x = n; };
+ };
+ };
+ }
+};