[libcxx-commits] [libcxx] Add [[clang::lifetimebound]] to numerous functions in libc++ include headers (PR #112751)
Haojian Wu via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Oct 28 02:34:16 PDT 2024
hokein wrote:
> I seem to be encountering a strange situation where this code gives a dangling reference error when I annotate `std::vector::operator[]` with `[[clang::lifetimebound]]`:
>
> ```c++
> #include <string_view>
> #include <vector>
>
> std::string_view foo() {
> std::vector<std::string_view> v(1);
> return v[0];
> }
> ```
>
> This doesn't trigger if I attempt to mimic `std::vector` with my own type, which makes me think there's a bug somewhere in Clang's built-in analysis that special-cases `std::vector`...
>
> Anybody know what is going on?
This looks like a bug in Clang's lifetime analysis when `lifetimebound` interacts with `gsl::Pointer`.
```cpp
class [[gsl::Pointer]] FooPointer {};
template<typename T>
class [[gsl::Owner]] Container {
public:
const T& get() const [[clang::lifetimebound]];
};
FooPointer abc2() {
Container<FooPointer> foo;
return foo.get(); // Clang emits a return-stack-address warning on `foo`.
}
```
https://godbolt.org/z/ffYjPa7vs,
If you remove any of `gsl::Pointer`, `gsl::Owner`, or `clang::lifetimebound`, the diagnostic disappears.
https://github.com/llvm/llvm-project/pull/112751
More information about the libcxx-commits
mailing list