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); +}