|
| 1 | +// Copyright (c) 2023, 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 when `inference-update-3` is disabled, and an assignment is made |
| 6 | +// to a local variable (or parameter), the unpromoted type of the variable is |
| 7 | +// used as the context for the RHS of the assignment. |
| 8 | + |
| 9 | +// @dart=3.3 |
| 10 | + |
| 11 | +import 'package:expect/expect.dart'; |
| 12 | + |
| 13 | +import '../static_type_helper.dart'; |
| 14 | + |
| 15 | +void testNonDemotingAssignmentOfParameter(num? x, num? y) { |
| 16 | + if (x != null) { |
| 17 | + x = contextType(1)..expectStaticType<Exactly<num>>(); |
| 18 | + } |
| 19 | + if (y is int) { |
| 20 | + y = contextType(1)..expectStaticType<Exactly<int>>(); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +void testNonDemotingAssignmentOfExplicitlyTypedLocal(num? x) { |
| 25 | + num? y = x; |
| 26 | + if (y != null) { |
| 27 | + y = contextType(1)..expectStaticType<Exactly<num>>(); |
| 28 | + } |
| 29 | + y = x; |
| 30 | + if (y is int) { |
| 31 | + y = contextType(1)..expectStaticType<Exactly<int>>(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +void testNonDemotingAssignmentOfImplicitlyTypedLocal(num? x) { |
| 36 | + var y = x; |
| 37 | + if (y != null) { |
| 38 | + y = contextType(1)..expectStaticType<Exactly<num>>(); |
| 39 | + } |
| 40 | + y = x; |
| 41 | + if (y is int) { |
| 42 | + y = contextType(1)..expectStaticType<Exactly<int>>(); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void testDemotingAssignmentOfParameter(num? x, num? y) { |
| 47 | + if (x != null) { |
| 48 | + // A type error is expected at runtime, because type inference will fill in |
| 49 | + // a type of `num` for the type argument of `contextType`, and `contextType` |
| 50 | + // tries to cast its input to its type argument. |
| 51 | + // |
| 52 | + // We can't use `Expect.throwsTypeError` to check for this, because then the |
| 53 | + // assignment would be happening inside a function expression, blocking type |
| 54 | + // promotion. |
| 55 | + bool errorOccurred = false; |
| 56 | + try { |
| 57 | + x = contextType(null)..expectStaticType<Exactly<num>>(); |
| 58 | + } on TypeError { |
| 59 | + errorOccurred = true; |
| 60 | + } |
| 61 | + Expect.equals(hasSoundNullSafety, errorOccurred); |
| 62 | + } |
| 63 | + if (y is int) { |
| 64 | + // A type error is expected at runtime, because type inference will fill in |
| 65 | + // a type of `int` for the type argument of `contextType`, and `contextType` |
| 66 | + // tries to cast its input to its type argument. |
| 67 | + // |
| 68 | + // We can't use `Expect.throwsTypeError` to check for this, because then the |
| 69 | + // assignment would be happening inside a function expression, blocking type |
| 70 | + // promotion. |
| 71 | + bool errorOccurred = false; |
| 72 | + try { |
| 73 | + y = contextType(1.5)..expectStaticType<Exactly<int>>(); |
| 74 | + } on TypeError { |
| 75 | + errorOccurred = true; |
| 76 | + } |
| 77 | + Expect.isTrue(errorOccurred); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void testDemotingAssignmentOfExplicitlyTypedLocal(num? x) { |
| 82 | + num? y = x; |
| 83 | + if (y != null) { |
| 84 | + // A type error is expected at runtime, because type inference will fill in |
| 85 | + // a type of `num` for the type argument of `contextType`, and `contextType` |
| 86 | + // tries to cast its input to its type argument. |
| 87 | + // |
| 88 | + // We can't use `Expect.throwsTypeError` to check for this, because then the |
| 89 | + // assignment would be happening inside a function expression, blocking type |
| 90 | + // promotion. |
| 91 | + bool errorOccurred = false; |
| 92 | + try { |
| 93 | + y = contextType(null)..expectStaticType<Exactly<num>>(); |
| 94 | + } on TypeError { |
| 95 | + errorOccurred = true; |
| 96 | + } |
| 97 | + Expect.equals(hasSoundNullSafety, errorOccurred); |
| 98 | + } |
| 99 | + y = x; |
| 100 | + if (y is int) { |
| 101 | + // A type error is expected at runtime, because type inference will fill in |
| 102 | + // a type of `int` for the type argument of `contextType`, and `contextType` |
| 103 | + // tries to cast its input to its type argument. |
| 104 | + // |
| 105 | + // We can't use `Expect.throwsTypeError` to check for this, because then the |
| 106 | + // assignment would be happening inside a function expression, blocking type |
| 107 | + // promotion. |
| 108 | + bool errorOccurred = false; |
| 109 | + try { |
| 110 | + y = contextType(1.5)..expectStaticType<Exactly<int>>(); |
| 111 | + } on TypeError { |
| 112 | + errorOccurred = true; |
| 113 | + } |
| 114 | + Expect.isTrue(errorOccurred); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +void testDemotingAssignmentOfImplicitlyTypedLocal(num? x) { |
| 119 | + var y = x; |
| 120 | + if (y != null) { |
| 121 | + // A type error is expected at runtime, because type inference will fill in |
| 122 | + // a type of `num` for the type argument of `contextType`, and `contextType` |
| 123 | + // tries to cast its input to its type argument. |
| 124 | + // |
| 125 | + // We can't use `Expect.throwsTypeError` to check for this, because then the |
| 126 | + // assignment would be happening inside a function expression, blocking type |
| 127 | + // promotion. |
| 128 | + bool errorOccurred = false; |
| 129 | + try { |
| 130 | + y = contextType(null)..expectStaticType<Exactly<num>>(); |
| 131 | + } on TypeError { |
| 132 | + errorOccurred = true; |
| 133 | + } |
| 134 | + Expect.equals(hasSoundNullSafety, errorOccurred); |
| 135 | + } |
| 136 | + y = x; |
| 137 | + if (y is int) { |
| 138 | + // A type error is expected at runtime, because type inference will fill in |
| 139 | + // a type of `int` for the type argument of `contextType`, and `contextType` |
| 140 | + // tries to cast its input to its type argument. |
| 141 | + // |
| 142 | + // We can't use `Expect.throwsTypeError` to check for this, because then the |
| 143 | + // assignment would be happening inside a function expression, blocking type |
| 144 | + // promotion. |
| 145 | + bool errorOccurred = false; |
| 146 | + try { |
| 147 | + y = contextType(1.5)..expectStaticType<Exactly<int>>(); |
| 148 | + } on TypeError { |
| 149 | + errorOccurred = true; |
| 150 | + } |
| 151 | + Expect.isTrue(errorOccurred); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +main() { |
| 156 | + for (var x in [null, 0, 0.5]) { |
| 157 | + testNonDemotingAssignmentOfParameter(x, x); |
| 158 | + testNonDemotingAssignmentOfExplicitlyTypedLocal(x); |
| 159 | + testNonDemotingAssignmentOfImplicitlyTypedLocal(x); |
| 160 | + testDemotingAssignmentOfParameter(x, x); |
| 161 | + testDemotingAssignmentOfExplicitlyTypedLocal(x); |
| 162 | + testDemotingAssignmentOfImplicitlyTypedLocal(x); |
| 163 | + } |
| 164 | +} |
0 commit comments