[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Headers / crash-instantiated-in-scope-cxx-modules.cpp
blob80844a58ad825a0cb753764d821a3968b5c2ea04
1 // RUN: rm -fR %t
2 // RUN: split-file %s %t
3 // RUN: cd %t
4 // RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header -Werror=uninitialized folly-conv.h
5 // RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header -Werror=uninitialized thrift_cpp2_base.h
6 // RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header -Werror=uninitialized -fmodule-file=folly-conv.pcm -fmodule-file=thrift_cpp2_base.pcm logger_base.h
8 //--- Conv.h
9 #pragma once
11 template <typename _Tp, typename _Up = _Tp&&>
12 _Up __declval(int);
14 template <typename _Tp>
15 auto declval() noexcept -> decltype(__declval<_Tp>(0));
17 namespace folly {
19 template <class Value, class Error>
20 struct Expected {
21 template <class Yes>
22 auto thenOrThrow() -> decltype(declval<Value&>()) {
23 return 1;
27 struct ExpectedHelper {
28 template <class Error, class T>
29 static constexpr Expected<T, Error> return_(T) {
30 return Expected<T, Error>();
33 template <class This, class Fn, class E = int, class T = ExpectedHelper>
34 static auto then_(This&&, Fn&&)
35 -> decltype(T::template return_<E>((declval<Fn>()(true), 0))) {
36 return Expected<int, int>();
40 template <class Tgt>
41 inline Expected<Tgt, const char*> tryTo() {
42 Tgt result = 0;
43 // In build with asserts:
44 // clang/lib/Sema/SemaTemplateInstantiate.cpp: llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *clang::LocalInstantiationScope::findInstantiationOf(const Decl *): Assertion `isa<LabelDecl>(D) && "declaration not instantiated in this scope"' failed.
45 // In release build compilation error on the line below inside lambda:
46 // error: variable 'result' is uninitialized when used here [-Werror,-Wuninitialized]
47 ExpectedHelper::then_(Expected<bool, int>(), [&](bool) { return result; });
48 return {};
51 } // namespace folly
53 inline void bar() {
54 folly::tryTo<int>();
56 // expected-no-diagnostics
58 //--- folly-conv.h
59 #pragma once
60 #include "Conv.h"
61 // expected-no-diagnostics
63 //--- thrift_cpp2_base.h
64 #pragma once
65 #include "Conv.h"
66 // expected-no-diagnostics
68 //--- logger_base.h
69 #pragma once
70 import "folly-conv.h";
71 import "thrift_cpp2_base.h";
73 inline void foo() {
74 folly::tryTo<unsigned>();
76 // expected-no-diagnostics