On 8/20/25 10:53 AM, Marek Polacek wrote:
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

-- >8 --
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.
---
  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(-)
  create mode 100644 gcc/testsuite/g++.dg/warn/Wshadow-20.C
  create mode 100644 gcc/testsuite/g++.dg/warn/Wshadow-21.C

diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index 46147905e04..ba624677625 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 d0b8dba8549..4f3425328ec 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 00000000000..420373b9ca8
--- /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 00000000000..9b71a082514
--- /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 7b44d52781a..1d8d21b9b6f 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;
   };
  }

base-commit: 39acf3c9dd767626b422843adb8f5be06206af08

Reply via email to