From bc854d66422bee5ca15f8d2d33a6e9da83eeef2e Mon Sep 17 00:00:00 2001 From: Nicholas Shahan Date: Tue, 21 Sep 2021 19:16:37 +0000 Subject: [PATCH] [tests] Add nested function subtype test Change-Id: I04368d00010e81148c608ba0d8f0371a516862e0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/213966 Commit-Queue: Nicholas Shahan Reviewed-by: Erik Ernst --- .../nested_function_type_test.dart | 44 ++++++++++++++++++ .../nested_function_type_test.dart | 46 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 tests/language/function_subtype/nested_function_type_test.dart create mode 100644 tests/language_2/function_subtype/nested_function_type_test.dart diff --git a/tests/language/function_subtype/nested_function_type_test.dart b/tests/language/function_subtype/nested_function_type_test.dart new file mode 100644 index 00000000000..a284690734a --- /dev/null +++ b/tests/language/function_subtype/nested_function_type_test.dart @@ -0,0 +1,44 @@ +// Copyright (c) 2021, 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. + +import "package:expect/expect.dart"; + +// Two function types that are identical except the argument type of the +// nested function is a type variable from the outer function. +typedef Fn = void Function(S val) Function(T val); +typedef Gn = void Function(T val) Function(T val); + +void Function(S) fn(T val) => (R val) {}; +void Function(T) gn(T val) => (T val) {}; + +// The same pattern here except with bounds on the type arguments. +typedef Xn = void Function(S val) Function(T val); +typedef Yn = void Function(T val) Function(T val); + +void Function(S) xn(T val) => + (R val) {}; +void Function(T) yn(T val) => + (T val) {}; + +// The nested function here uses concrete type in the argument position so it +// should satisfy either of the previous typedefs. +void Function(num) zn(T val) => + (num val) {}; + +void main() { + Expect.isTrue(fn is Fn); + Expect.isFalse(fn is Gn); + + Expect.isTrue(gn is Gn); + Expect.isFalse(gn is Fn); + + Expect.isTrue(xn is Xn); + Expect.isFalse(xn is Yn); + + Expect.isTrue(yn is Yn); + Expect.isFalse(yn is Xn); + + Expect.isTrue(zn is Xn); + Expect.isTrue(zn is Yn); +} diff --git a/tests/language_2/function_subtype/nested_function_type_test.dart b/tests/language_2/function_subtype/nested_function_type_test.dart new file mode 100644 index 00000000000..79bc0aae677 --- /dev/null +++ b/tests/language_2/function_subtype/nested_function_type_test.dart @@ -0,0 +1,46 @@ +// Copyright (c) 2021, 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. + +// @dart = 2.9 + +import "package:expect/expect.dart"; + +// Two function types that are identical except the argument type of the +// nested function is a type variable from the outer function. +typedef Fn = void Function(S val) Function(T val); +typedef Gn = void Function(T val) Function(T val); + +void Function(S) fn(T val) => (R val) {}; +void Function(T) gn(T val) => (T val) {}; + +// The same pattern here except with bounds on the type arguments. +typedef Xn = void Function(S val) Function(T val); +typedef Yn = void Function(T val) Function(T val); + +void Function(S) xn(T val) => + (R val) {}; +void Function(T) yn(T val) => + (T val) {}; + +// The nested function here uses concrete type in the argument position so it +// should satisfy either of the previous typedefs. +void Function(num) zn(T val) => + (num val) {}; + +void main() { + Expect.isTrue(fn is Fn); + Expect.isFalse(fn is Gn); + + Expect.isTrue(gn is Gn); + Expect.isFalse(gn is Fn); + + Expect.isTrue(xn is Xn); + Expect.isFalse(xn is Yn); + + Expect.isTrue(yn is Yn); + Expect.isFalse(yn is Xn); + + Expect.isTrue(zn is Xn); + Expect.isTrue(zn is Yn); +}