https://github.com/usx95 commented:

To a certain extent, it is possible to do this more systematically via 
LifetimeSafety.

```cpp
void test_thread() {
  int x = 0;
  std::thread t1([&x]() { x = 1; });
}
```
We can annotate `std::thread` ctor of `lifetime_capture_by(GLOBAL)` for 
lambdas. 

Supporting more general container of functions should also be possible.
```cpp
void local_vector() {
  std::vector<std::function<int()>> LocalFns;
​  { 
    int z;
    LocalFns.emplace_back([&z]() -> int { return z; });
  }
  (void)LocalFns; // use-after-scope of 'z'
}
```

cc: @kashika0112 who is working on capture_by annotation and @aeft who recently 
extended lifetime safety for lambdas and `std::function`

Filed https://github.com/llvm/llvm-project/issues/203886 and 
https://github.com/llvm/llvm-project/issues/203888 to track this.

That said, I expect some road bumps because of multi-level ref types.

https://github.com/llvm/llvm-project/pull/203757
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to