xref: /aosp_15_r20/external/clang/test/SemaCXX/cxx1y-contextual-conversion-tweaks.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -verify -fsyntax-only %s
2*67e74705SXin Li // RUN: %clang_cc1 -std=c++1y %s -verify -DCXX1Y
3*67e74705SXin Li 
4*67e74705SXin Li // Explicit member declarations behave as in C++11.
5*67e74705SXin Li 
6*67e74705SXin Li namespace n3323_example {
7*67e74705SXin Li 
8*67e74705SXin Li   template <class T> class zero_init {
9*67e74705SXin Li   public:
zero_init()10*67e74705SXin Li     zero_init() : val(static_cast<T>(0)) {}
zero_init(T val)11*67e74705SXin Li     zero_init(T val) : val(val) {}
12*67e74705SXin Li 
operator T&()13*67e74705SXin Li     operator T &() { return val; }     //@13
operator T() const14*67e74705SXin Li     operator T() const { return val; } //@14
15*67e74705SXin Li 
16*67e74705SXin Li   private:
17*67e74705SXin Li     T val;
18*67e74705SXin Li   };
19*67e74705SXin Li 
Delete()20*67e74705SXin Li   void Delete() {
21*67e74705SXin Li     zero_init<int *> p;
22*67e74705SXin Li     p = new int(7);
23*67e74705SXin Li     delete p; //@23
24*67e74705SXin Li     delete (p + 0);
25*67e74705SXin Li     delete + p;
26*67e74705SXin Li   }
27*67e74705SXin Li 
Switch()28*67e74705SXin Li   void Switch() {
29*67e74705SXin Li     zero_init<int> i;
30*67e74705SXin Li     i = 7;
31*67e74705SXin Li     switch (i) {} // @31
32*67e74705SXin Li     switch (i + 0) {}
33*67e74705SXin Li     switch (+i) {}
34*67e74705SXin Li   }
35*67e74705SXin Li }
36*67e74705SXin Li 
37*67e74705SXin Li #ifdef CXX1Y
38*67e74705SXin Li #else
39*67e74705SXin Li //expected-error@23 {{ambiguous conversion of delete expression of type 'zero_init<int *>' to a pointer}}
40*67e74705SXin Li //expected-note@13 {{conversion to pointer type 'int *'}}
41*67e74705SXin Li //expected-note@14 {{conversion to pointer type 'int *'}}
42*67e74705SXin Li //expected-error@31 {{multiple conversions from switch condition type 'zero_init<int>' to an integral or enumeration type}}
43*67e74705SXin Li //expected-note@13 {{conversion to integral type 'int'}}
44*67e74705SXin Li //expected-note@14 {{conversion to integral type 'int'}}
45*67e74705SXin Li #endif
46*67e74705SXin Li 
47*67e74705SXin Li namespace extended_examples {
48*67e74705SXin Li 
49*67e74705SXin Li   struct A0 {
50*67e74705SXin Li     operator int();      // matching and viable
51*67e74705SXin Li   };
52*67e74705SXin Li 
53*67e74705SXin Li   struct A1 {
54*67e74705SXin Li     operator int() &&;   // matching and not viable
55*67e74705SXin Li   };
56*67e74705SXin Li 
57*67e74705SXin Li   struct A2 {
58*67e74705SXin Li     operator float();    // not matching
59*67e74705SXin Li   };
60*67e74705SXin Li 
61*67e74705SXin Li   struct A3 {
62*67e74705SXin Li     template<typename T> operator T();  // not matching (ambiguous anyway)
63*67e74705SXin Li   };
64*67e74705SXin Li 
65*67e74705SXin Li   struct A4 {
66*67e74705SXin Li     template<typename T> operator int();  // not matching (ambiguous anyway)
67*67e74705SXin Li   };
68*67e74705SXin Li 
69*67e74705SXin Li   struct B1 {
70*67e74705SXin Li     operator int() &&;  // @70
71*67e74705SXin Li     operator int();     // @71  -- duplicate declaration with different qualifier is not allowed
72*67e74705SXin Li   };
73*67e74705SXin Li 
74*67e74705SXin Li   struct B2 {
75*67e74705SXin Li     operator int() &&;  // matching but not viable
76*67e74705SXin Li     operator float();   // not matching
77*67e74705SXin Li   };
78*67e74705SXin Li 
foo(A0 a0,A1 a1,A2 a2,A3 a3,A4 a4,B2 b2)79*67e74705SXin Li   void foo(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, B2 b2) {
80*67e74705SXin Li     switch (a0) {}
81*67e74705SXin Li     switch (a1) {}     // @81 -- fails for different reasons
82*67e74705SXin Li     switch (a2) {}     // @82
83*67e74705SXin Li     switch (a3) {}     // @83
84*67e74705SXin Li     switch (a4) {}     // @84
85*67e74705SXin Li     switch (b2) {}     // @85 -- fails for different reasons
86*67e74705SXin Li   }
87*67e74705SXin Li }
88*67e74705SXin Li 
89*67e74705SXin Li //expected-error@71 {{cannot overload a member function without a ref-qualifier with a member function with ref-qualifier '&&'}}
90*67e74705SXin Li //expected-note@70 {{previous declaration is here}}
91*67e74705SXin Li //expected-error@82 {{statement requires expression of integer type ('extended_examples::A2' invalid)}}
92*67e74705SXin Li //expected-error@83 {{statement requires expression of integer type ('extended_examples::A3' invalid)}}
93*67e74705SXin Li //expected-error@84 {{statement requires expression of integer type ('extended_examples::A4' invalid)}}
94*67e74705SXin Li 
95*67e74705SXin Li #ifdef CXX1Y
96*67e74705SXin Li //expected-error@81 {{statement requires expression of integer type ('extended_examples::A1' invalid)}}
97*67e74705SXin Li //expected-error@85 {{statement requires expression of integer type ('extended_examples::B2' invalid)}}
98*67e74705SXin Li #else
99*67e74705SXin Li //expected-error@81 {{cannot initialize object parameter of type 'extended_examples::A1' with an expression of type 'extended_examples::A1'}}
100*67e74705SXin Li //expected-error@85 {{cannot initialize object parameter of type 'extended_examples::B2' with an expression of type 'extended_examples::B2'}}
101*67e74705SXin Li #endif
102*67e74705SXin Li 
103*67e74705SXin Li namespace extended_examples_cxx1y {
104*67e74705SXin Li 
105*67e74705SXin Li   struct A1 {   // leads to viable match in C++1y, and no viable match in C++11
106*67e74705SXin Li     operator int() &&;                  // matching but not viable
107*67e74705SXin Li     template <typename T> operator T(); // In C++1y: matching and viable, since disambiguated by L.100
108*67e74705SXin Li   };
109*67e74705SXin Li 
110*67e74705SXin Li   struct A2 {   // leads to ambiguity in C++1y, and no viable match in C++11
111*67e74705SXin Li     operator int() &&;                    // matching but not viable
112*67e74705SXin Li     template <typename T> operator int(); // In C++1y: matching but ambiguous (disambiguated by L.105).
113*67e74705SXin Li   };
114*67e74705SXin Li 
115*67e74705SXin Li   struct B1 {    // leads to one viable match in both cases
116*67e74705SXin Li     operator int();                  // matching and viable
117*67e74705SXin Li     template <typename T> operator T(); // In C++1y: matching and viable, since disambiguated by L.110
118*67e74705SXin Li   };
119*67e74705SXin Li 
120*67e74705SXin Li   struct B2 {    // leads to one viable match in both cases
121*67e74705SXin Li     operator int();                  // matching and viable
122*67e74705SXin Li     template <typename T> operator int(); // In C++1y: matching but ambiguous, since disambiguated by L.115
123*67e74705SXin Li   };
124*67e74705SXin Li 
125*67e74705SXin Li   struct C {    // leads to no match in both cases
126*67e74705SXin Li     operator float();                  // not matching
127*67e74705SXin Li     template <typename T> operator T(); // In C++1y: not matching, nor viable.
128*67e74705SXin Li   };
129*67e74705SXin Li 
130*67e74705SXin Li   struct D {   // leads to viable match in C++1y, and no viable match in C++11
131*67e74705SXin Li     operator int() &&;                  // matching but not viable
132*67e74705SXin Li     operator float();                   // not matching
133*67e74705SXin Li     template <typename T> operator T(); // In C++1y: matching and viable, since disambiguated by L.125
134*67e74705SXin Li   };
135*67e74705SXin Li 
136*67e74705SXin Li 
foo(A1 a1,A2 a2,B1 b1,B2 b2,C c,D d)137*67e74705SXin Li   void foo(A1 a1, A2 a2, B1 b1, B2 b2, C c, D d) {
138*67e74705SXin Li     switch (a1) {} // @138 --  should presumably call templated conversion operator to convert to int.
139*67e74705SXin Li     switch (a2) {} // @139
140*67e74705SXin Li     switch (b1) {}
141*67e74705SXin Li     switch (b2) {}
142*67e74705SXin Li     switch (c) {}  // @142
143*67e74705SXin Li     switch (d) {}  // @143
144*67e74705SXin Li   }
145*67e74705SXin Li }
146*67e74705SXin Li 
147*67e74705SXin Li //expected-error@142 {{statement requires expression of integer type ('extended_examples_cxx1y::C' invalid)}}
148*67e74705SXin Li 
149*67e74705SXin Li #ifdef CXX1Y
150*67e74705SXin Li //expected-error@139 {{statement requires expression of integer type ('extended_examples_cxx1y::A2' invalid)}}
151*67e74705SXin Li #else
152*67e74705SXin Li //expected-error@138 {{cannot initialize object parameter of type 'extended_examples_cxx1y::A1' with an expression of type 'extended_examples_cxx1y::A1'}}
153*67e74705SXin Li //expected-error@139 {{cannot initialize object parameter of type 'extended_examples_cxx1y::A2' with an expression of type 'extended_examples_cxx1y::A2'}}
154*67e74705SXin Li //expected-error@143 {{cannot initialize object parameter of type 'extended_examples_cxx1y::D' with an expression of type 'extended_examples_cxx1y::D'}}
155*67e74705SXin Li #endif
156*67e74705SXin Li 
157*67e74705SXin Li namespace extended_examples_array_bounds {
158*67e74705SXin Li 
159*67e74705SXin Li   typedef decltype(sizeof(int)) size_t;
160*67e74705SXin Li 
161*67e74705SXin Li   struct Foo {
162*67e74705SXin Li     operator size_t();   // @162
163*67e74705SXin Li     operator unsigned short();  // @163
164*67e74705SXin Li   };
165*67e74705SXin Li 
bar()166*67e74705SXin Li   void bar() {
167*67e74705SXin Li     Foo x;
168*67e74705SXin Li     int *p = new int[x];        // @168
169*67e74705SXin Li   }
170*67e74705SXin Li }
171*67e74705SXin Li 
172*67e74705SXin Li #ifdef CXX1Y
173*67e74705SXin Li #else
174*67e74705SXin Li //expected-error@168 {{ambiguous conversion of array size expression of type 'extended_examples_array_bounds::Foo' to an integral or enumeration type}}
175*67e74705SXin Li //expected-note@162 {{conversion to integral type 'size_t'}}
176*67e74705SXin Li //expected-note@163 {{conversion to integral type 'unsigned short' declared here}}
177*67e74705SXin Li #endif
178