Add (passing) test for type parameters in exhaustiveness checking.

Change-Id: I2b2dc6a586d66c93e918b077bd008c3683986d18
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/301300
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
This commit is contained in:
Robert Nystrom 2023-05-04 12:18:49 +00:00 committed by Commit Queue
parent 0c7281fee0
commit af07557984

View file

@ -0,0 +1,34 @@
// 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
// Test that using a type parameter as a type test in a pattern doesn't
// incorrectly use the bound for exhaustiveness.
//
// The bound isn't a correct approximation because the type argument could be
// a subtype of the bound (including `Never`), which will cause the type test
// to match fewer values than the bound.
void test<T extends int>(num value) {
// Object pattern.
var result = switch (value) {
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_EXPRESSION
// ^
// [cfe] The type 'num' is not exhaustively matched by the switch cases since it doesn't match 'int()'.
T() => 'T',
double() => 'double'
};
// Variable type.
result = switch (value) {
// ^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_EXHAUSTIVE_SWITCH_EXPRESSION
// ^
// [cfe] The type 'num' is not exhaustively matched by the switch cases since it doesn't match 'int()'.
T _ => 'T',
double _ => 'double'
};
}