llvm / llvm/llvm-project

[LifetimeSafety] Diagnose `std::optional<T&>` captures

Open
#205,541 1 comment 0 reactions 0 assignees View on GitHub
clang:temporal-safety false-negative
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/hzEK87abr

```cpp
#include
#include

int main() {
std::optional p;
{
auto x = 42;
p = x;
}
auto result = *p;
printf("%d", result);
}
```

`-std=c++26 -Wlifetime-safety-all -Xclang=-flifetime-safety-inference -fsanitize=address,undefined`

I think this is similar to `std::optional`, `std::unique_ptr` or any gsl::Owner that owns a pointer. And therefore clang::lifetimebound interactions with `std::optional&` can be rather complicated.

Contributor guide

Open the contributing guide

Research direction

Start with the Godbolt reproducer and its C++26 lifetime-safety flags, then compare the behavior of std::optional with the optional pointer and lifetimebound cases mentioned in the issue. Done means establishing and implementing the expected diagnostic behavior for the dangling capture, including the interaction with lifetimebound.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.