public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55357] New: -Wshadow warns about lambda function parameters matching variables in outer scope
@ 2012-11-16 18:16 david at doublewise dot net
  2013-03-05 19:25 ` [Bug c++/55357] " jason at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: david at doublewise dot net @ 2012-11-16 18:16 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55357

             Bug #: 55357
           Summary: -Wshadow warns about lambda function parameters
                    matching variables in outer scope
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: david@doublewise.net


Similar to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30566

input:

int main() {
    int x = 1;
    auto const lambda = [](int x) {
        return x;
    };
}



output:

src/main.cpp: In lambda function:
src/main.cpp:3:30: error: declaration of 'x' shadows a previous local
[-Werror=shadow]
src/main.cpp:2:6: error: shadowed declaration is here [-Werror=shadow]
src/main.cpp: In static member function 'static int
main()::<lambda(int)>::_FUN(int)':
src/main.cpp:5:2: error: declaration of 'x' shadows a previous local
[-Werror=shadow]
src/main.cpp:2:6: error: shadowed declaration is here [-Werror=shadow]
cc1plus: all warnings being treated as errors



My lambda has an empty capture specification, so the outer x is not captured.
Note that if I change the lambda parameter's name but do not change the name of
the returned value, I get an error that "x" was not captured.

I can't decide if this is correct behavior for the warning. It would catch
errors caused by people thinking they were using the outer variables by simply
disallowing overlap, which is good. However, it's not possible to use the outer
scope variable no matter what I name my variables in the inner scope, so there
is no chance of a silent behavior change.

However, the following code should always give a warning with -Wshadow:

int main() {
    int x = 1;
    // Capture everything
    auto const lambda = [&](int x) {
        return x;
    };
}




I also note that gcc warns me about the first line the lambda appears on (line
3) and the last line (line 5).


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/55357] -Wshadow warns about lambda function parameters matching variables in outer scope
  2012-11-16 18:16 [Bug c++/55357] New: -Wshadow warns about lambda function parameters matching variables in outer scope david at doublewise dot net
@ 2013-03-05 19:25 ` jason at gcc dot gnu.org
  2013-03-17  2:39 ` jason at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: jason at gcc dot gnu.org @ 2013-03-05 19:25 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55357

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2013-03-05
                 CC|                            |jason at gcc dot gnu.org
         AssignedTo|unassigned at gcc dot       |jason at gcc dot gnu.org
                   |gnu.org                     |
     Ever Confirmed|0                           |1

--- Comment #1 from Jason Merrill <jason at gcc dot gnu.org> 2013-03-05 19:25:34 UTC ---
(In reply to comment #0)
> I can't decide if this is correct behavior for the warning. It would catch
> errors caused by people thinking they were using the outer variables by simply
> disallowing overlap, which is good.

I think it's appropriate to warn here; whether or not there is a default
capture seems like a subtle distinction that is easy to miss when reading the
code.

> I also note that gcc warns me about the first line the lambda appears on (line
> 3) and the last line (line 5).

That's a bug.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/55357] -Wshadow warns about lambda function parameters matching variables in outer scope
  2012-11-16 18:16 [Bug c++/55357] New: -Wshadow warns about lambda function parameters matching variables in outer scope david at doublewise dot net
  2013-03-05 19:25 ` [Bug c++/55357] " jason at gcc dot gnu.org
@ 2013-03-17  2:39 ` jason at gcc dot gnu.org
  2013-03-23 19:22 ` jason at gcc dot gnu.org
  2022-03-26  6:54 ` jcl at nvidia dot com
  3 siblings, 0 replies; 5+ messages in thread
From: jason at gcc dot gnu.org @ 2013-03-17  2:39 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55357

--- Comment #2 from Jason Merrill <jason at gcc dot gnu.org> 2013-03-17 02:38:16 UTC ---
Author: jason
Date: Sun Mar 17 02:38:01 2013
New Revision: 196739

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196739
Log:
    PR c++/55357
    * semantics.c (maybe_add_lambda_conv_op): Clear DECL_NAME of copied
    parms to avoid duplicate -Wshadow warnings.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-shadow1.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/55357] -Wshadow warns about lambda function parameters matching variables in outer scope
  2012-11-16 18:16 [Bug c++/55357] New: -Wshadow warns about lambda function parameters matching variables in outer scope david at doublewise dot net
  2013-03-05 19:25 ` [Bug c++/55357] " jason at gcc dot gnu.org
  2013-03-17  2:39 ` jason at gcc dot gnu.org
@ 2013-03-23 19:22 ` jason at gcc dot gnu.org
  2022-03-26  6:54 ` jcl at nvidia dot com
  3 siblings, 0 replies; 5+ messages in thread
From: jason at gcc dot gnu.org @ 2013-03-23 19:22 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55357

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.9.0

--- Comment #3 from Jason Merrill <jason at gcc dot gnu.org> 2013-03-23 19:22:43 UTC ---
Duplicate warning fixed for 4.9.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/55357] -Wshadow warns about lambda function parameters matching variables in outer scope
  2012-11-16 18:16 [Bug c++/55357] New: -Wshadow warns about lambda function parameters matching variables in outer scope david at doublewise dot net
                   ` (2 preceding siblings ...)
  2013-03-23 19:22 ` jason at gcc dot gnu.org
@ 2022-03-26  6:54 ` jcl at nvidia dot com
  3 siblings, 0 replies; 5+ messages in thread
From: jcl at nvidia dot com @ 2022-03-26  6:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55357

JC Liang <jcl at nvidia dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jcl at nvidia dot com

--- Comment #4 from JC Liang <jcl at nvidia dot com> ---
Could we reevaluate this case? I think it's not necessary to warning about
lambda parameters shadowing local vars in outer scope, because it's simply
won't compile.
MSVC's default-on policy C4457 doesn't have this issue. Our project is cross
platform so we really want to have parity in different compiler for this
warning.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-26  6:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-16 18:16 [Bug c++/55357] New: -Wshadow warns about lambda function parameters matching variables in outer scope david at doublewise dot net
2013-03-05 19:25 ` [Bug c++/55357] " jason at gcc dot gnu.org
2013-03-17  2:39 ` jason at gcc dot gnu.org
2013-03-23 19:22 ` jason at gcc dot gnu.org
2022-03-26  6:54 ` jcl at nvidia dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).