|
| 1 | +// Copyright (c) 2020, 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 | +// Verifies that variables assigned in loops are de-promoted at the top of the |
| 6 | +// loop body, since the loop body be executed multiple times. |
| 7 | + |
| 8 | +void forLoopAssignInCondition(Object x) { |
| 9 | + if (x is int) { |
| 10 | + print(x.isEven); // Verify that promotion occurred |
| 11 | + for (; 0 == 0 ? (x = 0) == 0 : true;) { |
| 12 | + // The assignment to x does de-promote because it happens after the top of |
| 13 | + // the loop, so flow analysis cannot check that the assigned value is an |
| 14 | + // int at the time de-promotion occurs. |
| 15 | + print(x.isEven); |
| 16 | + // ^^^^^^ |
| 17 | + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER |
| 18 | + // [cfe] The getter 'isEven' isn't defined for the class 'Object'. |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +void forLoopAssignInUpdater(Object x) { |
| 24 | + if (x is int) { |
| 25 | + print(x.isEven); // Verify that promotion occurred |
| 26 | + for (;; x = 0) { |
| 27 | + // The assignment to x does de-promote because it happens after the top of |
| 28 | + // the loop, so flow analysis cannot check that the assigned value is an |
| 29 | + // int at the time de-promotion occurs. |
| 30 | + print(x.isEven); |
| 31 | + // ^^^^^^ |
| 32 | + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER |
| 33 | + // [cfe] The getter 'isEven' isn't defined for the class 'Object'. |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +void forLoopAssignInBody(Object x) { |
| 39 | + if (x is int) { |
| 40 | + print(x.isEven); // Verify that promotion occurred |
| 41 | + for (;;) { |
| 42 | + // The assignment to x does de-promote because it happens after the top of |
| 43 | + // the loop, so flow analysis cannot check that the assigned value is an |
| 44 | + // int at the time de-promotion occurs. |
| 45 | + print(x.isEven); |
| 46 | + // ^^^^^^ |
| 47 | + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER |
| 48 | + // [cfe] The getter 'isEven' isn't defined for the class 'Object'. |
| 49 | + x = 0; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void forEachAssignInBody(Object x) { |
| 55 | + if (x is int) { |
| 56 | + print(x.isEven); // Verify that promotion occurred |
| 57 | + for (var y in [0]) { |
| 58 | + // The assignment to x does de-promote because it happens after the top of |
| 59 | + // the loop, so flow analysis cannot check that the assigned value is an |
| 60 | + // int at the time de-promotion occurs. |
| 61 | + print(x.isEven); |
| 62 | + // ^^^^^^ |
| 63 | + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER |
| 64 | + // [cfe] The getter 'isEven' isn't defined for the class 'Object'. |
| 65 | + x = 0; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +void whileAssignInCondition(Object x) { |
| 71 | + if (x is int) { |
| 72 | + print(x.isEven); // Verify that promotion occurred |
| 73 | + while (0 == 0 ? (x = 0) == 0 : true) { |
| 74 | + // The assignment to x does de-promote because it happens after the top of |
| 75 | + // the loop, so flow analysis cannot check that the assigned value is an |
| 76 | + // int at the time de-promotion occurs. |
| 77 | + print(x.isEven); |
| 78 | + // ^^^^^^ |
| 79 | + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER |
| 80 | + // [cfe] The getter 'isEven' isn't defined for the class 'Object'. |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void whileAssignInBody(Object x) { |
| 86 | + if (x is int) { |
| 87 | + print(x.isEven); // Verify that promotion occurred |
| 88 | + while (true) { |
| 89 | + // The assignment to x does de-promote because it happens after the top of |
| 90 | + // the loop, so flow analysis cannot check that the assigned value is an |
| 91 | + // int at the time de-promotion occurs. |
| 92 | + print(x.isEven); |
| 93 | + // ^^^^^^ |
| 94 | + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER |
| 95 | + // [cfe] The getter 'isEven' isn't defined for the class 'Object'. |
| 96 | + x = 0; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +void doAssignInCondition(Object x) { |
| 102 | + if (x is int) { |
| 103 | + print(x.isEven); // Verify that promotion occurred |
| 104 | + do { |
| 105 | + // The assignment to x does de-promote because it happens after the top of |
| 106 | + // the loop, so flow analysis cannot check that the assigned value is an |
| 107 | + // int at the time de-promotion occurs. |
| 108 | + print(x.isEven); |
| 109 | + // ^^^^^^ |
| 110 | + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER |
| 111 | + // [cfe] The getter 'isEven' isn't defined for the class 'Object'. |
| 112 | + } while ((x = 0) == 0); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void doAssignInBody(Object x) { |
| 117 | + if (x is int) { |
| 118 | + print(x.isEven); // Verify that promotion occurred |
| 119 | + do { |
| 120 | + // The assignment to x does de-promote because it happens after the top of |
| 121 | + // the loop, so flow analysis cannot check that the assigned value is an |
| 122 | + // int at the time de-promotion occurs. |
| 123 | + print(x.isEven); |
| 124 | + // ^^^^^^ |
| 125 | + // [analyzer] STATIC_TYPE_WARNING.UNDEFINED_GETTER |
| 126 | + // [cfe] The getter 'isEven' isn't defined for the class 'Object'. |
| 127 | + x = 0; |
| 128 | + } while (true); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +main() { |
| 133 | + forLoopAssignInCondition(0); |
| 134 | + forLoopAssignInUpdater(0); |
| 135 | + forLoopAssignInBody(0); |
| 136 | + forEachAssignInBody(0); |
| 137 | + whileAssignInCondition(0); |
| 138 | + whileAssignInBody(0); |
| 139 | + doAssignInCondition(0); |
| 140 | + doAssignInBody(0); |
| 141 | +} |
0 commit comments