mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 15:47:08 +00:00
Add tests for proposed version 2.32 changes to patterns.
Bug: https://github.com/dart-lang/sdk/issues/52199 Change-Id: I04fcac4a2b9a7e49a0231441feb764a022dd5ab8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/300320 Reviewed-by: Bob Nystrom <rnystrom@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
This commit is contained in:
parent
32abc0a682
commit
a29487d803
168
tests/language/patterns/version_2_32_changes_error_test.dart
Normal file
168
tests/language/patterns/version_2_32_changes_error_test.dart
Normal file
|
@ -0,0 +1,168 @@
|
|||
// Copyright (c) 2023, 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
|
||||
|
||||
// This test captures the changes introduced in 2.32 of the patterns proposal.
|
||||
|
||||
main() {
|
||||
// In a refutable context, it's an error for a variable in a variable pattern
|
||||
// to be named `when` or `as`.
|
||||
{
|
||||
switch (expr()) {
|
||||
case int when:
|
||||
// ^^^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
switch (expr()) {
|
||||
case int as:
|
||||
// ^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
switch (expr()) {
|
||||
case var when:
|
||||
// ^^^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
switch (expr()) {
|
||||
case var as:
|
||||
// ^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
switch (expr()) {
|
||||
case final int when:
|
||||
// ^^^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
switch (expr()) {
|
||||
case final int as:
|
||||
// ^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
switch (expr()) {
|
||||
case final when:
|
||||
// ^^^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
switch (expr()) {
|
||||
case final as:
|
||||
// ^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
}
|
||||
|
||||
// In a refutable context, it's an error for an identifier pattern to be named
|
||||
// `when` or `as`.
|
||||
{
|
||||
const when = 0;
|
||||
const as = 1;
|
||||
|
||||
switch (expr()) {
|
||||
case when:
|
||||
// ^^^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
switch (expr()) {
|
||||
case as:
|
||||
// ^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
}
|
||||
|
||||
// In a pattern variable declaration, it's an error for a variable in a
|
||||
// variable pattern to be named `when` or `as`.
|
||||
{
|
||||
var (int when) = expr<int>();
|
||||
// ^^^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
{
|
||||
var (int as) = expr<int>();
|
||||
// ^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
|
||||
// In a pattern variable declaration, it's an error for an identifier pattern
|
||||
// to be named `when` or `as`.
|
||||
{
|
||||
var (when) = expr();
|
||||
// ^^^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
{
|
||||
var (as) = expr();
|
||||
// ^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
|
||||
// In a pattern assignment, it's an error for an identifier pattern to be
|
||||
// named `when` or `as`.
|
||||
{
|
||||
Object? when;
|
||||
Object? as;
|
||||
|
||||
(when) = expr();
|
||||
// ^^^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
(as) = expr();
|
||||
// ^^
|
||||
// [analyzer] unspecified
|
||||
// [cfe] unspecified
|
||||
}
|
||||
|
||||
// It is, however, ok for `when` or `as` to appear as part of a qualified name
|
||||
// in a constant pattern, provided it's not otherwise prohibited by the
|
||||
// grammar (e.g. `as` can't be used as a prefix because it's a builtin).
|
||||
{
|
||||
switch (expr()) {
|
||||
case C.when:
|
||||
}
|
||||
|
||||
switch (expr()) {
|
||||
case when.as:
|
||||
}
|
||||
}
|
||||
|
||||
// And it is ok for `when` to be used as the type of a variable or wildcard
|
||||
// pattern, or as the type of an object pattern. (`as` can't be used in this
|
||||
// way because it's not the legal name of a type).
|
||||
{
|
||||
switch (expr()) {
|
||||
case when _:
|
||||
}
|
||||
|
||||
switch (expr()) {
|
||||
case when w:
|
||||
}
|
||||
|
||||
switch (expr()) {
|
||||
case when():
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
T expr<T>() => throw UnimplementedError();
|
||||
|
||||
class C {
|
||||
static const when = 0;
|
||||
}
|
||||
|
||||
class when {
|
||||
static const as = 1;
|
||||
}
|
Loading…
Reference in a new issue