Skip to content

Commit 0b6e6a1

Browse files
committed
Add test for as with typesafety
1 parent 016f9ed commit 0b6e6a1

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
void throw_error(CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM) {
2+
throw std::runtime_error(std::string("Type safety exception: ") + msg);
3+
}
4+
5+
make_throwable: (inout cg : cpp2::contract_group) -> _ = {
6+
h := cg.get_handler();
7+
sh := :(pcg : *cpp2::contract_group) = {
8+
pcg*.set_handler(h$);
9+
};
10+
cg.set_handler(throw_error);
11+
return std::unique_ptr<cpp2::contract_group, decltype(sh)>(cg&, sh);
12+
}
13+
14+
void expect_no_throw(auto&& fun) try {
15+
fun();
16+
} catch(std::exception const& e) {
17+
std::cout << e.what() << std::endl;
18+
} catch(...) {
19+
std::cout << "Unknown exception!" << std::endl;
20+
}
21+
22+
main: () = {
23+
o : std::optional<int> = ();
24+
25+
(_ := make_throwable(cpp2::type_safety)) {
26+
expect_no_throw( :() = {
27+
std::cout << (o$ as int) << std::endl; // that will throw
28+
});
29+
}
30+
31+
std::cout << (o as int) << std::endl; // that will terminate
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Type safety exception: 'as' cast failed for 'std::optional'
2+
Type safety violation: 'as' cast failed for 'std::optional'
3+
libc++abi: terminating
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
3+
//=== Cpp2 type declarations ====================================================
4+
5+
6+
#include "cpp2util.h"
7+
8+
#line 1 "mixed-as-with-typesafety.cpp2"
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
#line 1 "mixed-as-with-typesafety.cpp2"
14+
void throw_error(CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM) {
15+
throw std::runtime_error(std::string("Type safety exception: ") + msg);
16+
}
17+
18+
#line 5 "mixed-as-with-typesafety.cpp2"
19+
[[nodiscard]] auto make_throwable(cpp2::contract_group& cg) -> auto;
20+
#line 13 "mixed-as-with-typesafety.cpp2"
21+
22+
void expect_no_throw(auto&& fun) try {
23+
fun();
24+
} catch(std::exception const& e) {
25+
std::cout << e.what() << std::endl;
26+
} catch(...) {
27+
std::cout << "Unknown exception!" << std::endl;
28+
}
29+
30+
auto main() -> int;
31+
32+
//=== Cpp2 function definitions =================================================
33+
34+
#line 1 "mixed-as-with-typesafety.cpp2"
35+
36+
#line 5 "mixed-as-with-typesafety.cpp2"
37+
[[nodiscard]] auto make_throwable(cpp2::contract_group& cg) -> auto{
38+
auto h {CPP2_UFCS(get_handler)(cg)};
39+
auto sh {[_0 = cpp2::move(h)](cpp2::contract_group* pcg) mutable -> void{
40+
CPP2_UFCS(set_handler)((*cpp2::impl::assert_not_null(pcg)), _0);
41+
}};
42+
CPP2_UFCS(set_handler)(cg, throw_error);
43+
return std::unique_ptr<cpp2::contract_group,decltype(sh)>(&cg, cpp2::move(sh));
44+
}
45+
46+
#line 22 "mixed-as-with-typesafety.cpp2"
47+
auto main() -> int{
48+
std::optional<int> o {};
49+
{
50+
[[maybe_unused]] auto const& unnamed_param_1{make_throwable(cpp2::type_safety)};
51+
52+
#line 25 "mixed-as-with-typesafety.cpp2"
53+
{
54+
expect_no_throw([_0 = o]() mutable -> void{
55+
std::cout << (cpp2::impl::as_<int>(_0)) << std::endl;// that will throw
56+
});
57+
}
58+
}
59+
60+
#line 31 "mixed-as-with-typesafety.cpp2"
61+
std::cout << (cpp2::impl::as_<int>(cpp2::move(o))) << std::endl;// that will terminate
62+
}
63+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mixed-as-with-typesafety.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)
2+

0 commit comments

Comments
 (0)