Skip to content

Commit a464e80

Browse files
committed
Add tests for as casts that ends with static assert
* Add test for as cast to unrelated type * Add test for as cast of arithetic types bigger to smaller * Add test for as cast of bigger signed int to smaller unsigned * Add test for as cast of double to int * Add test for as cast of class to unrelated class * Add test pure2-as-cast-custom-class-to-string
1 parent c0baa18 commit a464e80

24 files changed

+256
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
main: () = {
2+
i := u16(12) as u8; // triggers static assert
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
main: () = {
2+
i := i16(12) as u8; // triggers static assert
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
main: () = {
2+
a := B() as A; // triggers static assert
3+
}
4+
5+
A: type = {}
6+
B: type = {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
main: () = {
2+
s := A() as std::string; // triggers static assert
3+
}
4+
5+
A: type = {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
main: () = {
2+
i := 12.0 as int; // triggers static assert
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
A: type = {}
2+
3+
main: () = {
4+
i := 42;
5+
a := i as A; // triggers static assert
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
In file included from pure2-as-cast-arithetic-types-bigger-to-smaller.cpp:7:
2+
../../../include/cpp2util.h:1794:9: error: static_assert failed due to requirement 'program_violates_type_safety_guarantee<unsigned char, unsigned short &&>' "'as' does not allow unsafe narrowing conversions - if you're sure you want this, use `unsafe_narrow<T>()` to force the conversion"
3+
static_assert(
4+
^
5+
pure2-as-cast-arithetic-types-bigger-to-smaller.cpp:19:19: note: in instantiation of function template specialization 'cpp2::as_<unsigned char, unsigned short>' requested here
6+
auto i {cpp2::as_<cpp2::u8>(cpp2::u16(12))}; // triggers static assert
7+
^
8+
1 error generated.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
In file included from pure2-as-cast-bigger-signed-to-smaller-unsigned.cpp:7:
2+
../../../include/cpp2util.h:1794:9: error: static_assert failed due to requirement 'program_violates_type_safety_guarantee<unsigned char, short &&>' "'as' does not allow unsafe narrowing conversions - if you're sure you want this, use `unsafe_narrow<T>()` to force the conversion"
3+
static_assert(
4+
^
5+
pure2-as-cast-bigger-signed-to-smaller-unsigned.cpp:19:19: note: in instantiation of function template specialization 'cpp2::as_<unsigned char, short>' requested here
6+
auto i {cpp2::as_<cpp2::u8>(cpp2::i16(12))}; // triggers static assert
7+
^
8+
1 error generated.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
In file included from pure2-as-cast-class-to-unrelated-class.cpp:7:
2+
../../../include/cpp2util.h:1800:9: error: static_assert failed due to requirement 'program_violates_type_safety_guarantee<A, B &&>' "No safe 'as' cast available - please check your cast"
3+
static_assert(
4+
^
5+
pure2-as-cast-class-to-unrelated-class.cpp2:12:19: note: in instantiation of function template specialization 'cpp2::as_<A, B>' requested here
6+
auto a {cpp2::as_<A>(B())}; // triggers static assert
7+
^
8+
1 error generated.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
In file included from pure2-as-cast-custom-class-to-string.cpp:7:
2+
../../../include/cpp2util.h:1800:9: error: static_assert failed due to requirement 'program_violates_type_safety_guarantee<std::string, A &&>' "No safe 'as' cast available - please check your cast"
3+
static_assert(
4+
^
5+
pure2-as-cast-custom-class-to-string.cpp2:11:19: note: in instantiation of function template specialization 'cpp2::as_<std::string, A>' requested here
6+
auto s {cpp2::as_<std::string>(A())}; // triggers static assert
7+
^
8+
1 error generated.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
In file included from pure2-as-cast-double-to-int.cpp:7:
2+
../../../include/cpp2util.h:1794:9: error: static_assert failed due to requirement 'program_violates_type_safety_guarantee<int, double &&>' "'as' does not allow unsafe narrowing conversions - if you're sure you want this, use `unsafe_narrow<T>()` to force the conversion"
3+
static_assert(
4+
^
5+
pure2-as-cast-double-to-int.cpp:19:19: note: in instantiation of function template specialization 'cpp2::as_<int, double>' requested here
6+
auto i {cpp2::as_<int>(12.0)}; // triggers static assert
7+
^
8+
1 error generated.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
In file included from pure2-as-cast-to-unrelated-type.cpp:7:
2+
../../../include/cpp2util.h:1800:9: error: static_assert failed due to requirement 'program_violates_type_safety_guarantee<A, int &>' "No safe 'as' cast available - please check your cast"
3+
static_assert(
4+
^
5+
pure2-as-cast-to-unrelated-type.cpp2:5:19: note: in instantiation of function template specialization 'cpp2::as_<A, int &>' requested here
6+
auto a {cpp2::as_<A>(i)}; // triggers static assert
7+
^
8+
1 error generated.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
auto main() -> int;
14+
15+
16+
//=== Cpp2 function definitions =================================================
17+
18+
auto main() -> int{
19+
auto i {cpp2::as_<cpp2::u8>(cpp2::u16(12))}; // triggers static assert
20+
}
21+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-as-cast-arithetic-types-bigger-to-smaller.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
auto main() -> int;
14+
15+
16+
//=== Cpp2 function definitions =================================================
17+
18+
auto main() -> int{
19+
auto i {cpp2::as_<cpp2::u8>(cpp2::i16(12))}; // triggers static assert
20+
}
21+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-as-cast-bigger-signed-to-smaller-unsigned.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
#line 5 "pure2-as-cast-class-to-unrelated-class.cpp2"
11+
class A;
12+
class B;
13+
14+
15+
//=== Cpp2 type definitions and function declarations ===========================
16+
17+
auto main() -> int;
18+
19+
20+
#line 5 "pure2-as-cast-class-to-unrelated-class.cpp2"
21+
class A {
22+
public: A() = default;
23+
public: A(A const&) = delete; /* No 'that' constructor, suppress copy */
24+
public: auto operator=(A const&) -> void = delete;
25+
26+
#line 5 "pure2-as-cast-class-to-unrelated-class.cpp2"
27+
};
28+
class B {
29+
public: B() = default;
30+
public: B(B const&) = delete; /* No 'that' constructor, suppress copy */
31+
public: auto operator=(B const&) -> void = delete;
32+
33+
#line 6 "pure2-as-cast-class-to-unrelated-class.cpp2"
34+
};
35+
36+
37+
//=== Cpp2 function definitions =================================================
38+
39+
auto main() -> int{
40+
auto a {cpp2::as_<A>(B())}; // triggers static assert
41+
}
42+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-as-cast-class-to-unrelated-class.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
#line 5 "pure2-as-cast-custom-class-to-string.cpp2"
11+
class A;
12+
13+
14+
//=== Cpp2 type definitions and function declarations ===========================
15+
16+
auto main() -> int;
17+
18+
19+
#line 5 "pure2-as-cast-custom-class-to-string.cpp2"
20+
class A {
21+
public: A() = default;
22+
public: A(A const&) = delete; /* No 'that' constructor, suppress copy */
23+
public: auto operator=(A const&) -> void = delete;
24+
25+
#line 5 "pure2-as-cast-custom-class-to-string.cpp2"
26+
};
27+
28+
29+
//=== Cpp2 function definitions =================================================
30+
31+
auto main() -> int{
32+
auto s {cpp2::as_<std::string>(A())}; // triggers static assert
33+
}
34+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-as-cast-custom-class-to-string.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
auto main() -> int;
14+
15+
16+
//=== Cpp2 function definitions =================================================
17+
18+
auto main() -> int{
19+
auto i {cpp2::as_<int>(12.0)}; // triggers static assert
20+
}
21+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-as-cast-double-to-int.cpp2... ok (all Cpp2, passes safety checks)
2+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
class A;
10+
11+
12+
//=== Cpp2 type definitions and function declarations ===========================
13+
14+
class A {
15+
public: A() = default;
16+
public: A(A const&) = delete; /* No 'that' constructor, suppress copy */
17+
public: auto operator=(A const&) -> void = delete;
18+
};
19+
20+
auto main() -> int;
21+
22+
23+
//=== Cpp2 function definitions =================================================
24+
25+
26+
#line 3 "pure2-as-cast-to-unrelated-type.cpp2"
27+
auto main() -> int{
28+
auto i {42};
29+
auto a {cpp2::as_<A>(i)}; // triggers static assert
30+
}
31+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-as-cast-to-unrelated-type.cpp2... ok (all Cpp2, passes safety checks)
2+

0 commit comments

Comments
 (0)