https://gcc.gnu.org/g:51fbd1e4ea8023847d786a0fdc89219e93bcc666

commit r16-3305-g51fbd1e4ea8023847d786a0fdc89219e93bcc666
Author: Marek Polacek <pola...@redhat.com>
Date:   Wed Aug 20 10:49:47 2025 -0400

    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 <ja...@redhat.com>

Diff:
---
 gcc/cp/name-lookup.cc                  | 3 ++-
 gcc/testsuite/g++.dg/warn/Wshadow-19.C | 4 ++--
 gcc/testsuite/g++.dg/warn/Wshadow-20.C | 7 +++++++
 gcc/testsuite/g++.dg/warn/Wshadow-21.C | 8 ++++++++
 gcc/testsuite/g++.dg/warn/Wshadow-6.C  | 8 ++++----
 5 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index 46147905e041..ba6246776258 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 d0b8dba8549f..4f3425328eca 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 000000000000..420373b9ca8e
--- /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 000000000000..9b71a082514b
--- /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 7b44d52781aa..1d8d21b9b6f1 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;
  };
 }

Reply via email to