[clang] lifetimebound and explicit object parameters
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
I'm trying to compile the following code:
```c++
struct S {
S &setImplicit(int *NewPointer [[clang::lifetimebound]])
[[clang::lifetimebound]] {
Pointer = NewPointer;
return *this;
}
S &setPtr(this S &Self [[clang::lifetimebound]],
int *NewPointer [[clang::lifetimebound]]) {
Self.Pointer = NewPointer;
return Self;
}
S &wrapper(this S &Self [[clang::lifetimebound]],
int *NewPointer [[clang::lifetimebound]]) {
return Self.setPtr(NewPointer);
}
int *Pointer;
};
int main() {}
```
With `clang -std=c++23 -Wlifetime-safety-all -o bug bug.cpp`, I get 3 warnings that I don't think should be appearing:
```
bug.cpp:2:36: warning: could not verify that the return value can be lifetime bound to 'NewPointer' [-Wlifetime-safety-lifetimebound-violation]
2 | S &setImplicit(int *NewPointer [[clang::lifetimebound]])
| ^~~~~~~~~~~~~~~~~~~~
bug.cpp:9:31: warning: could not verify that the return value can be lifetime bound to 'NewPointer' [-Wlifetime-safety-lifetimebound-violation]
9 | int *NewPointer [[clang::lifetimebound]]) {
| ^~~~~~~~~~~~~~~~~~~~
bug.cpp:13:29: warning: could not verify that the return value can be lifetime bound to 'Self' [-Wlifetime-safety-lifetimebound-violation]
13 | S &wrapper(this S &Self [[clang::lifetimebound]],
| ^~~~~~~~~~~~~~~~~~~~
3 warnings generated.
```
This has been tested with homebrew clang:
```
Homebrew clang version 23.1.0
Target: arm64-apple-darwin25.6.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/llvm/23.1.0/bin
Configuration file: /opt/homebrew/Cellar/llvm/23.1.0/etc/clang/arm64-apple-darwin25.cfg
```
and the llvm toolchain bundled with homebrew emscripten:
```
clang version 24.0.0git
Target: arm64-apple-darwin25.6.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/emscripten/6.0.9/libexec/llvm/bin
```
Contributor guide
Research direction
Start by reproducing the reduced C++23 example with clang -std=c++23 -Wlifetime-safety-all and compare the three diagnostics involving lifetimebound and explicit object parameters. Trace the lifetime-safety diagnostic handling for these declarations and add a regression test based on the example; done means the valid code no longer produces those three warnings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100