Files
sdk/pkg/front_end/testcases/patterns/same_name_captured_variables.dart
T
Chloe Stefantsova 414f318a05 [cfe] Cache access to scrutinee members in pattern desugaring
The desugaring is presented as a series of nested if-statements
interleaved with intermediate variable declarations that cache the
results of accessing the members of the scrutinee object. Among other
things, it prevents double access to a list element when checking the
pattern conditions on it and then assigning it into a variable pattern.

Part of https://github.com/dart-lang/sdk/issues/49749

Change-Id: I5bf0b040c44d150eacef9a727a8a66fd3a9a568d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/270701
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2022-11-22 10:41:31 +00:00

28 lines
730 B
Dart

// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// The test checks that the captured variables with the same name are note
// placed into the same scope, which can be achieved, for example, by using new
// blocks to adjust scoping.
test(dynamic x1, dynamic x2) {
if (x1 case var y) {}
if (x1 case var y) {
if (x2 case var y) {
return y;
}
}
throw "Expected to never reach this line of the program.";
}
main() {
expectEquals(test(1, 2), 2);
}
expectEquals(x, y) {
if (x != y) {
throw "Expected ${x} and ${y} to be equal.";
}
}