mirror of
https://github.com/dart-lang/sdk
synced 2024-09-19 14:43:32 +00:00
1694bb85c8
Change-Id: I69f09a4f854b6c84dfaff82abbf0214565bf6aa6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293881 Reviewed-by: Bob Nystrom <rnystrom@google.com> Commit-Queue: Bob Nystrom <rnystrom@google.com>
135 lines
3.6 KiB
Dart
135 lines
3.6 KiB
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.
|
|
|
|
// SharedOptions=--enable-experiment=patterns
|
|
|
|
main() {
|
|
// Error to assign to pattern variables in guard.
|
|
switch (false) {
|
|
case bool x when x = true:
|
|
// ^
|
|
// [analyzer] COMPILE_TIME_ERROR.PATTERN_VARIABLE_ASSIGNMENT_INSIDE_GUARD
|
|
// [cfe] Pattern variables can't be assigned inside the guard of the enclosing guarded pattern.
|
|
print(x);
|
|
default:
|
|
}
|
|
|
|
print(switch (false) {
|
|
bool x when x = true => x,
|
|
// ^
|
|
// [analyzer] COMPILE_TIME_ERROR.PATTERN_VARIABLE_ASSIGNMENT_INSIDE_GUARD
|
|
// [cfe] Pattern variables can't be assigned inside the guard of the enclosing guarded pattern.
|
|
_ => false
|
|
});
|
|
|
|
if (false case bool x when x = true) {
|
|
// ^
|
|
// [analyzer] COMPILE_TIME_ERROR.PATTERN_VARIABLE_ASSIGNMENT_INSIDE_GUARD
|
|
// [cfe] Pattern variables can't be assigned inside the guard of the enclosing guarded pattern.
|
|
print(x);
|
|
}
|
|
|
|
print([
|
|
if (false case bool x when x = true)
|
|
// ^
|
|
// [analyzer] COMPILE_TIME_ERROR.PATTERN_VARIABLE_ASSIGNMENT_INSIDE_GUARD
|
|
// [cfe] Pattern variables can't be assigned inside the guard of the enclosing guarded pattern.
|
|
x
|
|
]);
|
|
|
|
// Error even if assignment is nested inside closure.
|
|
switch (false) {
|
|
case var x
|
|
when () {
|
|
return x = true;
|
|
// ^
|
|
// [analyzer] COMPILE_TIME_ERROR.PATTERN_VARIABLE_ASSIGNMENT_INSIDE_GUARD
|
|
// [cfe] Pattern variables can't be assigned inside the guard of the enclosing guarded pattern.
|
|
}():
|
|
print(x);
|
|
default:
|
|
}
|
|
|
|
print(switch (false) {
|
|
var x
|
|
when (() {
|
|
return x = true;
|
|
// ^
|
|
// [analyzer] COMPILE_TIME_ERROR.PATTERN_VARIABLE_ASSIGNMENT_INSIDE_GUARD
|
|
// [cfe] Pattern variables can't be assigned inside the guard of the enclosing guarded pattern.
|
|
})() =>
|
|
x,
|
|
_ => false
|
|
});
|
|
|
|
if (false case var x
|
|
when (() {
|
|
return x = true;
|
|
// ^
|
|
// [analyzer] COMPILE_TIME_ERROR.PATTERN_VARIABLE_ASSIGNMENT_INSIDE_GUARD
|
|
// [cfe] Pattern variables can't be assigned inside the guard of the enclosing guarded pattern.
|
|
})()) {
|
|
print(x);
|
|
}
|
|
|
|
print([
|
|
if (false case var x
|
|
when (() {
|
|
return x = true;
|
|
// ^
|
|
// [analyzer] COMPILE_TIME_ERROR.PATTERN_VARIABLE_ASSIGNMENT_INSIDE_GUARD
|
|
// [cfe] Pattern variables can't be assigned inside the guard of the enclosing guarded pattern.
|
|
})())
|
|
x
|
|
]);
|
|
|
|
// Not an error to assign to other variables.
|
|
var local = false;
|
|
switch (false) {
|
|
case var x when local = true:
|
|
// No error.
|
|
print(x);
|
|
default:
|
|
}
|
|
|
|
print(switch (false) {
|
|
var x when local = true => x, // No error.
|
|
_ => 0,
|
|
});
|
|
|
|
if (false case var x when local = true) {
|
|
// No error.
|
|
print(x);
|
|
}
|
|
|
|
print([
|
|
if (false case var x when local = true) x // No error.
|
|
]);
|
|
|
|
// Not an error to assign to pattern variables from other pattern.
|
|
switch (false) {
|
|
case bool x:
|
|
switch (false) {
|
|
case bool y when x = true:
|
|
// No error.
|
|
print(y);
|
|
default:
|
|
}
|
|
|
|
print(switch (false) {
|
|
bool y when x = true => y, // No error.
|
|
_ => 0,
|
|
});
|
|
|
|
if (false case bool y when x = true) {
|
|
// No error.
|
|
print(y);
|
|
}
|
|
|
|
print([
|
|
if (false case bool y when x = true) x // No error.
|
|
]);
|
|
}
|
|
}
|