llvm / llvm/llvm-project

[[clang::lifetimebound]] false positive with template functions

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

Description

```c++
#include

auto forward1(std::span s [[clang::lifetimebound]]) {
return s;
}

template
std::span forward2(T&& s [[clang::lifetimebound]]) {
return std::span(s);
}

void bar(std::span sp){
{auto data = forward1(forward1(sp));} // success
{auto data = forward1(forward2(sp));} // success
{auto data = forward2(sp) ;} // success
{auto data = forward2(forward1(sp));} // fails with -Wdangling
{auto data = forward2(forward2(sp));} // fails with -Wdangling
}
```

The same happens when replacing

```c++
template
std::span forward2(T&& s [[clang::lifetimebound]]) {
return std::span(s);
}
```

with

```c++
template
std::span forward2(const T& s [[clang::lifetimebound]]) {
return std::span(s);
}
```
.

With

```c++
template
std::span forward2( std::span s [[clang::lifetimebound]]) {
return std::span(s);
}
```

no false positive is triggered.

Contributor guide

Open the contributing guide

Research direction

Start by compiling the supplied C++ reproducer with Clang and -Wdangling, comparing the forward1 and forward2 cases and their span parameter variants. Done means the templated forward2 calls no longer produce false-positive warnings while the intended lifetime diagnostics remain.

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
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.