|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Testing erroneous ways of using shorthands with the `==` and `!=` operators. |
| 6 | + |
| 7 | +// SharedOptions=--enable-experiment=enum-shorthands |
| 8 | + |
| 9 | +import '../enum_shorthand_helper.dart'; |
| 10 | + |
| 11 | +class ConstConstructorAssert { |
| 12 | + const ConstConstructorAssert.blue(Color color) |
| 13 | + : assert(.blue == color); |
| 14 | + // ^ |
| 15 | + // [analyzer] unspecified |
| 16 | + // [cfe] unspecified |
| 17 | + |
| 18 | + const ConstConstructorAssert.notBlue(Color color) |
| 19 | + : assert(.blue != color); |
| 20 | + // ^ |
| 21 | + // [analyzer] unspecified |
| 22 | + // [cfe] unspecified |
| 23 | + |
| 24 | + const ConstConstructorAssert.one(Integer integer) |
| 25 | + : assert(.constOne == integer); |
| 26 | + // ^ |
| 27 | + // [analyzer] unspecified |
| 28 | + // [cfe] unspecified |
| 29 | + |
| 30 | + const ConstConstructorAssert.notOne(Integer integer) |
| 31 | + : assert(.constOne != integer); |
| 32 | + // ^ |
| 33 | + // [analyzer] unspecified |
| 34 | + // [cfe] unspecified |
| 35 | + |
| 36 | + const ConstConstructorAssert.oneExt(IntegerExt integer) |
| 37 | + : assert(.constOne == integer); |
| 38 | + // ^ |
| 39 | + // [analyzer] unspecified |
| 40 | + // [cfe] unspecified |
| 41 | + |
| 42 | + const ConstConstructorAssert.notOneExt(IntegerExt integer) |
| 43 | + : assert(.constOne != integer); |
| 44 | + // ^ |
| 45 | + // [analyzer] unspecified |
| 46 | + // [cfe] unspecified |
| 47 | +} |
| 48 | + |
| 49 | +void notSymmetrical(Color color, Integer integer, IntegerExt integerExt) { |
| 50 | + const bool symBlueEq = .blue == color; |
| 51 | + // ^ |
| 52 | + // [analyzer] unspecified |
| 53 | + // [cfe] unspecified |
| 54 | + |
| 55 | + const bool symBlueNeq = .blue != color; |
| 56 | + // ^ |
| 57 | + // [analyzer] unspecified |
| 58 | + // [cfe] unspecified |
| 59 | + |
| 60 | + const bool symOneEq = .one == integer; |
| 61 | + // ^ |
| 62 | + // [analyzer] unspecified |
| 63 | + // [cfe] unspecified |
| 64 | + |
| 65 | + const bool symOneNeq = .one != integer; |
| 66 | + // ^ |
| 67 | + // [analyzer] unspecified |
| 68 | + // [cfe] unspecified |
| 69 | + |
| 70 | + const bool symOneExtEq = .one == integerExt; |
| 71 | + // ^ |
| 72 | + // [analyzer] unspecified |
| 73 | + // [cfe] unspecified |
| 74 | + |
| 75 | + const bool symOneExtNeq = .one != integerExt; |
| 76 | + // ^ |
| 77 | + // [analyzer] unspecified |
| 78 | + // [cfe] unspecified |
| 79 | + |
| 80 | + if (.blue == color) print('not ok'); |
| 81 | + // ^ |
| 82 | + // [analyzer] unspecified |
| 83 | + // [cfe] unspecified |
| 84 | + |
| 85 | + if (.blue != color) print('not ok'); |
| 86 | + // ^ |
| 87 | + // [analyzer] unspecified |
| 88 | + // [cfe] unspecified |
| 89 | + |
| 90 | + if (.one == integer) print('not ok'); |
| 91 | + // ^ |
| 92 | + // [analyzer] unspecified |
| 93 | + // [cfe] unspecified |
| 94 | + |
| 95 | + if (.one != integer) print('not ok'); |
| 96 | + // ^ |
| 97 | + // [analyzer] unspecified |
| 98 | + // [cfe] unspecified |
| 99 | + |
| 100 | + if (.one == integerExt) print('not ok'); |
| 101 | + // ^ |
| 102 | + // [analyzer] unspecified |
| 103 | + // [cfe] unspecified |
| 104 | + |
| 105 | + if (.one != integerExt) print('not ok'); |
| 106 | + // ^ |
| 107 | + // [analyzer] unspecified |
| 108 | + // [cfe] unspecified |
| 109 | +} |
| 110 | + |
| 111 | +void rhsNeedsToBeShorthand(Color color, Integer integer, IntegerExt integerExt, bool condition) { |
| 112 | + const Color constColor = Color.red; |
| 113 | + const Object obj = true; |
| 114 | + const bool constCondition = obj as bool; |
| 115 | + |
| 116 | + const bool rhsColorEq = constColor == (constCondition ? .red : .green); |
| 117 | + // ^ |
| 118 | + // [analyzer] unspecified |
| 119 | + // [cfe] unspecified |
| 120 | + |
| 121 | + const bool rhsColorNeq = constColor != (constCondition ? .red : .green); |
| 122 | + // ^ |
| 123 | + // [analyzer] unspecified |
| 124 | + // [cfe] unspecified |
| 125 | + |
| 126 | + if (color == (condition ? .red : .green)) print('not ok'); |
| 127 | + // ^ |
| 128 | + // [analyzer] unspecified |
| 129 | + // [cfe] unspecified |
| 130 | + |
| 131 | + if (color != (condition ? .red : .green)) print('not ok'); |
| 132 | + // ^ |
| 133 | + // [analyzer] unspecified |
| 134 | + // [cfe] unspecified |
| 135 | + |
| 136 | + if (color case == (constCondition ? .red : .green)) { |
| 137 | + // ^ |
| 138 | + // [analyzer] unspecified |
| 139 | + // [cfe] unspecified |
| 140 | + print('not ok'); |
| 141 | + } |
| 142 | + |
| 143 | + if (color case != (constCondition ? .red : .green)) { |
| 144 | + // ^ |
| 145 | + // [analyzer] unspecified |
| 146 | + // [cfe] unspecified |
| 147 | + print('not ok'); |
| 148 | + } |
| 149 | + |
| 150 | + const Integer constInteger = Integer.constOne; |
| 151 | + const bool rhsIntegerEq = constInteger == (constCondition ? .constOne : .constTwo); |
| 152 | + // ^ |
| 153 | + // [analyzer] unspecified |
| 154 | + // [cfe] unspecified |
| 155 | + |
| 156 | + const bool rhsIntegerNeq = constInteger != (constCondition ? .constOne : .constTwo); |
| 157 | + // ^ |
| 158 | + // [analyzer] unspecified |
| 159 | + // [cfe] unspecified |
| 160 | + |
| 161 | + if (integer == (condition ? .constOne : .constTwo)) { |
| 162 | + // ^ |
| 163 | + // [analyzer] unspecified |
| 164 | + // [cfe] unspecified |
| 165 | + print('not ok'); |
| 166 | + } |
| 167 | + |
| 168 | + if (integer != (condition ? .constOne : .constTwo)) { |
| 169 | + // ^ |
| 170 | + // [analyzer] unspecified |
| 171 | + // [cfe] unspecified |
| 172 | + print('not ok'); |
| 173 | + } |
| 174 | + |
| 175 | + if (integer case == (constCondition ? .constOne : .constTwo)) { |
| 176 | + // ^ |
| 177 | + // [analyzer] unspecified |
| 178 | + // [cfe] unspecified |
| 179 | + print('not ok'); |
| 180 | + } |
| 181 | + |
| 182 | + if (integer case != (constCondition ? .constOne : .constTwo)) { |
| 183 | + // ^ |
| 184 | + // [analyzer] unspecified |
| 185 | + // [cfe] unspecified |
| 186 | + print('not ok'); |
| 187 | + } |
| 188 | + |
| 189 | + const IntegerExt constIntegerExt = IntegerExt.constOne; |
| 190 | + const bool rhsIntegerExtEq = constIntegerExt == (constCondition ? .constOne : .constTwo); |
| 191 | + // ^ |
| 192 | + // [analyzer] unspecified |
| 193 | + // [cfe] unspecified |
| 194 | + |
| 195 | + const bool rhsIntegerExtNeq = constIntegerExt != (constCondition ? .constOne : .constTwo); |
| 196 | + // ^ |
| 197 | + // [analyzer] unspecified |
| 198 | + // [cfe] unspecified |
| 199 | + |
| 200 | + if (integerExt == (condition ? .constOne : .constTwo)) { |
| 201 | + // ^ |
| 202 | + // [analyzer] unspecified |
| 203 | + // [cfe] unspecified |
| 204 | + print('not ok'); |
| 205 | + } |
| 206 | + |
| 207 | + if (integerExt != (condition ? .constOne : .constTwo)) { |
| 208 | + // ^ |
| 209 | + // [analyzer] unspecified |
| 210 | + // [cfe] unspecified |
| 211 | + print('not ok'); |
| 212 | + } |
| 213 | + |
| 214 | + if (integerExt case == (constCondition ? .constOne : .constTwo)) { |
| 215 | + // ^ |
| 216 | + // [analyzer] unspecified |
| 217 | + // [cfe] unspecified |
| 218 | + print('not ok'); |
| 219 | + } |
| 220 | + |
| 221 | + if (integerExt case != (constCondition ? .constOne : .constTwo)) { |
| 222 | + // ^ |
| 223 | + // [analyzer] unspecified |
| 224 | + // [cfe] unspecified |
| 225 | + print('not ok'); |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +void objectContextType(Color color, Integer integer, IntegerExt integerExt) { |
| 230 | + const Color constColor = Color.red; |
| 231 | + const bool contextTypeColorEq = (constColor as Object) == .blue; |
| 232 | + // ^ |
| 233 | + // [analyzer] unspecified |
| 234 | + // [cfe] unspecified |
| 235 | + |
| 236 | + const bool contextTypeColorNeq = (constColor as Object) != .blue; |
| 237 | + // ^ |
| 238 | + // [analyzer] unspecified |
| 239 | + // [cfe] unspecified |
| 240 | + |
| 241 | + if ((color as Object) == .blue) print('not ok'); |
| 242 | + // ^ |
| 243 | + // [analyzer] unspecified |
| 244 | + // [cfe] unspecified |
| 245 | + |
| 246 | + if ((color as Object) case == .blue) print('not ok'); |
| 247 | + // ^ |
| 248 | + // [analyzer] unspecified |
| 249 | + // [cfe] unspecified |
| 250 | + |
| 251 | + if ((color as Object) != .blue) print('not ok'); |
| 252 | + // ^ |
| 253 | + // [analyzer] unspecified |
| 254 | + // [cfe] unspecified |
| 255 | + |
| 256 | + if ((color as Object) case != .blue) print('not ok'); |
| 257 | + // ^ |
| 258 | + // [analyzer] unspecified |
| 259 | + // [cfe] unspecified |
| 260 | + |
| 261 | + const Integer constInteger = Integer.constOne; |
| 262 | + const bool contextTypeIntegerEq = (constInteger as Object) == .constTwo; |
| 263 | + // ^ |
| 264 | + // [analyzer] unspecified |
| 265 | + // [cfe] unspecified |
| 266 | + |
| 267 | + const bool contextTypeIntegerNeq = (constInteger as Object) != .constTwo; |
| 268 | + // ^ |
| 269 | + // [analyzer] unspecified |
| 270 | + // [cfe] unspecified |
| 271 | + |
| 272 | + if ((integer as Object) == .one) print('not ok'); |
| 273 | + // ^ |
| 274 | + // [analyzer] unspecified |
| 275 | + // [cfe] unspecified |
| 276 | + |
| 277 | + if ((integer as Object) case == .constOne) print('not ok'); |
| 278 | + // ^ |
| 279 | + // [analyzer] unspecified |
| 280 | + // [cfe] unspecified |
| 281 | + |
| 282 | + if ((integer as Object) != .one) print('not ok'); |
| 283 | + // ^ |
| 284 | + // [analyzer] unspecified |
| 285 | + // [cfe] unspecified |
| 286 | + |
| 287 | + if ((integer as Object) case != .constOne) print('not ok'); |
| 288 | + // ^ |
| 289 | + // [analyzer] unspecified |
| 290 | + // [cfe] unspecified |
| 291 | + |
| 292 | + const IntegerExt constIntegerExt = IntegerExt.constOne; |
| 293 | + const bool contextTypeIntegerExtEq = (constIntegerExt as Object) == .constTwo; |
| 294 | + // ^ |
| 295 | + // [analyzer] unspecified |
| 296 | + // [cfe] unspecified |
| 297 | + |
| 298 | + const bool contextTypeIntegerExtNeq = (constIntegerExt as Object) != .constTwo; |
| 299 | + // ^ |
| 300 | + // [analyzer] unspecified |
| 301 | + // [cfe] unspecified |
| 302 | + |
| 303 | + if ((integerExt as Object) == .one) print('not ok'); |
| 304 | + // ^ |
| 305 | + // [analyzer] unspecified |
| 306 | + // [cfe] unspecified |
| 307 | + |
| 308 | + if ((integerExt as Object) case == .constOne) print('not ok'); |
| 309 | + // ^ |
| 310 | + // [analyzer] unspecified |
| 311 | + // [cfe] unspecified |
| 312 | + |
| 313 | + if ((integerExt as Object) != .one) print('not ok'); |
| 314 | + // ^ |
| 315 | + // [analyzer] unspecified |
| 316 | + // [cfe] unspecified |
| 317 | + |
| 318 | + if ((integerExt as Object) case != .constOne) print('not ok'); |
| 319 | + // ^ |
| 320 | + // [analyzer] unspecified |
| 321 | + // [cfe] unspecified |
| 322 | +} |
| 323 | + |
| 324 | +void typeParameterContext<C extends Color, T extends Object>(C color, T value) { |
| 325 | + if (color == .red) print('not ok'); |
| 326 | + // ^ |
| 327 | + // [analyzer] unspecified |
| 328 | + // [cfe] unspecified |
| 329 | + if (value is Color) { |
| 330 | + if (value == .red) print('not ok'); |
| 331 | + // ^ |
| 332 | + // [analyzer] unspecified |
| 333 | + // [cfe] unspecified |
| 334 | + } |
| 335 | +} |
| 336 | + |
| 337 | +void main() { |
| 338 | + Color color = .blue; |
| 339 | + Integer integer = .one; |
| 340 | + IntegerExt integerExt = .one; |
| 341 | + |
| 342 | + notSymmetrical(color, integer, integerExt); |
| 343 | + rhsNeedsToBeShorthand(color, integer, integerExt, true); |
| 344 | + rhsNeedsToBeShorthand(color, integer, integerExt, false); |
| 345 | + objectContextType(color, integer, integerExt); |
| 346 | + |
| 347 | + typeParameterContext(color, integer); |
| 348 | + typeParameterContext(color, Color.red); |
| 349 | + |
| 350 | + // Test the constant evaluation for enum shorthands in const constructor |
| 351 | + // asserts. |
| 352 | + const ConstConstructorAssert.blue(Color.blue); |
| 353 | + const ConstConstructorAssert.notBlue(Color.red); |
| 354 | + const ConstConstructorAssert.one(Integer.constOne); |
| 355 | + const ConstConstructorAssert.notOne(Integer.constTwo); |
| 356 | + const ConstConstructorAssert.oneExt(IntegerExt.constOne); |
| 357 | + const ConstConstructorAssert.notOneExt(IntegerExt.constTwo); |
| 358 | +} |
0 commit comments