|
| 1 | +// Copyright (c) 2026, 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 a variable declared by a for-in loop can be promoted inside the |
| 6 | +// loop even if a closure later in the loop body writes to it. |
| 7 | +// |
| 8 | +// Each iteration of the loop gets a fresh copy of the loop variable, so a |
| 9 | +// closure created in one iteration can only write to that iteration's copy. |
| 10 | +// Therefore the closure can't invalidate a promotion that happens in the same |
| 11 | +// iteration before the closure is created, nor can it invalidate promotions in |
| 12 | +// other iterations. |
| 13 | + |
| 14 | +import 'package:expect/expect.dart'; |
| 15 | +import 'package:expect/static_type_helper.dart'; |
| 16 | + |
| 17 | +void statementWriteCapturedLater(List<Object?> xs) { |
| 18 | + var closures = <void Function()>[]; |
| 19 | + for (var x in xs) { |
| 20 | + if (x is int) { |
| 21 | + x.expectStaticType<Exactly<int>>(); |
| 22 | + // Invoking closures from earlier iterations doesn't affect `x`. |
| 23 | + for (var closure in closures) { |
| 24 | + closure(); |
| 25 | + } |
| 26 | + Expect.isTrue(x.isEven); |
| 27 | + } |
| 28 | + closures.add(() { |
| 29 | + x = null; |
| 30 | + }); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +void statementWriteCapturedEarlier(List<Object?> xs) { |
| 35 | + for (var x in xs) { |
| 36 | + () { |
| 37 | + x = null; |
| 38 | + }; |
| 39 | + if (x is int) { |
| 40 | + x.expectStaticType<Exactly<Object?>>(); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +void statementWriteCapturedInNestedLoop(List<Object?> xs) { |
| 46 | + for (var x in xs) { |
| 47 | + for (var i = 0; i < 2; i++) { |
| 48 | + // A closure created in an earlier iteration of the inner loop writes the |
| 49 | + // same copy of `x`, so `x` is considered write captured here and does |
| 50 | + // not promote. |
| 51 | + if (x is int) { |
| 52 | + x.expectStaticType<Exactly<Object?>>(); |
| 53 | + } |
| 54 | + () { |
| 55 | + x = null; |
| 56 | + }; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void identifierWriteCaptured(List<Object?> xs) { |
| 62 | + // In this form, every iteration assigns to the same variable, so the |
| 63 | + // promotion must be invalidated. |
| 64 | + Object? x; |
| 65 | + for (x in xs) { |
| 66 | + if (x is int) { |
| 67 | + x.expectStaticType<Exactly<Object?>>(); |
| 68 | + } |
| 69 | + () { |
| 70 | + x = null; |
| 71 | + }; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +Future<void> asyncStatementWriteCapturedLater(Stream<Object?> xs) async { |
| 76 | + await for (var x in xs) { |
| 77 | + if (x is int) { |
| 78 | + x.expectStaticType<Exactly<int>>(); |
| 79 | + } |
| 80 | + () { |
| 81 | + x = null; |
| 82 | + }; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void listElementWriteCapturedLater(List<Object?> xs) { |
| 87 | + [ |
| 88 | + for (var x in xs) ...[ |
| 89 | + if (x is int) x.expectStaticType<Exactly<int>>(), |
| 90 | + () { |
| 91 | + x = null; |
| 92 | + }, |
| 93 | + ], |
| 94 | + ]; |
| 95 | +} |
| 96 | + |
| 97 | +void listElementWriteCapturedEarlier(List<Object?> xs) { |
| 98 | + [ |
| 99 | + for (var x in xs) ...[ |
| 100 | + () { |
| 101 | + x = null; |
| 102 | + }, |
| 103 | + if (x is int) x.expectStaticType<Exactly<Object?>>(), |
| 104 | + ], |
| 105 | + ]; |
| 106 | +} |
| 107 | + |
| 108 | +void setElementWriteCapturedLater(List<Object?> xs) { |
| 109 | + <Object?>{ |
| 110 | + for (var x in xs) ...[ |
| 111 | + if (x is int) x.expectStaticType<Exactly<int>>(), |
| 112 | + () { |
| 113 | + x = null; |
| 114 | + }, |
| 115 | + ], |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +void mapElementWriteCapturedLater(List<Object?> xs) { |
| 120 | + <Object?, Object?>{ |
| 121 | + for (var x in xs) ...{ |
| 122 | + if (x is int) x.expectStaticType<Exactly<int>>(): null, |
| 123 | + () { |
| 124 | + x = null; |
| 125 | + }: null, |
| 126 | + }, |
| 127 | + }; |
| 128 | +} |
| 129 | + |
| 130 | +main() async { |
| 131 | + statementWriteCapturedLater([0, null, 2]); |
| 132 | + statementWriteCapturedEarlier([0, null]); |
| 133 | + statementWriteCapturedInNestedLoop([0, null]); |
| 134 | + identifierWriteCaptured([0, null]); |
| 135 | + await asyncStatementWriteCapturedLater(Stream.fromIterable([0, null])); |
| 136 | + listElementWriteCapturedLater([0, null]); |
| 137 | + listElementWriteCapturedEarlier([0, null]); |
| 138 | + setElementWriteCapturedLater([0, null]); |
| 139 | + mapElementWriteCapturedLater([0, null]); |
| 140 | +} |
0 commit comments