|
| 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 | +// Tests that assignment expressions in if statements as described |
| 6 | +// by https://github.com/dart-lang/language/issues/3658 don't promote when the |
| 7 | +// `inference-update-4` feature flag is disabled. |
| 8 | + |
| 9 | +// @dart=3.6 |
| 10 | + |
| 11 | +import '../static_type_helper.dart'; |
| 12 | + |
| 13 | +class A { |
| 14 | + A operator +(int i) { |
| 15 | + return new B(); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class B extends A {} |
| 20 | + |
| 21 | +int? nullableInt() => 1; |
| 22 | + |
| 23 | +testEqNull_assignIfNull() { |
| 24 | + int? x = null; |
| 25 | + if ((x ??= nullableInt()) == null) { |
| 26 | + x.expectStaticType<Exactly<int?>>(); |
| 27 | + } else { |
| 28 | + // Since `inference-update-4` isn't enabled, flow analysis won't promote `x` |
| 29 | + // if it's in an assignment expression, leaving the type as `int?` even |
| 30 | + // though it can't be nullable in this `else`. |
| 31 | + x.expectStaticType<Exactly<int?>>(); |
| 32 | + } |
| 33 | + x.expectStaticType<Exactly<int?>>(); |
| 34 | +} |
| 35 | + |
| 36 | +testEqNull_eq() { |
| 37 | + int? x = null; |
| 38 | + if ((x = nullableInt()) == null) { |
| 39 | + x.expectStaticType<Exactly<int?>>(); |
| 40 | + } else { |
| 41 | + // Since `inference-update-4` isn't enabled, flow analysis won't promote `x` |
| 42 | + // if it's in an assignment expression, leaving the type as `int?` even |
| 43 | + // though it can't be nullable in this `else`. |
| 44 | + x.expectStaticType<Exactly<int?>>(); |
| 45 | + } |
| 46 | + x.expectStaticType<Exactly<int?>>(); |
| 47 | +} |
| 48 | + |
| 49 | +testNeqNull_assignIfNull() { |
| 50 | + int? x = null; |
| 51 | + if ((x ??= nullableInt()) != null) { |
| 52 | + // Since `inference-update-4` isn't enabled, flow analysis won't promote `x` |
| 53 | + // if it's in an assignment expression, leaving the type as `int?`. |
| 54 | + x.expectStaticType<Exactly<int?>>(); |
| 55 | + } |
| 56 | + x.expectStaticType<Exactly<int?>>(); |
| 57 | +} |
| 58 | + |
| 59 | +testNeqNull_eq() { |
| 60 | + int? x = null; |
| 61 | + if ((x = nullableInt()) != null) { |
| 62 | + // Since `inference-update-4` isn't enabled, flow analysis won't promote `x` |
| 63 | + // if it's in an assignment expression, leaving the type as `int?`. |
| 64 | + x.expectStaticType<Exactly<int?>>(); |
| 65 | + } |
| 66 | + x.expectStaticType<Exactly<int?>>(); |
| 67 | +} |
| 68 | + |
| 69 | +testIs_eq() { |
| 70 | + int? x = null; |
| 71 | + if ((x = nullableInt()) is int) { |
| 72 | + // Since `inference-update-4` isn't enabled, flow analysis won't promote `x` |
| 73 | + // if it's in an assignment expression, leaving the type as `int?`. |
| 74 | + x.expectStaticType<Exactly<int?>>(); |
| 75 | + } |
| 76 | + x.expectStaticType<Exactly<int?>>(); |
| 77 | +} |
| 78 | + |
| 79 | +testIs_plusEq() { |
| 80 | + A x = A(); |
| 81 | + if ((x += 1) is B) { |
| 82 | + // Since `inference-update-4` isn't enabled, flow analysis won't promote `x` |
| 83 | + // if it's in an assignment expression, leaving the type as `A`. |
| 84 | + x.expectStaticType<Exactly<A>>(); |
| 85 | + } |
| 86 | + x.expectStaticType<Exactly<A>>(); |
| 87 | +} |
| 88 | + |
| 89 | +testIs_postfix() { |
| 90 | + A x = A(); |
| 91 | + if (x++ is B) { |
| 92 | + x.expectStaticType<Exactly<A>>(); |
| 93 | + } |
| 94 | + x.expectStaticType<Exactly<A>>(); |
| 95 | +} |
| 96 | + |
| 97 | +testIs_prefix() { |
| 98 | + A x = A(); |
| 99 | + if ((++x) is B) { |
| 100 | + // Since `inference-update-4` isn't enabled, flow analysis won't promote `x` |
| 101 | + // if it's in an assignment expression, leaving the type as `A`. |
| 102 | + x.expectStaticType<Exactly<A>>(); |
| 103 | + } |
| 104 | + x.expectStaticType<Exactly<A>>(); |
| 105 | +} |
| 106 | + |
| 107 | +main() { |
| 108 | + testEqNull_assignIfNull(); |
| 109 | + testEqNull_eq(); |
| 110 | + testNeqNull_assignIfNull(); |
| 111 | + testNeqNull_eq(); |
| 112 | + testIs_eq(); |
| 113 | + testIs_plusEq(); |
| 114 | + testIs_postfix(); |
| 115 | + testIs_prefix(); |
| 116 | +} |
0 commit comments