|
| 1 | +// Copyright (c) 2021, 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 | +// @dart=2.13 |
| 6 | + |
| 7 | +import '../../static_type_helper.dart'; |
| 8 | + |
| 9 | +// This test checks whether a local boolean variable can be used to perform type |
| 10 | +// promotion, if that variable is implicitly typed. |
| 11 | +// |
| 12 | +// Due to https://github.com/dart-lang/language/issues/1785, initializer |
| 13 | +// expressions on implicitly typed variables are ignored for the purposes of |
| 14 | +// type promotion (however, later assignments to those variables still do |
| 15 | +// influence promotion). To avoid introducing breaking language changes, we |
| 16 | +// intend to preserve this behavior until a specific Dart language version. |
| 17 | +// This test verifies that for code that is not opted in to the newer behavior, |
| 18 | +// the old (buggy) behavior persists. |
| 19 | + |
| 20 | +parameterUnmodified(int? x) { |
| 21 | + { |
| 22 | + late final b = x != null; |
| 23 | + // We wouldn't promote based on the initializers of late locals anyhow, |
| 24 | + // because we don't know when they execute. |
| 25 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 26 | + } |
| 27 | + { |
| 28 | + late final b; |
| 29 | + b = x != null; |
| 30 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 31 | + } |
| 32 | + { |
| 33 | + late var b = x != null; |
| 34 | + // We wouldn't promote based on the initializers of late locals anyhow, |
| 35 | + // because we don't know when they execute. |
| 36 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 37 | + } |
| 38 | + { |
| 39 | + late var b; |
| 40 | + b = x != null; |
| 41 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 42 | + } |
| 43 | + { |
| 44 | + final b = x != null; |
| 45 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 46 | + } |
| 47 | + { |
| 48 | + final b; |
| 49 | + b = x != null; |
| 50 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 51 | + } |
| 52 | + { |
| 53 | + var b = x != null; |
| 54 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 55 | + } |
| 56 | + { |
| 57 | + var b; |
| 58 | + b = x != null; |
| 59 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +parameterModifiedLater(int? x, int? y) { |
| 64 | + x = y; |
| 65 | + { |
| 66 | + late final b = x != null; |
| 67 | + // We wouldn't promote based on the initializers of late locals anyhow, |
| 68 | + // because we don't know when they execute. |
| 69 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 70 | + } |
| 71 | + { |
| 72 | + late final b; |
| 73 | + b = x != null; |
| 74 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 75 | + } |
| 76 | + { |
| 77 | + late var b = x != null; |
| 78 | + // We wouldn't promote based on the initializers of late locals anyhow, |
| 79 | + // because we don't know when they execute. |
| 80 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 81 | + } |
| 82 | + { |
| 83 | + late var b; |
| 84 | + b = x != null; |
| 85 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 86 | + } |
| 87 | + { |
| 88 | + final b = x != null; |
| 89 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 90 | + } |
| 91 | + { |
| 92 | + final b; |
| 93 | + b = x != null; |
| 94 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 95 | + } |
| 96 | + { |
| 97 | + var b = x != null; |
| 98 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 99 | + } |
| 100 | + { |
| 101 | + var b; |
| 102 | + b = x != null; |
| 103 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +localVariableInitialized(int? y) { |
| 108 | + int? x = y; |
| 109 | + { |
| 110 | + late final b = x != null; |
| 111 | + // We wouldn't promote based on the initializers of late locals anyhow, |
| 112 | + // because we don't know when they execute. |
| 113 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 114 | + } |
| 115 | + { |
| 116 | + late final b; |
| 117 | + b = x != null; |
| 118 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 119 | + } |
| 120 | + { |
| 121 | + late var b = x != null; |
| 122 | + // We wouldn't promote based on the initializers of late locals anyhow, |
| 123 | + // because we don't know when they execute. |
| 124 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 125 | + } |
| 126 | + { |
| 127 | + late var b; |
| 128 | + b = x != null; |
| 129 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 130 | + } |
| 131 | + { |
| 132 | + final b = x != null; |
| 133 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 134 | + } |
| 135 | + { |
| 136 | + final b; |
| 137 | + b = x != null; |
| 138 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 139 | + } |
| 140 | + { |
| 141 | + var b = x != null; |
| 142 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 143 | + } |
| 144 | + { |
| 145 | + var b; |
| 146 | + b = x != null; |
| 147 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +localVariableModifiedLater(int? y) { |
| 152 | + int? x; |
| 153 | + x = y; |
| 154 | + { |
| 155 | + late final b = x != null; |
| 156 | + // We wouldn't promote based on the initializers of late locals anyhow, |
| 157 | + // because we don't know when they execute. |
| 158 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 159 | + } |
| 160 | + { |
| 161 | + late final b; |
| 162 | + b = x != null; |
| 163 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 164 | + } |
| 165 | + { |
| 166 | + late var b = x != null; |
| 167 | + // We wouldn't promote based on the initializers of late locals anyhow, |
| 168 | + // because we don't know when they execute. |
| 169 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 170 | + } |
| 171 | + { |
| 172 | + late var b; |
| 173 | + b = x != null; |
| 174 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 175 | + } |
| 176 | + { |
| 177 | + final b = x != null; |
| 178 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 179 | + } |
| 180 | + { |
| 181 | + final b; |
| 182 | + b = x != null; |
| 183 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 184 | + } |
| 185 | + { |
| 186 | + var b = x != null; |
| 187 | + if (b) x.expectStaticType<Exactly<int?>>(); |
| 188 | + } |
| 189 | + { |
| 190 | + var b; |
| 191 | + b = x != null; |
| 192 | + if (b) x.expectStaticType<Exactly<int>>(); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +main() { |
| 197 | + parameterUnmodified(null); |
| 198 | + parameterUnmodified(0); |
| 199 | + parameterModifiedLater(null, null); |
| 200 | + parameterModifiedLater(null, 0); |
| 201 | + localVariableInitialized(null); |
| 202 | + localVariableInitialized(0); |
| 203 | + localVariableModifiedLater(null); |
| 204 | + localVariableModifiedLater(0); |
| 205 | +} |
0 commit comments