Skip to content

Commit 46bd9f3

Browse files
natebiggsCommit Queue
authored andcommitted
[ddc] Handle optional/defaulted parameters when created scoped parameter renames for sync* transform.
This was missed in the original implementation of the sync* transformer because prior to my recent change, ScopedIds couldn't end up within a DestructuredVariable. Change-Id: I2733ce1e01edb50659634347204bac1769269615 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401080 Reviewed-by: Nicholas Shahan <[email protected]> Commit-Queue: Nate Biggs <[email protected]>
1 parent f6ecfba commit 46bd9f3

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

pkg/dev_compiler/lib/src/compiler/rewrite_async.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,8 +2197,13 @@ class SyncStarRewriter extends AsyncRewriterBase {
21972197
for (var parameter in parameters) {
21982198
final name = parameter.parameterName;
21992199
final renamedIdentifier = ScopedId(name);
2200-
final parameterRef =
2201-
parameter is ScopedId ? parameter : js_ast.Identifier(name);
2200+
final parameterRef = switch (parameter) {
2201+
ScopedId() => ScopedId.from(parameter),
2202+
js_ast.DestructuredVariable() when parameter.name is ScopedId =>
2203+
ScopedId.from(parameter.name as ScopedId),
2204+
_ => js_ast.Identifier(name)
2205+
};
2206+
22022207
innerDeclarationsList
22032208
.add(js_ast.VariableInitialization(parameterRef, renamedIdentifier));
22042209
outerDeclarationsList
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2024, 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+
// Ensure optional parameters get renamed properly for sync* transformations.
6+
7+
Iterable<num> range(num startOrStop, [num? stop, num? step]) sync* {
8+
final start = stop == null ? 0 : startOrStop;
9+
stop ??= startOrStop;
10+
step ??= 1;
11+
12+
if (step == 0) throw ArgumentError('step cannot be 0');
13+
if (step > 0 && stop < start) {
14+
throw ArgumentError('if step is positive, stop must be greater than start');
15+
}
16+
if (step < 0 && stop > start) {
17+
throw ArgumentError('if step is negative, stop must be less than start');
18+
}
19+
20+
for (
21+
num value = start;
22+
step < 0 ? value > stop : value < stop;
23+
value += step
24+
) {
25+
yield value;
26+
}
27+
}
28+
29+
void main() {
30+
print(range(10, 20, 2));
31+
}

0 commit comments

Comments
 (0)