aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2025-08-20 10:49:47 -0400
committerMarek Polacek <polacek@redhat.com>2025-08-20 16:28:57 -0400
commit51fbd1e4ea8023847d786a0fdc89219e93bcc666 (patch)
tree5a96f78fdaeb5ef0ca723690417bbdd8ae8952ea
parent6747672747cd86f75519f814816b7f83ddaedcc2 (diff)
downloadgcc-51fbd1e4ea8023847d786a0fdc89219e93bcc666.zip
gcc-51fbd1e4ea8023847d786a0fdc89219e93bcc666.tar.gz
gcc-51fbd1e4ea8023847d786a0fdc89219e93bcc666.tar.bz2
c++: lambda capture and shadowing [PR121553]
P2036 says that this: [x=1]{ int x; } should be rejected, but with my P2036 we started giving an error for the attached testcase as well, breaking Dolphin. So let's keep the error only for init-captures. PR c++/121553 gcc/cp/ChangeLog: * name-lookup.cc (check_local_shadow): Check !is_normal_capture_proxy. gcc/testsuite/ChangeLog: * g++.dg/warn/Wshadow-19.C: Revert P2036 changes. * g++.dg/warn/Wshadow-6.C: Likewise. * g++.dg/warn/Wshadow-20.C: New test. * g++.dg/warn/Wshadow-21.C: New test. Reviewed-by: Jason Merrill <jason@redhat.com>
-rw-r--r--gcc/cp/name-lookup.cc3
-rw-r--r--gcc/testsuite/g++.dg/warn/Wshadow-19.C4
-rw-r--r--gcc/testsuite/g++.dg/warn/Wshadow-20.C7
-rw-r--r--gcc/testsuite/g++.dg/warn/Wshadow-21.C8
-rw-r--r--gcc/testsuite/g++.dg/warn/Wshadow-6.C8
5 files changed, 23 insertions, 7 deletions
diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index 4614790..ba62467 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -3355,7 +3355,8 @@ check_local_shadow (tree decl)
&& TREE_CODE (old) == PARM_DECL)
/* We should also give an error for
[x=1]{ int x; } */
- || is_capture_proxy (old)))
+ || (is_capture_proxy (old)
+ && !is_normal_capture_proxy (old))))
{
/* Go to where the parms should be and see if we find
them there. */
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-19.C b/gcc/testsuite/g++.dg/warn/Wshadow-19.C
index d0b8dba..4f34253 100644
--- a/gcc/testsuite/g++.dg/warn/Wshadow-19.C
+++ b/gcc/testsuite/g++.dg/warn/Wshadow-19.C
@@ -1,5 +1,5 @@
// { dg-do compile }
-// { dg-options "-Wshadow -Wpedantic" }
+// { dg-options "-Wshadow" }
void
foo (int x)
@@ -10,7 +10,7 @@ foo (int x)
extern int y; // { dg-warning "declaration of 'y' shadows a previous local" }
}
#if __cplusplus >= 201102L
- auto fn = [x] () { extern int x; return 0; }; // { dg-warning "declaration of 'int x' shadows a parameter" "" { target c++11 } }
+ auto fn = [x] () { extern int x; return 0; }; // { dg-warning "declaration of 'x' shadows a lambda capture" "" { target c++11 } }
#endif
}
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-20.C b/gcc/testsuite/g++.dg/warn/Wshadow-20.C
new file mode 100644
index 0000000..420373b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wshadow-20.C
@@ -0,0 +1,7 @@
+// PR c++/121553
+// { dg-do compile { target c++11 } }
+
+void foo () {
+ int i;
+ auto f = [i] () { int i; return 0; };
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-21.C b/gcc/testsuite/g++.dg/warn/Wshadow-21.C
new file mode 100644
index 0000000..9b71a082
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wshadow-21.C
@@ -0,0 +1,8 @@
+// PR c++/121553
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wshadow" }
+
+void foo () {
+ int i;
+ auto f = [i] () { int i; return 0; }; // { dg-warning "declaration of .i. shadows a lambda capture" }
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-6.C b/gcc/testsuite/g++.dg/warn/Wshadow-6.C
index 7b44d52..1d8d21b 100644
--- a/gcc/testsuite/g++.dg/warn/Wshadow-6.C
+++ b/gcc/testsuite/g++.dg/warn/Wshadow-6.C
@@ -33,8 +33,8 @@ void f2(struct S i, int j) {
void f3(int i) {
[=]{
- int j = i; // { dg-message "previously declared here" }
- int i; // { dg-error "shadows a parameter" }
+ int j = i; // { dg-message "shadowed declaration" }
+ int i; // { dg-warning "shadows a lambda capture" }
i = 1;
};
}
@@ -42,8 +42,8 @@ void f3(int i) {
template <class T>
void f4(int i) {
[=]{
- int j = i; // { dg-message "previously declared here" }
- int i; // { dg-error "shadows a parameter" }
+ int j = i; // { dg-message "shadowed declaration" }
+ int i; // { dg-warning "shadows a " }
i = 1;
};
}