aboutsummaryrefslogtreecommitdiff
path: root/clang/test/Headers/crash-instantiated-in-scope-cxx-modules.cpp
blob: 80844a58ad825a0cb753764d821a3968b5c2ea04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// RUN: rm -fR %t
// RUN: split-file %s %t
// RUN: cd %t
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header -Werror=uninitialized folly-conv.h
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header -Werror=uninitialized thrift_cpp2_base.h
// 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

//--- Conv.h
#pragma once

template <typename _Tp, typename _Up = _Tp&&>
_Up __declval(int);

template <typename _Tp>
auto declval() noexcept -> decltype(__declval<_Tp>(0));

namespace folly {

template <class Value, class Error>
struct Expected {
  template <class Yes>
  auto thenOrThrow() -> decltype(declval<Value&>()) {
    return 1;
  }
};

struct ExpectedHelper {
  template <class Error, class T>
  static constexpr Expected<T, Error> return_(T) {
    return Expected<T, Error>();
  }

  template <class This, class Fn, class E = int, class T = ExpectedHelper>
  static auto then_(This&&, Fn&&)
      -> decltype(T::template return_<E>((declval<Fn>()(true), 0))) {
    return Expected<int, int>();
  }
};

template <class Tgt>
inline Expected<Tgt, const char*> tryTo() {
  Tgt result = 0;
  // In build with asserts:
  // 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.
  // In release build compilation error on the line below inside lambda:
  // error: variable 'result' is uninitialized when used here [-Werror,-Wuninitialized]
  ExpectedHelper::then_(Expected<bool, int>(), [&](bool) { return result; });
  return {};
}

} // namespace folly

inline void bar() {
  folly::tryTo<int>();
}
// expected-no-diagnostics

//--- folly-conv.h
#pragma once
#include "Conv.h"
// expected-no-diagnostics

//--- thrift_cpp2_base.h
#pragma once
#include "Conv.h"
// expected-no-diagnostics

//--- logger_base.h
#pragma once
import "folly-conv.h";
import "thrift_cpp2_base.h";

inline void foo() {
  folly::tryTo<unsigned>();
}
// expected-no-diagnostics