Skip to content

Commit 2480221

Browse files
committed
Add assertion + test case for hsutter#108
1 parent 4929b15 commit 2480221

10 files changed

+125
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <vector>
2+
#include <array>
3+
#include <string>
4+
5+
template <typename A, typename B>
6+
struct my_type {};
7+
8+
fun: (v : _) -> std::string = {
9+
return inspect v -> std::string {
10+
is std::vector = "std::vector";
11+
is std::array = "std::array";
12+
is std::variant = "std::variant";
13+
is my_type = "my_type";
14+
is _ = "unknown";
15+
};
16+
}
17+
18+
fun2: (v : _) -> std::string = {
19+
if v is std::vector { return "std::vector"; }
20+
if v is std::array { return "std::array"; }
21+
if v is std::variant { return "std::variant"; }
22+
if v is my_type { return "my_type"; }
23+
return "unknown";
24+
}
25+
26+
main: () -> int = {
27+
vec : std::vector<int> = (1,2,3);
28+
arr : std::array<int,4> = (1,2,3,4);
29+
var : std::variant<int, double, std::string> = ("cpp2 rulez");
30+
myt : my_type<int, double> = ();
31+
32+
std::cout << "inspected vec : (fun(vec))$" << std::endl;
33+
std::cout << "inspected arr : (fun(arr))$" << std::endl;
34+
std::cout << "inspected var : (fun(var))$" << std::endl;
35+
std::cout << "inspected myt : (fun(myt))$" << std::endl;
36+
37+
std::cout << "inspected vec : (fun2(vec))$" << std::endl;
38+
std::cout << "inspected arr : (fun2(arr))$" << std::endl;
39+
std::cout << "inspected var : (fun2(var))$" << std::endl;
40+
std::cout << "inspected myt : (fun2(myt))$" << std::endl;
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
inspected vec : std::vector
2+
inspected arr : std::array
3+
inspected var : std::variant
4+
inspected myt : my_type
5+
inspected vec : std::vector
6+
inspected arr : std::array
7+
inspected var : std::variant
8+
inspected myt : my_type

regression-tests/test-results/clang-12/mixed-inspect-templates.cpp.output

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
inspected vec : std::vector
2+
inspected arr : std::array
3+
inspected var : std::variant
4+
inspected myt : my_type
5+
inspected vec : std::vector
6+
inspected arr : std::array
7+
inspected var : std::variant
8+
inspected myt : my_type

regression-tests/test-results/gcc-10/mixed-inspect-templates.cpp.output

Whitespace-only changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// ----- Cpp2 support -----
2+
#include "cpp2util.h"
3+
4+
#line 1 "mixed-inspect-templates.cpp2"
5+
#include <vector>
6+
#include <array>
7+
#include <string>
8+
9+
template <typename A, typename B>
10+
struct my_type {};
11+
12+
[[nodiscard]] auto fun(auto const& v) -> std::string;
13+
#line 18 "mixed-inspect-templates.cpp2"
14+
[[nodiscard]] auto fun2(auto const& v) -> std::string;
15+
#line 26 "mixed-inspect-templates.cpp2"
16+
[[nodiscard]] auto main() -> int;
17+
18+
//=== Cpp2 definitions ==========================================================
19+
20+
#line 7 "mixed-inspect-templates.cpp2"
21+
22+
[[nodiscard]] auto fun(auto const& v) -> std::string{
23+
return [&] () -> std::string { auto&& __expr = v;
24+
if (cpp2::is<std::vector>(__expr)) { if constexpr( requires{"std::vector";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("std::vector")),std::string> ) return "std::vector"; else return std::string{}; else return std::string{}; }
25+
else if (cpp2::is<std::array>(__expr)) { if constexpr( requires{"std::array";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("std::array")),std::string> ) return "std::array"; else return std::string{}; else return std::string{}; }
26+
else if (cpp2::is<std::variant>(__expr)) { if constexpr( requires{"std::variant";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("std::variant")),std::string> ) return "std::variant"; else return std::string{}; else return std::string{}; }
27+
else if (cpp2::is<my_type>(__expr)) { if constexpr( requires{"my_type";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("my_type")),std::string> ) return "my_type"; else return std::string{}; else return std::string{}; }
28+
else return "unknown"; }
29+
();
30+
}
31+
32+
[[nodiscard]] auto fun2(auto const& v) -> std::string{
33+
if (cpp2::is<std::vector>(v)) {return "std::vector"; }
34+
if (cpp2::is<std::array>(v)) {return "std::array"; }
35+
if (cpp2::is<std::variant>(v)) {return "std::variant"; }
36+
if (cpp2::is<my_type>(v)) {return "my_type"; }
37+
return "unknown";
38+
}
39+
40+
[[nodiscard]] auto main() -> int{
41+
std::vector<int> vec { 1, 2, 3 };
42+
std::array<int,4> arr { 1, 2, 3, 4 };
43+
std::variant<int,double,std::string> var { "cpp2 rulez" };
44+
my_type <int, double> myt { };
45+
46+
std::cout << "inspected vec : " + cpp2::to_string(fun(vec)) << std::endl;
47+
std::cout << "inspected arr : " + cpp2::to_string(fun(arr)) << std::endl;
48+
std::cout << "inspected var : " + cpp2::to_string(fun(var)) << std::endl;
49+
std::cout << "inspected myt : " + cpp2::to_string(fun(myt)) << std::endl;
50+
51+
std::cout << "inspected vec : " + cpp2::to_string(fun2(vec)) << std::endl;
52+
std::cout << "inspected arr : " + cpp2::to_string(fun2(arr)) << std::endl;
53+
std::cout << "inspected var : " + cpp2::to_string(fun2(var)) << std::endl;
54+
std::cout << "inspected myt : " + cpp2::to_string(fun2(myt)) << std::endl;
55+
}
56+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mixed-inspect-templates.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)
2+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
inspected vec : std::vector
2+
inspected arr : std::array
3+
inspected var : std::variant
4+
inspected myt : my_type
5+
inspected vec : std::vector
6+
inspected arr : std::array
7+
inspected var : std::variant
8+
inspected myt : my_type
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mixed-inspect-templates.cpp

source/cppfront.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,7 @@ class cppfront
17891789
assert(i->op_close);
17901790
auto local_args = text_chunks_with_parens_position{{}, i->op->position(), i->op_close->position()};
17911791

1792+
assert (i->expr_list);
17921793
if (!i->expr_list->expressions.empty()) {
17931794
local_args.text_chunks = print_to_text_chunks(*i->expr_list);
17941795
}

0 commit comments

Comments
 (0)