|
| 1 | +// Copyright (c) 2022, 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 field promotion works during top level type inference. In the |
| 6 | +// CFE, top level types are inferred earlier than method bodies, so this |
| 7 | +// verifies that the data structures necessary to support field promotion have |
| 8 | +// been initialized in time. |
| 9 | + |
| 10 | +// SharedOptions=--enable-experiment=inference-update-2 |
| 11 | + |
| 12 | +class C { |
| 13 | + final int? _promotable; |
| 14 | + final int? _notPromotable; // due to D._notPromotable |
| 15 | + |
| 16 | + C(int i) |
| 17 | + : _promotable = i, |
| 18 | + _notPromotable = i; |
| 19 | + |
| 20 | + static final staticPromotable = |
| 21 | + ((C c) => c._promotable != null ? c._promotable : 0)(new C(0)); |
| 22 | + |
| 23 | + static final staticNotPromotable = |
| 24 | + ((C c) => c._notPromotable != null ? c._notPromotable : 0)(new C(0)); |
| 25 | + |
| 26 | + final instancePromotable = |
| 27 | + ((C c) => c._promotable != null ? c._promotable : 0)(new C(0)); |
| 28 | + |
| 29 | + final instanceNotPromotable = |
| 30 | + ((C c) => c._notPromotable != null ? c._notPromotable : 0)(new C(0)); |
| 31 | + |
| 32 | + late final instancePromotableViaThis = _promotable != null ? _promotable : 0; |
| 33 | + |
| 34 | + late final instanceNotPromotableViaThis = |
| 35 | + _notPromotable != null ? _notPromotable : 0; |
| 36 | +} |
| 37 | + |
| 38 | +class D { |
| 39 | + int? _notPromotable; |
| 40 | +} |
| 41 | + |
| 42 | +final topLevelPromotable = |
| 43 | + ((C c) => c._promotable != null ? c._promotable : 0)(new C(0)); |
| 44 | + |
| 45 | +final topLevelNotPromotable = |
| 46 | + ((C c) => c._notPromotable != null ? c._notPromotable : 0)(new C(0)); |
| 47 | + |
| 48 | +void acceptsInt(int x) {} |
| 49 | + |
| 50 | +void testTopLevelPromotable() { |
| 51 | + var x = topLevelPromotable; |
| 52 | + // `x` has type `int` so this is ok |
| 53 | + acceptsInt(x); |
| 54 | +} |
| 55 | + |
| 56 | +void testTopLevelNotPromotable() { |
| 57 | + var x = topLevelNotPromotable; |
| 58 | + // `x` has type `int?` so this is ok |
| 59 | + x = null; |
| 60 | +} |
| 61 | + |
| 62 | +void testStaticPromotable() { |
| 63 | + var x = C.staticPromotable; |
| 64 | + // `x` has type `int` so this is ok |
| 65 | + acceptsInt(x); |
| 66 | +} |
| 67 | + |
| 68 | +void testStaticNotPromotable() { |
| 69 | + var x = C.staticNotPromotable; |
| 70 | + // `x` has type `int?` so this is ok |
| 71 | + x = null; |
| 72 | +} |
| 73 | + |
| 74 | +void testInstancePromotable(C c) { |
| 75 | + var x = c.instancePromotable; |
| 76 | + // `x` has type `int` so this is ok |
| 77 | + acceptsInt(x); |
| 78 | +} |
| 79 | + |
| 80 | +void testInstanceNotPromotable(C c) { |
| 81 | + var x = c.instanceNotPromotable; |
| 82 | + // `x` has type `int?` so this is ok |
| 83 | + x = null; |
| 84 | +} |
| 85 | + |
| 86 | +void testInstancePromotableViaThis(C c) { |
| 87 | + var x = c.instancePromotableViaThis; |
| 88 | + // `x` has type `int` so this is ok |
| 89 | + acceptsInt(x); |
| 90 | +} |
| 91 | + |
| 92 | +void testInstanceNotPromotableViaThis(C c) { |
| 93 | + var x = c.instanceNotPromotableViaThis; |
| 94 | + // `x` has type `int?` so this is ok |
| 95 | + x = null; |
| 96 | +} |
| 97 | + |
| 98 | +main() {} |
0 commit comments