xref: /llvm-project/clang/test/SemaCXX/char8_t.cpp (revision d0d2772379bd89f1dce3c456520272678cf4b966)
1 // RUN: %clang_cc1 -fchar8_t -std=c++17 -verify %s
2 // RUN: %clang_cc1 -std=c++2a -verify=expected %s
3 // RUN: %clang_cc1 -std=c++2a -verify=expected -fno-signed-char %s
4 
5 
6 char8_t a = u8'a';
7 char8_t b[] = u8"foo";
8 char8_t c = 'a';
9 char8_t d[] = "foo"; // expected-error {{initializing 'char8_t' array with plain string literal}} expected-note {{add 'u8' prefix}}
10 
11 char e = u8'a';
12 char g = 'a';
13 char h[] = "foo";
14 
15 unsigned char i[] = u8"foo";
16 unsigned char j[] = { u8"foo" };
17 char k[] = u8"foo";
18 char l[] = { u8"foo" };
19 signed char m[] = u8"foo"; // expected-error {{initialization of char array with UTF-8 string literal is not permitted}}
20 signed char n[] = { u8"foo" }; // expected-error {{cannot initialize an array element of type 'signed char' with an lvalue of type 'const char8_t[4]'}}
21 
22 const unsigned char* uptr = u8"foo"; // expected-error {{cannot initialize}}
23 const signed char* sptr = u8"foo"; // expected-error {{cannot initialize}}
24 const char* ptr = u8"foo"; // expected-error {{cannot initialize}}
25 
26 template <typename T>
check_values()27 void check_values() {
28   constexpr T c[] = {0, static_cast<T>(0xFF), 0x42};
29   constexpr T a[] = u8"\x00\xFF\x42";
30 
31   static_assert(a[0] == c[0]);
32   static_assert(a[1] == c[1]);
33   static_assert(a[2] == c[2]);
34 }
35 
call_check_values()36 void call_check_values() {
37   check_values<char>();
38   check_values<unsigned char>();
39 }
40 
disambig()41 void disambig() {
42   char8_t (a) = u8'x';
43 }
44 
45 void operator""_a(char);
46 void operator""_a(const char*, decltype(sizeof(0)));
47 
test_udl1()48 void test_udl1() {
49   int &x = u8'a'_a; // expected-error {{no matching literal operator}}
50   float &y = u8"a"_a; // expected-error {{no matching literal operator}}
51 }
52 
53 int &operator""_a(char8_t);
54 float &operator""_a(const char8_t*, decltype(sizeof(0)));
55 
test_udl2()56 void test_udl2() {
57   int &x = u8'a'_a;
58   float &y = u8"a"_a;
59 }
60 
check(T && t)61 template<typename E, typename T> void check(T &&t) {
62   using Check = E;
63   using Check = T;
64 }
check_deduction()65 void check_deduction() {
66   check<char8_t>(u8'a');
67   check<const char8_t(&)[5]>(u8"a\u1000");
68 }
69 
70 static_assert(sizeof(char8_t) == 1);
71 static_assert(char8_t(-1) > 0);
72 static_assert(u8"\u0080"[0] > 0);
73 
74 namespace ambiguous {
75 
76 struct A {
77 	char8_t s[10];
78 };
79 struct B {
80   char s[10];
81 };
82 
83 void f(A); // expected-note {{candidate}}
84 void f(B); // expected-note {{candidate}}
85 
test()86 int test() {
87   f({u8"foo"}); // expected-error {{call to 'f' is ambiguous}}
88 }
89 
90 }
91