public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106508] New: Extra warnings with lambda captures
@ 2022-08-02 18:20 ozixtheorange at gmail dot com
  2022-12-11  1:51 ` [Bug c++/106508] " pinskia at gcc dot gnu.org
  2022-12-11  1:55 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: ozixtheorange at gmail dot com @ 2022-08-02 18:20 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106508
           Summary: Extra warnings with lambda captures
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ozixtheorange at gmail dot com
  Target Milestone: ---

The following will not work, because `this` needs to be captured to access
`member`. Seems like GCC should be able to detect that `member` is a class
member and warn that `this` is not being captured:

int outer;
struct A{
  int member = 0;
  auto fn(){
     return [&]{
       outer = member; // invalid
     };
  }
};

The following also fails, this time because `val` goes out of scope and so the
reference to `val` is invalid. I don't know if it is feasible for GCC to detect
this scenario, e.g. the lambda's lifetime extends beyond the scope of
captured-by-reference variables; but, a warning in this case would be nice as
well.

int outer;
auto fn1(int val = 0){
  return [&]{
    outer = val; // invalid
  };
}
std::thread lambda;
void fn2(int val = 0){
  lambda = std::thread([&]{
    outer = val; // invalid
  });
}

The reason I suggest this is because the compilation will succeed without
warning, but the binary can segfault and it is difficult to trace where the
source of the problem is. The first case is more subtle (forgetting `this`),
the second perhaps a bit easier to recognize though when debugging.

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

* [Bug c++/106508] Extra warnings with lambda captures
  2022-08-02 18:20 [Bug c++/106508] New: Extra warnings with lambda captures ozixtheorange at gmail dot com
@ 2022-12-11  1:51 ` pinskia at gcc dot gnu.org
  2022-12-11  1:55 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-11  1:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The first case you use default capturing as reference, therefor this will be
captured just fine.

The second case is similar to:
struct f1{int *a;};
struct f1 f(int t)
{
    struct f1 t1 = {&t};
    return t1;
}
Which I thought we warned about but it looks like I was wrong.

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

* [Bug c++/106508] Extra warnings with lambda captures
  2022-08-02 18:20 [Bug c++/106508] New: Extra warnings with lambda captures ozixtheorange at gmail dot com
  2022-12-11  1:51 ` [Bug c++/106508] " pinskia at gcc dot gnu.org
@ 2022-12-11  1:55 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-11  1:55 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 104958.

*** This bug has been marked as a duplicate of bug 104958 ***

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

end of thread, other threads:[~2022-12-11  1:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-02 18:20 [Bug c++/106508] New: Extra warnings with lambda captures ozixtheorange at gmail dot com
2022-12-11  1:51 ` [Bug c++/106508] " pinskia at gcc dot gnu.org
2022-12-11  1:55 ` pinskia at gcc dot gnu.org

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).