|
| 1 | +// Copyright (c) 2025, 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 | +// Regression test for http://dartbug.com/60409. |
| 6 | +// |
| 7 | +// It appears that dart2js fails to correctly model scopes of for-loops in |
| 8 | +// control-flow collections in plain field initializer expressions. |
| 9 | +// |
| 10 | +// The `field` and `fieldFinal` cases failed at the time of reporting #60409, |
| 11 | +// the other variations passed. |
| 12 | + |
| 13 | +import 'package:expect/expect.dart'; |
| 14 | + |
| 15 | +final List logField = []; |
| 16 | +final List logFinalField = []; |
| 17 | +final List logLateField = []; |
| 18 | +final List logLateFinalField = []; |
| 19 | +final List logConstructorInitializedField = []; |
| 20 | +final List logStaticField = []; |
| 21 | +final List logStaticFinalField = []; |
| 22 | + |
| 23 | +class A { |
| 24 | + List<int> field = [ |
| 25 | + for (int i = 0; i < 9; ++i) |
| 26 | + () { |
| 27 | + logField.add(i); |
| 28 | + i += 2; |
| 29 | + return i; |
| 30 | + }(), |
| 31 | + ]; |
| 32 | + |
| 33 | + final List<int> finalField = [ |
| 34 | + for (int i = 0; i < 9; ++i) |
| 35 | + () { |
| 36 | + logFinalField.add(i); |
| 37 | + i += 2; |
| 38 | + return i; |
| 39 | + }(), |
| 40 | + ]; |
| 41 | + |
| 42 | + final List<int> constructorInitializedField; |
| 43 | + |
| 44 | + late List<int> lateField = [ |
| 45 | + for (int i = 0; i < 9; ++i) |
| 46 | + () { |
| 47 | + logLateField.add(i); |
| 48 | + i += 2; |
| 49 | + return i; |
| 50 | + }(), |
| 51 | + ]; |
| 52 | + |
| 53 | + late final List<int> lateFinalField = [ |
| 54 | + for (int i = 0; i < 9; ++i) |
| 55 | + () { |
| 56 | + logLateFinalField.add(i); |
| 57 | + i += 2; |
| 58 | + return i; |
| 59 | + }(), |
| 60 | + ]; |
| 61 | + |
| 62 | + static List<int> staticField = [ |
| 63 | + for (int i = 0; i < 9; ++i) |
| 64 | + () { |
| 65 | + logStaticField.add(i); |
| 66 | + i += 2; |
| 67 | + return i; |
| 68 | + }(), |
| 69 | + ]; |
| 70 | + |
| 71 | + static final List<int> staticFinalField = [ |
| 72 | + for (int i = 0; i < 9; ++i) |
| 73 | + () { |
| 74 | + logStaticFinalField.add(i); |
| 75 | + i += 2; |
| 76 | + return i; |
| 77 | + }(), |
| 78 | + ]; |
| 79 | + |
| 80 | + int version = ++_version; |
| 81 | + static int _version = 0; |
| 82 | + |
| 83 | + A() |
| 84 | + : constructorInitializedField = [ |
| 85 | + for (int i = 0; i < 9; ++i) |
| 86 | + () { |
| 87 | + logConstructorInitializedField.add(i); |
| 88 | + i += 2; |
| 89 | + return i; |
| 90 | + }(), |
| 91 | + ] { |
| 92 | + print('A($version)'); |
| 93 | + logField.add('c'); |
| 94 | + logFinalField.add('c'); |
| 95 | + logLateField.add('c'); |
| 96 | + logLateFinalField.add('c'); |
| 97 | + logConstructorInitializedField.add('c'); |
| 98 | + logStaticField.add('c'); |
| 99 | + logStaticFinalField.add('c'); |
| 100 | + } |
| 101 | + |
| 102 | + /// Use all the fields to ensure lazy initialization has happened. |
| 103 | + void use() { |
| 104 | + field; |
| 105 | + finalField; |
| 106 | + constructorInitializedField; |
| 107 | + lateField; |
| 108 | + lateFinalField; |
| 109 | + staticField; |
| 110 | + staticFinalField; |
| 111 | + } |
| 112 | + |
| 113 | + void check() { |
| 114 | + void expect(String expected, List log) { |
| 115 | + Expect.equals(expected, log.join(',')); |
| 116 | + } |
| 117 | + |
| 118 | + expect('2,5,8', field); |
| 119 | + expect('2,5,8', finalField); |
| 120 | + expect('2,5,8', constructorInitializedField); |
| 121 | + expect('2,5,8', lateField); |
| 122 | + expect('2,5,8', lateFinalField); |
| 123 | + expect('2,5,8', staticField); |
| 124 | + expect('2,5,8', staticFinalField); |
| 125 | + |
| 126 | + expect('0,3,6,c,x,0,3,6,c', logField); |
| 127 | + expect('0,3,6,c,x,0,3,6,c', logFinalField); |
| 128 | + expect('0,3,6,c,x,0,3,6,c', logConstructorInitializedField); |
| 129 | + expect('c,0,3,6,x,c,0,3,6', logLateField); |
| 130 | + expect('c,0,3,6,x,c,0,3,6', logLateFinalField); |
| 131 | + expect('c,0,3,6,x,c', logStaticField); |
| 132 | + expect('c,0,3,6,x,c', logStaticFinalField); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void main() { |
| 137 | + final a1 = A(); |
| 138 | + a1.use(); |
| 139 | + |
| 140 | + logField.add('x'); |
| 141 | + logFinalField.add('x'); |
| 142 | + logLateField.add('x'); |
| 143 | + logLateFinalField.add('x'); |
| 144 | + logConstructorInitializedField.add('x'); |
| 145 | + logStaticField.add('x'); |
| 146 | + logStaticFinalField.add('x'); |
| 147 | + |
| 148 | + final a2 = A(); |
| 149 | + a2.use(); |
| 150 | + a2.check(); |
| 151 | +} |
0 commit comments