| Issue |
180391
|
| Summary |
[LifetimeSafety] False-positive (`object whose reference is captured is later invalidated`) with `push_back` in range-based for-loop
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
SidneyCogdill
|
https://godbolt.org/z/Ef17Txo1K
```cpp
#include <vector>
auto g(std::vector<int> a) {
for (auto& _ : a) {
a.push_back(42);
}
}
```
`-Werror=lifetime-safety`
```
<source>:4:20: error: object whose reference is captured is later invalidated [-Werror,-Wlifetime-safety-invalidation]
4 | for (auto& _ : a) {
| ^
<source>:5:11: note: invalidated here
5 | a.push_back(42);
| ~~^~~~~~~~~~~~~
<source>:4:18: note: later used here
4 | for (auto& _ : a) {
| ^
1 error generated.
Compiler returned: 1
```
---
Interestingly but unrelated to the current Lifetime Safety implementation, the [P1179](https://wg21.link/p1179) prototype implementation has the same false-positive: https://godbolt.org/z/4x3xfanaP
`-Werror=lifetime`
```
<source>:4:18: error: passing a dangling pointer as argument [-Werror,-Wlifetime]
for (auto& _ : a) {
^
<source>:5:9: note: modified here
a.push_back(42);
^
1 error generated.
Compiler returned: 1
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs