llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LifetimeSafety] Assertion failure for implicit lifetimebound in nested std namespace

Open zeyi2 opened this issue 6 months ago • 5 comments

I found this crash while working on https://github.com/llvm/llvm-project/pull/195231#issuecomment-4373047817

Reproducer: https://godbolt.org/z/KGz6zT6fT

This seems to happen when standard library code is compiled as ordinary user source rather than being treated as a system header, for example from a preprocessed repro. The repro attached in the linked comment does exactly that: it contains expanded libc++ code, so -Wlifetime-safety analyzes declarations that would normally be hidden behind <...> system headers.

The likely issue is that isInStlNamespace() only checks the immediate declaration context. For functions/classes nested below std through intermediate namespaces or records, the direct context is not necessarily the std namespace, so the STL guard returns false.

zeyi2 avatar May 13 '26 14:05 zeyi2

@llvm/issue-subscribers-clang-temporal-safety

Author: Zeyi Xu (zeyi2)

I found this crash while working on https://github.com/llvm/llvm-project/pull/195231#issuecomment-4373047817

Reproducer: https://godbolt.org/z/KGz6zT6fT

The likely issue is that isInStlNamespace() only checks the immediate declaration context. For functions/classes nested below std through intermediate namespaces or records, the direct context is not necessarily the std namespace, so the STL guard returns false.

I'm not sure whether this crash is considered important enough to fix, since it appears to require preprocessed standard library code, but the assertion does seem reachable with a reduced repro.

If it is meaningful, I'd like to work on this issue myself.

zeyi2 avatar May 13 '26 14:05 zeyi2

Ohhh this is relatable! I remember encountering this, although not as a crash, but just getting an incorrect result from that function.

NeKon69 avatar May 13 '26 16:05 NeKon69

Oo. This looks important specially if it happens frequently.

We must remove the assert for now add a FIXME to add back later. Maybe the proper fix is to verify only "inferred" annotations and not which were added implicitly by special treatment.

usx95 avatar May 13 '26 19:05 usx95

Just my two cents here. I think this can be solved by walking the DeclContext chain in isInStlNamespace.

NeKon69 avatar May 13 '26 20:05 NeKon69